Host on edges

This commit is contained in:
2023-08-03 18:34:53 +01:00
parent 52ce412491
commit 5a2bd21cec
7 changed files with 125 additions and 4 deletions

View File

@ -0,0 +1,35 @@
using System.Diagnostics;
using MaxMind.GeoIP2;
namespace NostrStreamer.Services;
public class EdgeSteering
{
private readonly Config _config;
private readonly IGeoIP2DatabaseReader _db;
private readonly ILogger<EdgeSteering> _logger;
public EdgeSteering(Config config, IGeoIP2DatabaseReader db, ILogger<EdgeSteering> logger)
{
_config = config;
_db = db;
_logger = logger;
}
public EdgeLocation? GetEdge(HttpContext ctx)
{
var sw = Stopwatch.StartNew();
var loc = ctx.GetLocation(_db);
if (loc != default)
{
var ret = _config.Edges.MinBy(a => Extensions.GetDistance(a.Longitude, a.Latitude, loc.Value.lon, loc.Value.lat));
sw.Stop();
_logger.LogTrace("Found edge in {n:#,##0.#}ms", sw.Elapsed.TotalMilliseconds);
return ret;
}
sw.Stop();
_logger.LogTrace("Found no edge in {n:#,##0.#}ms", sw.Elapsed.TotalMilliseconds);
return default;
}
}