Skip to content

Commit

Permalink
VaultChangeWatcher refactored to support multiple VaultConfiguration …
Browse files Browse the repository at this point in the history
…providers (#13)

Co-authored-by: Mike Oldfield <[email protected]>
  • Loading branch information
MrZoidberg and Mike Oldfield authored Mar 9, 2021
1 parent 378f151 commit 6535fe0
Showing 1 changed file with 26 additions and 14 deletions.
40 changes: 26 additions & 14 deletions Source/VaultSharp.Extensions.Configuration/VaultChangeWatcher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ namespace VaultSharp.Extensions.Configuration
public class VaultChangeWatcher : BackgroundService
{
private readonly ILogger? _logger;
private VaultConfigurationProvider? _configProvider;
private IEnumerable<VaultConfigurationProvider> _configProviders;

/// <summary>
/// Initializes a new instance of the <see cref="VaultChangeWatcher"/> class.
Expand All @@ -34,35 +34,47 @@ public VaultChangeWatcher(IConfigurationRoot configurationRoot, ILogger? logger

this._logger = logger;

this._configProvider = (VaultConfigurationProvider?)configurationRoot.Providers.FirstOrDefault(p =>
p is VaultConfigurationProvider);
this._configProviders = configurationRoot.Providers.OfType<VaultConfigurationProvider>().Where(p => p.ConfigurationSource.Options.ReloadOnChange).ToList() !;
}

/// <inheritdoc />
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
{
if (this._configProvider == null || !this._configProvider.ConfigurationSource.Options.ReloadOnChange)
Dictionary<int, int> timers = new Dictionary<int, int>(); // key - index of config provider, value - timer
var minTime = int.MaxValue;
var i = 0;
foreach (VaultConfigurationProvider provider in this._configProviders)
{
this._logger?.LogInformation(
"VaultChangeWatcher won't work because configuration provider is null or ReloadOnChange is disabled");
return;
var waitForSec = provider.ConfigurationSource.Options.ReloadCheckIntervalSeconds;
minTime = Math.Min(minTime, waitForSec);
timers[i] = waitForSec;
i++;
}

int waitForSec = this._configProvider.ConfigurationSource.Options.ReloadCheckIntervalSeconds;
this._logger?.LogInformation($"VaultChangeWatcher will use {minTime} seconds interval");

while (!stoppingToken.IsCancellationRequested)
{
this._logger?.LogInformation(
$"VaultChangeWatcher will wait for {waitForSec} seconds");
await Task.Delay(TimeSpan.FromSeconds(waitForSec), stoppingToken).ConfigureAwait(false);
await Task.Delay(TimeSpan.FromSeconds(minTime), stoppingToken).ConfigureAwait(false);
if (stoppingToken.IsCancellationRequested)
{
break;
}

this._logger?.LogInformation(
"Vault configuration reload is triggered by VaultChangeWatcher");
this._configProvider.Load();
for (var j = 0; j < this._configProviders.Count(); j++)
{
var timer = timers[j];
timer -= minTime;
if (timer <= 0)
{
this._configProviders.ElementAt(j).Load();
timers[j] = this._configProviders.ElementAt(j).ConfigurationSource.Options.ReloadCheckIntervalSeconds;
}
else
{
timers[j] = timer;
}
}
}
}
}
Expand Down

0 comments on commit 6535fe0

Please sign in to comment.