using System.Collections.Concurrent; using System.Net; namespace PrivaPub.Infrastructure.Http { public interface IConnectedAddresses { void Remember(string host, IPAddress address); IPAddress Of(string host); } public class ConnectedAddresses : IConnectedAddresses { const int MaxHosts = 50_000; readonly ConcurrentDictionary _addresses = new(StringComparer.OrdinalIgnoreCase); public void Remember(string host, IPAddress address) { if (string.IsNullOrEmpty(host) || address == default) return; if (_addresses.Count >= MaxHosts) _addresses.Clear(); _addresses[host] = address.IsIPv4MappedToIPv6 ? address.MapToIPv4() : address; } public IPAddress Of(string host) => host != default && _addresses.TryGetValue(host, out var address) ? address : default; } }