@@ -17,7 +17,7 @@ namespace VaultSharp.Extensions.Configuration
17
17
public class VaultChangeWatcher : BackgroundService
18
18
{
19
19
private readonly ILogger ? _logger ;
20
- private VaultConfigurationProvider ? _configProvider ;
20
+ private IEnumerable < VaultConfigurationProvider > _configProviders ;
21
21
22
22
/// <summary>
23
23
/// Initializes a new instance of the <see cref="VaultChangeWatcher"/> class.
@@ -34,35 +34,47 @@ public VaultChangeWatcher(IConfigurationRoot configurationRoot, ILogger? logger
34
34
35
35
this . _logger = logger ;
36
36
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 ( ) ! ;
39
38
}
40
39
41
40
/// <inheritdoc />
42
41
protected override async Task ExecuteAsync ( CancellationToken stoppingToken )
43
42
{
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 )
45
47
{
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 ++ ;
49
52
}
50
53
51
- int waitForSec = this . _configProvider . ConfigurationSource . Options . ReloadCheckIntervalSeconds ;
54
+ this . _logger ? . LogInformation ( $ "VaultChangeWatcher will use { minTime } seconds interval" ) ;
52
55
53
56
while ( ! stoppingToken . IsCancellationRequested )
54
57
{
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 ) ;
58
59
if ( stoppingToken . IsCancellationRequested )
59
60
{
60
61
break ;
61
62
}
62
63
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
+ }
66
78
}
67
79
}
68
80
}
0 commit comments