-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Background cache update for ConsulServiceAddressCache.cs
- Loading branch information
Showing
4 changed files
with
122 additions
and
92 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Net; | ||
using System.Threading.Tasks; | ||
using Consul; | ||
using NLog; | ||
|
||
namespace ATI.Services.Consul; | ||
|
||
internal class ConsulAdapter: IDisposable | ||
{ | ||
private readonly ILogger _logger = LogManager.GetCurrentClassLogger(); | ||
private ConsulClient _consulClient; | ||
|
||
/// <summary> | ||
/// Возвращает список живых сервисов | ||
/// </summary> | ||
/// <returns></returns> | ||
public async Task<List<ServiceEntry>> GetPassingServiceInstancesAsync(string serviceName, string environment, bool passingOnly = true, ulong index = 0, TimeSpan? waitTime = null) | ||
{ | ||
try | ||
{ | ||
_consulClient = new ConsulClient(); | ||
var fromConsul = await _consulClient.Health.Service(serviceName, environment, passingOnly, new QueryOptions { WaitIndex = index, WaitTime = waitTime}); | ||
if (fromConsul.StatusCode == HttpStatusCode.OK) | ||
{ | ||
return fromConsul.Response?.ToList(); | ||
} | ||
|
||
_logger.Error($"По запросу в консул {serviceName}:{environment}, вернулся ответ со статусом: {fromConsul.StatusCode}"); | ||
} | ||
catch (Exception e) | ||
{ | ||
_logger.Error(e); | ||
} | ||
|
||
return new List<ServiceEntry>(); | ||
} | ||
|
||
public void Dispose() | ||
{ | ||
_consulClient?.Dispose(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,79 +1,51 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Net; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using Consul; | ||
using NLog; | ||
|
||
namespace ATI.Services.Consul | ||
namespace ATI.Services.Consul; | ||
|
||
/// <summary> | ||
/// Обеспечивает получение доступных сервисов от консула и их кеширование (опционально) | ||
/// </summary> | ||
internal class ConsulServiceAddressCache: IDisposable | ||
{ | ||
private readonly string _serviceName; | ||
private readonly string _environment; | ||
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) | ||
{ | ||
_serviceName = serviceName; | ||
_environment = environment; | ||
_consulAdapter = new ConsulAdapter(); | ||
_updateCacheTimer = new Timer(_ => ReloadCache(), null, ttl, ttl); | ||
_cachedServices = _consulAdapter.GetPassingServiceInstancesAsync(serviceName, environment).GetAwaiter().GetResult(); | ||
} | ||
|
||
/// <summary> | ||
/// Обеспечивает получение доступных сервисов от консула и их кеширование (опционально) | ||
/// Возвращает коллекцию сервисов | ||
/// </summary> | ||
public class ConsulServiceAddressCache | ||
{ | ||
private Task<List<ServiceEntry>> _reloadCacheTask; | ||
private readonly ILogger _logger = LogManager.GetCurrentClassLogger(); | ||
private readonly bool _useCaching; | ||
private readonly string _serviceName; | ||
private readonly string _environment; | ||
/// <returns></returns> | ||
public List<ServiceEntry> GetCachedObjectsAsync() => _cachedServices; | ||
|
||
public ConsulServiceAddressCache(bool useCaching, string serviceName, string environment) | ||
{ | ||
_useCaching = useCaching; | ||
_serviceName = serviceName; | ||
_environment = environment; | ||
|
||
if (!_useCaching) | ||
return; | ||
|
||
_reloadCacheTask = GetServiceFromConsulAsync(); | ||
} | ||
/// <summary> | ||
/// Запускает таску на обновление кеша | ||
/// </summary> | ||
private void ReloadCache() | ||
{ | ||
if(_updateCacheTask.IsCompleted) | ||
_updateCacheTask = _consulAdapter.GetPassingServiceInstancesAsync(_serviceName, _environment); | ||
|
||
/// <summary> | ||
/// Возвращает коллекцию сервисов | ||
/// </summary> | ||
/// <returns></returns> | ||
public Task<List<ServiceEntry>> GetCachedObjectsAsync() | ||
{ | ||
return _useCaching ? _reloadCacheTask : GetServiceFromConsulAsync(); | ||
} | ||
|
||
/// <summary> | ||
/// Запускает таску на обновление кеша, если кеширование включено | ||
/// </summary> | ||
public void ReloadCache() | ||
{ | ||
if (!_useCaching) | ||
return; | ||
|
||
if (!_reloadCacheTask.IsCompleted) | ||
return; | ||
|
||
_reloadCacheTask = GetServiceFromConsulAsync(); | ||
} | ||
_cachedServices = _updateCacheTask.GetAwaiter().GetResult(); | ||
} | ||
|
||
/// <summary> | ||
/// Возвращает список живых сервисов | ||
/// </summary> | ||
/// <returns></returns> | ||
private async Task<List<ServiceEntry>> GetServiceFromConsulAsync() | ||
{ | ||
try | ||
{ | ||
using var cc = new ConsulClient(); | ||
var fromConsul = await cc.Health.Service(_serviceName, _environment, true); | ||
if (fromConsul.StatusCode == HttpStatusCode.OK && fromConsul.Response.Length > 0) | ||
{ | ||
return fromConsul.Response.ToList(); | ||
} | ||
} | ||
catch (Exception e) | ||
{ | ||
_logger.Error(e); | ||
} | ||
return new List<ServiceEntry>(); | ||
} | ||
public void Dispose() | ||
{ | ||
_updateCacheTimer.Dispose(); | ||
} | ||
} |