Skip to content

Commit 7f59d4c

Browse files
authored
refactor: Refactor Init and Shutdown (#416)
Signed-off-by: André Silva <[email protected]>
1 parent 8067fd1 commit 7f59d4c

File tree

3 files changed

+38
-48
lines changed

3 files changed

+38
-48
lines changed

src/OpenFeature.Contrib.Providers.Flagd/FlagdProvider.cs

Lines changed: 4 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,6 @@ public FlagdProvider(FlagdConfig config)
8080
internal FlagdProvider(Resolver.Resolver resolver)
8181
{
8282
_resolver = resolver;
83-
_resolver.Init();
8483
}
8584

8685
// just for testing, internal but visible in tests
@@ -105,27 +104,15 @@ public static string GetProviderName()
105104
internal Resolver.Resolver GetResolver() => _resolver;
106105

107106
/// <inheritdoc/>
108-
public override Task InitializeAsync(EvaluationContext context, CancellationToken cancellationToken = default)
107+
public override async Task InitializeAsync(EvaluationContext context, CancellationToken cancellationToken = default)
109108
{
110-
return Task.Run(async () =>
111-
{
112-
await _resolver.Init().ConfigureAwait(false);
113-
}).ContinueWith((t) =>
114-
{
115-
if (t.IsFaulted)
116-
{
117-
throw t.Exception;
118-
}
119-
});
109+
await _resolver.Init().ConfigureAwait(false);
120110
}
121111

122112
/// <inheritdoc/>
123-
public override Task ShutdownAsync(CancellationToken cancellationToken = default)
113+
public override async Task ShutdownAsync(CancellationToken cancellationToken = default)
124114
{
125-
return _resolver.Shutdown().ContinueWith((t) =>
126-
{
127-
if (t.IsFaulted) throw t.Exception;
128-
});
115+
await _resolver.Shutdown().ConfigureAwait(false);
129116
}
130117

131118
/// <inheritdoc/>

src/OpenFeature.Contrib.Providers.Flagd/Resolver/InProcess/InProcessResolver.cs

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@ internal class InProcessResolver : Resolver
3333
private readonly Mutex _mtx;
3434
private int _eventStreamRetryBackoff = InitialEventStreamRetryBaseBackoff;
3535
private readonly FlagdConfig _config;
36-
private Thread _handleEventsThread;
3736
private GrpcChannel _channel;
3837
private Channel<object> _eventChannel;
3938
private Model.Metadata _providerMetadata;
@@ -66,29 +65,29 @@ public async Task Init()
6665
{
6766
await _jsonSchemaValidator.InitializeAsync().ConfigureAwait(false);
6867

69-
await Task.Run(() =>
68+
var latch = new CountdownEvent(1);
69+
var handleEventsThread = new Thread(async () => await HandleEvents(latch).ConfigureAwait(false))
7070
{
71-
var latch = new CountdownEvent(1);
72-
_handleEventsThread = new Thread(async () => await HandleEvents(latch).ConfigureAwait(false))
73-
{
74-
IsBackground = true
75-
};
76-
_handleEventsThread.Start();
77-
latch.Wait();
78-
}).ContinueWith((task) =>
79-
{
80-
if (task.IsFaulted) throw task.Exception;
81-
}).ConfigureAwait(false);
71+
IsBackground = true
72+
};
73+
handleEventsThread.Start();
74+
await Task.Run(() => latch.Wait()).ConfigureAwait(false);
8275
}
8376

84-
public Task Shutdown()
77+
public async Task Shutdown()
8578
{
8679
_cancellationTokenSource.Cancel();
87-
return _channel?.ShutdownAsync().ContinueWith((t) =>
80+
try
8881
{
89-
_channel.Dispose();
90-
if (t.IsFaulted) throw t.Exception;
91-
});
82+
if (_channel != null)
83+
{
84+
await _channel.ShutdownAsync().ConfigureAwait(false);
85+
}
86+
}
87+
finally
88+
{
89+
_channel?.Dispose();
90+
}
9291
}
9392

9493
public Task<ResolutionDetails<bool>> ResolveBooleanValueAsync(string flagKey, bool defaultValue, EvaluationContext context = null)
@@ -205,7 +204,8 @@ private T BuildClient<T>(FlagdConfig config, Func<GrpcChannel, T> constructorFun
205204
var certificate = CertificateLoader.LoadCertificate(config.CertificatePath);
206205

207206
#if NET8_0_OR_GREATER
208-
handler.ServerCertificateCustomValidationCallback = (message, cert, chain, _) => {
207+
handler.ServerCertificateCustomValidationCallback = (message, cert, chain, _) =>
208+
{
209209
// the the custom cert to the chain, Build returns a bool if valid.
210210
chain.ChainPolicy.TrustMode = X509ChainTrustMode.CustomRootTrust;
211211
chain.ChainPolicy.CustomTrustStore.Add(certificate);
@@ -284,7 +284,8 @@ private FlagSyncService.FlagSyncServiceClient BuildClientForPlatform(FlagdConfig
284284
{
285285
var certificate = CertificateLoader.LoadCertificate(config.CertificatePath);
286286
#if NET5_0_OR_GREATER
287-
handler.ServerCertificateCustomValidationCallback = (message, cert, chain, _) => {
287+
handler.ServerCertificateCustomValidationCallback = (message, cert, chain, _) =>
288+
{
288289
// the the custom cert to the chain, Build returns a bool if valid.
289290
chain.ChainPolicy.TrustMode = X509ChainTrustMode.CustomRootTrust;
290291
chain.ChainPolicy.CustomTrustStore.Add(certificate);

src/OpenFeature.Contrib.Providers.Flagd/Resolver/Rpc/RpcResolver.cs

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ internal class RpcResolver : Resolver
3030
private GrpcChannel _channel;
3131
private Channel<object> _eventChannel;
3232
private Model.Metadata _providerMetadata;
33-
private Thread _handleEventsThread;
3433

3534
internal RpcResolver(FlagdConfig config, Channel<object> eventChannel, Model.Metadata providerMetadata)
3635
{
@@ -59,19 +58,21 @@ internal RpcResolver(Service.ServiceClient client, FlagdConfig config, ICache<st
5958

6059
public Task Init()
6160
{
62-
_handleEventsThread = new Thread(HandleEvents);
63-
_handleEventsThread.Start();
64-
return Task.CompletedTask; // TODO: an elegant way of testing the connection status before completing this task
61+
_ = Task.Run(this.HandleEvents);
62+
return Task.CompletedTask;
6563
}
6664

67-
public Task Shutdown()
65+
public async Task Shutdown()
6866
{
6967
_cancellationTokenSource.Cancel();
70-
return _channel?.ShutdownAsync().ContinueWith((t) =>
68+
try
7169
{
72-
_channel.Dispose();
73-
if (t.IsFaulted) throw t.Exception;
74-
});
70+
await _channel.ShutdownAsync().ConfigureAwait(false);
71+
}
72+
finally
73+
{
74+
_channel?.Dispose();
75+
}
7576
}
7677

7778
public async Task<ResolutionDetails<bool>> ResolveBooleanValueAsync(string flagKey, bool defaultValue, EvaluationContext context = null)
@@ -197,7 +198,7 @@ private async Task<ResolutionDetails<T>> ResolveValue<T>(string flagKey, Func<St
197198
}
198199
}
199200

200-
private async void HandleEvents()
201+
private async Task HandleEvents()
201202
{
202203
CancellationToken token = _cancellationTokenSource.Token;
203204
while (!token.IsCancellationRequested && _eventStreamRetries < _config.MaxEventStreamRetries)
@@ -452,7 +453,8 @@ private Service.ServiceClient BuildClientForPlatform(FlagdConfig config)
452453
{
453454
var certificate = CertificateLoader.LoadCertificate(config.CertificatePath);
454455
#if NET5_0_OR_GREATER
455-
handler.ServerCertificateCustomValidationCallback = (message, cert, chain, _) => {
456+
handler.ServerCertificateCustomValidationCallback = (message, cert, chain, _) =>
457+
{
456458
// the the custom cert to the chain, Build returns a bool if valid.
457459
chain.ChainPolicy.TrustMode = System.Security.Cryptography.X509Certificates.X509ChainTrustMode.CustomRootTrust;
458460
chain.ChainPolicy.CustomTrustStore.Add(certificate);

0 commit comments

Comments
 (0)