using System.Collections.Concurrent; namespace MP.IOC.Services { public class InMemoryWeightProvider : IWeightProvider { private readonly ConcurrentDictionary _map = new(); private readonly int _defaultOld; private readonly int _defaultNew; public InMemoryWeightProvider(IConfiguration config) { _defaultOld = config.GetValue("RouteMan:DefaultWeightOld", 100); _defaultNew = config.GetValue("RouteMan:DefaultWeightNew", 0); } public (int oldWeight, int newWeight) GetWeightsFor(string method) { if (string.IsNullOrEmpty(method)) method = "unknown"; return _map.GetOrAdd(method, _ => (_defaultOld, _defaultNew)); } public void SetWeights(string method, int oldWeight, int newWeight) { _map[method] = (Math.Clamp(oldWeight, 0, 100), Math.Clamp(newWeight, 0, 100)); } } }