-
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.
Consul cache background update (#21)
Background cache update for ConsulServiceAddressCache.cs
- Loading branch information
Showing
4 changed files
with
149 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,54 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Net; | ||
using System.Threading.Tasks; | ||
using ATI.Services.Common.Behaviors; | ||
using ATI.Services.Common.Metrics; | ||
using Consul; | ||
using NLog; | ||
|
||
namespace ATI.Services.Consul; | ||
|
||
internal class ConsulAdapter: IDisposable | ||
{ | ||
private readonly ILogger _logger = LogManager.GetCurrentClassLogger(); | ||
private readonly ConsulClient _consulClient = new(); | ||
private readonly MetricsFactory _metricsFactory = MetricsFactory.CreateHttpClientMetricsFactory(nameof(ConsulAdapter), "consul"); | ||
|
||
/// <summary> | ||
/// Возвращает список живых инстансов сервиса | ||
/// </summary> | ||
/// <returns></returns> | ||
public async Task<OperationResult<List<ServiceEntry>>> GetPassingServiceInstancesAsync( | ||
string serviceName, | ||
string environment, | ||
bool passingOnly = true) | ||
{ | ||
try | ||
{ | ||
using (_metricsFactory.CreateMetricsTimer("/health/service/:service")) | ||
{ | ||
var fromConsul = await _consulClient.Health.Service(serviceName, environment, passingOnly); | ||
if (fromConsul.StatusCode == HttpStatusCode.OK) | ||
{ | ||
return new(fromConsul.Response?.ToList()); | ||
} | ||
|
||
_logger.Error( | ||
$"По запросу в консул {serviceName}:{environment}, вернулся ответ со статусом: {fromConsul.StatusCode}"); | ||
} | ||
} | ||
catch (Exception e) | ||
{ | ||
_logger.Error(e); | ||
} | ||
|
||
return new(ActionStatus.InternalServerError); | ||
} | ||
|
||
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,65 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Net; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using ATI.Services.Common.Behaviors; | ||
using ATI.Services.Common.Extensions; | ||
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 readonly bool _passingOnly; | ||
private List<ServiceEntry> _cachedServices; | ||
private readonly Timer _updateCacheTimer; | ||
private Task<OperationResult<List<ServiceEntry>>> _updateCacheTask; | ||
private readonly ConsulAdapter _consulAdapter; | ||
|
||
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, passingOnly); | ||
_cachedServices = _updateCacheTask.GetAwaiter().GetResult() is var result && result.Success | ||
? result.Value | ||
: new List<ServiceEntry>(); | ||
|
||
_updateCacheTimer = new Timer(_ => ReloadCache().Forget(), null, ttl, ttl); | ||
} | ||
|
||
/// <summary> | ||
/// Обеспечивает получение доступных сервисов от консула и их кеширование (опционально) | ||
/// Возвращает коллекцию сервисов | ||
/// </summary> | ||
public class ConsulServiceAddressCache | ||
/// <returns></returns> | ||
public List<ServiceEntry> GetCachedObjectsAsync() => _cachedServices; | ||
|
||
/// <summary> | ||
/// Запускает таску на обновление кеша | ||
/// </summary> | ||
private async Task ReloadCache() | ||
{ | ||
private Task<List<ServiceEntry>> _reloadCacheTask; | ||
private readonly ILogger _logger = LogManager.GetCurrentClassLogger(); | ||
private readonly bool _useCaching; | ||
private readonly string _serviceName; | ||
private readonly string _environment; | ||
|
||
public ConsulServiceAddressCache(bool useCaching, string serviceName, string environment) | ||
{ | ||
_useCaching = useCaching; | ||
_serviceName = serviceName; | ||
_environment = environment; | ||
|
||
if (!_useCaching) | ||
return; | ||
|
||
_reloadCacheTask = GetServiceFromConsulAsync(); | ||
} | ||
|
||
/// <summary> | ||
/// Возвращает коллекцию сервисов | ||
/// </summary> | ||
/// <returns></returns> | ||
public Task<List<ServiceEntry>> GetCachedObjectsAsync() | ||
{ | ||
return _useCaching ? _reloadCacheTask : GetServiceFromConsulAsync(); | ||
} | ||
if(_updateCacheTask == null || _updateCacheTask.IsCompleted) | ||
_updateCacheTask = _consulAdapter.GetPassingServiceInstancesAsync(_serviceName, _environment, _passingOnly); | ||
|
||
/// <summary> | ||
/// Запускает таску на обновление кеша, если кеширование включено | ||
/// </summary> | ||
public void ReloadCache() | ||
{ | ||
if (!_useCaching) | ||
return; | ||
|
||
if (!_reloadCacheTask.IsCompleted) | ||
return; | ||
|
||
_reloadCacheTask = GetServiceFromConsulAsync(); | ||
} | ||
_cachedServices = await _updateCacheTask is var result && result.Success | ||
? result.Value | ||
: _cachedServices; | ||
} | ||
|
||
/// <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(); | ||
_consulAdapter.Dispose(); | ||
} | ||
} |