Skip to content

Commit

Permalink
Refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
muphblu committed Nov 13, 2023
1 parent f2d063b commit 827b541
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 8 deletions.
4 changes: 2 additions & 2 deletions ATI.Services.Consul/ConsulAdapter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@ internal class ConsulAdapter: IDisposable
/// Возвращает список живых сервисов
/// </summary>
/// <returns></returns>
public async Task<List<ServiceEntry>> GetPassingServiceInstancesAsync(string serviceName, string environment, bool passingOnly = true, ulong index = 0, TimeSpan? waitTime = null)
public async Task<List<ServiceEntry>> 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();
Expand Down
7 changes: 4 additions & 3 deletions ATI.Services.Consul/ConsulServiceAddress.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,22 +20,23 @@ 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;
_serviceName = 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);
}
}

Expand Down
11 changes: 8 additions & 3 deletions ATI.Services.Consul/ConsulServiceAddressCache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,22 @@ internal class ConsulServiceAddressCache: IDisposable
{
private readonly string _serviceName;
private readonly string _environment;
private readonly bool _passingOnly;
private List<ServiceEntry> _cachedServices;
private readonly Timer _updateCacheTimer;
private Task<List<ServiceEntry>> _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);
}
Expand All @@ -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();
}
Expand Down

0 comments on commit 827b541

Please sign in to comment.