@@ -9,8 +9,7 @@ internal sealed class DefaultRequestExecutorOptionsMonitor
9
9
private readonly SemaphoreSlim _semaphore = new ( 1 , 1 ) ;
10
10
private readonly IOptionsMonitor < RequestExecutorSetup > _optionsMonitor ;
11
11
private readonly IRequestExecutorOptionsProvider [ ] _optionsProviders ;
12
- private readonly Dictionary < string , List < IConfigureRequestExecutorSetup > > _configs =
13
- new ( ) ;
12
+ private readonly Dictionary < string , List < IConfigureRequestExecutorSetup > > _configs = new ( ) ;
14
13
private readonly List < IDisposable > _disposables = [ ] ;
15
14
private readonly List < Action < string > > _listeners = [ ] ;
16
15
private bool _initialized ;
@@ -28,7 +27,7 @@ public async ValueTask<RequestExecutorSetup> GetAsync(
28
27
string schemaName ,
29
28
CancellationToken cancellationToken = default )
30
29
{
31
- await InitializeAsync ( cancellationToken ) . ConfigureAwait ( false ) ;
30
+ await TryInitializeAsync ( cancellationToken ) . ConfigureAwait ( false ) ;
32
31
33
32
var options = new RequestExecutorSetup ( ) ;
34
33
_optionsMonitor . Get ( schemaName ) . CopyTo ( options ) ;
@@ -44,47 +43,57 @@ public async ValueTask<RequestExecutorSetup> GetAsync(
44
43
return options ;
45
44
}
46
45
47
- private async ValueTask InitializeAsync ( CancellationToken cancellationToken )
46
+ private async ValueTask TryInitializeAsync ( CancellationToken cancellationToken )
48
47
{
49
48
if ( ! _initialized )
50
49
{
51
50
await _semaphore . WaitAsync ( cancellationToken ) . ConfigureAwait ( false ) ;
52
51
53
- if ( ! _initialized )
52
+ try
54
53
{
55
- _configs . Clear ( ) ;
54
+ await TryInitializeUnsafeAsync ( cancellationToken ) . ConfigureAwait ( false ) ;
55
+ }
56
+ finally
57
+ {
58
+ _semaphore . Release ( ) ;
59
+ }
60
+ }
61
+ }
56
62
57
- foreach ( var provider in _optionsProviders )
58
- {
59
- _disposables . Add ( provider . OnChange ( OnChange ) ) ;
63
+ private async ValueTask TryInitializeUnsafeAsync ( CancellationToken cancellationToken )
64
+ {
65
+ if ( ! _initialized )
66
+ {
67
+ _configs . Clear ( ) ;
68
+
69
+ foreach ( var provider in _optionsProviders )
70
+ {
71
+ _disposables . Add ( provider . OnChange ( OnChange ) ) ;
60
72
61
- var allConfigurations =
62
- await provider . GetOptionsAsync ( cancellationToken )
63
- . ConfigureAwait ( false ) ;
73
+ var allConfigurations =
74
+ await provider . GetOptionsAsync ( cancellationToken )
75
+ . ConfigureAwait ( false ) ;
64
76
65
- foreach ( var configuration in allConfigurations )
77
+ foreach ( var configuration in allConfigurations )
78
+ {
79
+ if ( ! _configs . TryGetValue (
80
+ configuration . SchemaName ,
81
+ out var configurations ) )
66
82
{
67
- if ( ! _configs . TryGetValue (
68
- configuration . SchemaName ,
69
- out var configurations ) )
70
- {
71
- configurations = [ ] ;
72
- _configs . Add ( configuration . SchemaName , configurations ) ;
73
- }
74
-
75
- configurations . Add ( configuration ) ;
83
+ configurations = [ ] ;
84
+ _configs . Add ( configuration . SchemaName , configurations ) ;
76
85
}
77
- }
78
86
79
- _initialized = true ;
87
+ configurations . Add ( configuration ) ;
88
+ }
80
89
}
81
90
82
- _semaphore . Release ( ) ;
91
+ _initialized = true ;
83
92
}
84
93
}
85
94
86
- public IDisposable OnChange ( Action < string > listener ) =>
87
- new Session ( this , listener ) ;
95
+ public IDisposable OnChange ( Action < string > listener )
96
+ => new Session ( this , listener ) ;
88
97
89
98
private void OnChange ( IConfigureRequestExecutorSetup changes )
90
99
{
0 commit comments