Skip to content

Commit 6535fe0

Browse files
MrZoidbergMike Oldfield
andauthored
VaultChangeWatcher refactored to support multiple VaultConfiguration providers (#13)
Co-authored-by: Mike Oldfield <[email protected]>
1 parent 378f151 commit 6535fe0

File tree

1 file changed

+26
-14
lines changed

1 file changed

+26
-14
lines changed

Source/VaultSharp.Extensions.Configuration/VaultChangeWatcher.cs

Lines changed: 26 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ namespace VaultSharp.Extensions.Configuration
1717
public class VaultChangeWatcher : BackgroundService
1818
{
1919
private readonly ILogger? _logger;
20-
private VaultConfigurationProvider? _configProvider;
20+
private IEnumerable<VaultConfigurationProvider> _configProviders;
2121

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

3535
this._logger = logger;
3636

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

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

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

5356
while (!stoppingToken.IsCancellationRequested)
5457
{
55-
this._logger?.LogInformation(
56-
$"VaultChangeWatcher will wait for {waitForSec} seconds");
57-
await Task.Delay(TimeSpan.FromSeconds(waitForSec), stoppingToken).ConfigureAwait(false);
58+
await Task.Delay(TimeSpan.FromSeconds(minTime), stoppingToken).ConfigureAwait(false);
5859
if (stoppingToken.IsCancellationRequested)
5960
{
6061
break;
6162
}
6263

63-
this._logger?.LogInformation(
64-
"Vault configuration reload is triggered by VaultChangeWatcher");
65-
this._configProvider.Load();
64+
for (var j = 0; j < this._configProviders.Count(); j++)
65+
{
66+
var timer = timers[j];
67+
timer -= minTime;
68+
if (timer <= 0)
69+
{
70+
this._configProviders.ElementAt(j).Load();
71+
timers[j] = this._configProviders.ElementAt(j).ConfigurationSource.Options.ReloadCheckIntervalSeconds;
72+
}
73+
else
74+
{
75+
timers[j] = timer;
76+
}
77+
}
6678
}
6779
}
6880
}

0 commit comments

Comments
 (0)