feat: add prefix cache warm-up routine to PrefixService
- Introduce `WarmUpAsync` method to pre-cache prefixes for ASNs and RU-specific routes. - Update `Program` to execute cache warm-up before starting BGP operations. - Extend `IPrefixService` interface with `WarmUpAsync`.
This commit is contained in:
parent
e0ddb980bd
commit
0a9300db57
3 changed files with 39 additions and 0 deletions
|
|
@ -78,6 +78,38 @@ public sealed class PrefixService : IPrefixService
|
|||
return GetPrefixesForAsns(ruAsns);
|
||||
}
|
||||
|
||||
public async Task WarmUpAsync()
|
||||
{
|
||||
var lists = _config.RipeStat?.AsnLists ?? [];
|
||||
|
||||
var allAsns = lists.SelectMany(l => l.Asns).Distinct().ToList();
|
||||
foreach (var asn in allAsns)
|
||||
{
|
||||
try
|
||||
{
|
||||
await GetPrefixesAsync(asn);
|
||||
Console.WriteLine($" WarmUp: AS{asn} cached");
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.WriteLine($" WarmUp: AS{asn} failed — {ex.Message}");
|
||||
}
|
||||
}
|
||||
|
||||
if (lists.Any(l => l.Country is not null))
|
||||
{
|
||||
try
|
||||
{
|
||||
var ru = await GetRuPrefixesAsync();
|
||||
Console.WriteLine($" WarmUp: RU prefixes — {ru.Count} entries");
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.WriteLine($" WarmUp: RU prefixes failed — {ex.Message}");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static IReadOnlyList<(uint Prefix, byte Length)> LoadFromFile(string path)
|
||||
{
|
||||
var result = new List<(uint Prefix, byte Length)>();
|
||||
|
|
|
|||
|
|
@ -6,4 +6,5 @@ public interface IPrefixService
|
|||
Task<List<(uint Prefix, byte Length, uint Asn)>> GetPrefixesForAsns(IEnumerable<uint> asns);
|
||||
Task<int> GetPrefixCountAsync(uint asn);
|
||||
Task<List<(uint Prefix, byte Length, uint Asn)>> GetRuPrefixesAsync();
|
||||
Task WarmUpAsync();
|
||||
}
|
||||
|
|
|
|||
|
|
@ -135,6 +135,12 @@ var logger = host.Services.GetRequiredService<ILogger<Program>>();
|
|||
logger.LogInformation("BGPLite starting — ASN={Asn}, RouterId={RouterId}", config.Bgp.Asn, config.Bgp.RouterId);
|
||||
logger.LogInformation("Loaded routes: {RouteCount}", routeTable.Count);
|
||||
|
||||
// Pre-warm prefix cache before accepting BGP connections
|
||||
Console.WriteLine("Warming up prefix cache...");
|
||||
var prefixService = host.Services.GetRequiredService<IPrefixService>();
|
||||
await prefixService.WarmUpAsync();
|
||||
Console.WriteLine("Prefix cache ready");
|
||||
|
||||
await host.RunAsync();
|
||||
return;
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue