diff --git a/ATI.Services.Consul/ConsulAdapter.cs b/ATI.Services.Consul/ConsulAdapter.cs index ce7188a..dd3c4bb 100644 --- a/ATI.Services.Consul/ConsulAdapter.cs +++ b/ATI.Services.Consul/ConsulAdapter.cs @@ -17,12 +17,12 @@ internal class ConsulAdapter: IDisposable /// Возвращает список живых сервисов /// /// - public async Task> GetPassingServiceInstancesAsync(string serviceName, string environment, bool passingOnly = true, ulong index = 0, TimeSpan? waitTime = null) + public async Task> GetPassingServiceInstancesAsync(string serviceName, string environment, bool passingOnly = true) { try { _consulClient = new ConsulClient(); - var fromConsul = await _consulClient.Health.Service(serviceName, environment, passingOnly, new QueryOptions { WaitIndex = index, WaitTime = waitTime}); + var fromConsul = await _consulClient.Health.Service(serviceName, environment, passingOnly); if (fromConsul.StatusCode == HttpStatusCode.OK) { return fromConsul.Response?.ToList(); diff --git a/ATI.Services.Consul/ConsulServiceAddress.cs b/ATI.Services.Consul/ConsulServiceAddress.cs index 408c8ee..88f5c1c 100644 --- a/ATI.Services.Consul/ConsulServiceAddress.cs +++ b/ATI.Services.Consul/ConsulServiceAddress.cs @@ -20,7 +20,8 @@ public class ConsulServiceAddress : IDisposable public ConsulServiceAddress(string serviceName, string environment, TimeSpan? timeToReload = null, - bool useCaching = true) + bool useCaching = true, + bool passingOnly = true) { timeToReload ??= TimeSpan.FromSeconds(5); _environment = environment; @@ -28,14 +29,14 @@ public ConsulServiceAddress(string serviceName, if (useCaching) { - _serviceAddressCache = new ConsulServiceAddressCache(_serviceName, _environment, timeToReload.Value); + _serviceAddressCache = new ConsulServiceAddressCache(_serviceName, _environment, timeToReload.Value, passingOnly); _getServices = () => Task.FromResult(_serviceAddressCache.GetCachedObjectsAsync()); } else { _consulAdapter = new ConsulAdapter(); _getServices = async () => - await _consulAdapter.GetPassingServiceInstancesAsync(serviceName, environment); + await _consulAdapter.GetPassingServiceInstancesAsync(serviceName, environment, passingOnly); } } diff --git a/ATI.Services.Consul/ConsulServiceAddressCache.cs b/ATI.Services.Consul/ConsulServiceAddressCache.cs index d9cb057..59c97fb 100644 --- a/ATI.Services.Consul/ConsulServiceAddressCache.cs +++ b/ATI.Services.Consul/ConsulServiceAddressCache.cs @@ -13,17 +13,22 @@ internal class ConsulServiceAddressCache: IDisposable { private readonly string _serviceName; private readonly string _environment; + private readonly bool _passingOnly; private List _cachedServices; private readonly Timer _updateCacheTimer; private Task> _updateCacheTask; private readonly ConsulAdapter _consulAdapter; - public ConsulServiceAddressCache(string serviceName, string environment, TimeSpan ttl) + public ConsulServiceAddressCache(string serviceName, + string environment, + TimeSpan ttl, + bool passingOnly = true) { _serviceName = serviceName; _environment = environment; + _passingOnly = passingOnly; _consulAdapter = new ConsulAdapter(); - _updateCacheTask = _consulAdapter.GetPassingServiceInstancesAsync(_serviceName, _environment); + _updateCacheTask = _consulAdapter.GetPassingServiceInstancesAsync(_serviceName, _environment, passingOnly); _cachedServices = _updateCacheTask.GetAwaiter().GetResult(); _updateCacheTimer = new Timer(_ => ReloadCache(), null, ttl, ttl); } @@ -40,7 +45,7 @@ public ConsulServiceAddressCache(string serviceName, string environment, TimeSpa private void ReloadCache() { if(_updateCacheTask == null || _updateCacheTask.IsCompleted) - _updateCacheTask = _consulAdapter.GetPassingServiceInstancesAsync(_serviceName, _environment); + _updateCacheTask = _consulAdapter.GetPassingServiceInstancesAsync(_serviceName, _environment, _passingOnly); _cachedServices = _updateCacheTask.GetAwaiter().GetResult(); }