-
Notifications
You must be signed in to change notification settings - Fork 371
Add optional parameter to configuration providers for non-blocking startup #1745
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
f17d73f
d33da80
f99ca95
6557847
96c20b8
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -26,13 +26,14 @@ namespace Dapr.Extensions.Configuration; | |
| /// </summary> | ||
| internal class DaprConfigurationStoreProvider : ConfigurationProvider, IDisposable | ||
| { | ||
| private string store; | ||
| private IReadOnlyList<string> keys; | ||
| private DaprClient daprClient; | ||
| private TimeSpan sidecarWaitTimeout; | ||
| private bool isStreaming; | ||
| private IReadOnlyDictionary<string, string>? metadata; | ||
| private CancellationTokenSource cts; | ||
| private readonly string store; | ||
| private readonly IReadOnlyList<string> keys; | ||
| private readonly DaprClient daprClient; | ||
| private readonly TimeSpan sidecarWaitTimeout; | ||
| private readonly bool isStreaming; | ||
| private readonly bool isOptional; | ||
| private readonly IReadOnlyDictionary<string, string>? metadata; | ||
| private readonly CancellationTokenSource cts; | ||
| private Task subscribeTask = Task.CompletedTask; | ||
|
|
||
| /// <summary> | ||
|
|
@@ -44,30 +45,83 @@ internal class DaprConfigurationStoreProvider : ConfigurationProvider, IDisposab | |
| /// <param name="sidecarWaitTimeout">The <see cref="TimeSpan"/> used to configure the timeout waiting for Dapr.</param> | ||
| /// <param name="isStreaming">Determines if the source is streaming or not.</param> | ||
| /// <param name="metadata">Optional metadata sent to the configuration store.</param> | ||
| /// <param name="isOptional">When true, does not block startup waiting for the sidecar.</param> | ||
| public DaprConfigurationStoreProvider( | ||
| string store, | ||
| IReadOnlyList<string> keys, | ||
| DaprClient daprClient, | ||
| TimeSpan sidecarWaitTimeout, | ||
| bool isStreaming = false, | ||
| IReadOnlyDictionary<string, string>? metadata = default) | ||
| IReadOnlyDictionary<string, string>? metadata = default, | ||
| bool isOptional = false) | ||
| { | ||
| this.store = store; | ||
| this.keys = keys; | ||
| this.daprClient = daprClient; | ||
| this.sidecarWaitTimeout = sidecarWaitTimeout; | ||
| this.isStreaming = isStreaming; | ||
| this.isOptional = isOptional; | ||
| this.metadata = metadata ?? new Dictionary<string, string>(); | ||
| this.cts = new CancellationTokenSource(); | ||
| } | ||
|
|
||
| public void Dispose() | ||
| { | ||
| cts.Cancel(); | ||
| cts.Dispose(); | ||
| } | ||
|
|
||
| /// <inheritdoc/> | ||
| public override void Load() => LoadAsync().ConfigureAwait(false).GetAwaiter().GetResult(); | ||
| public override void Load() | ||
| { | ||
| if (isOptional) | ||
| { | ||
| Data = new Dictionary<string, string?>(StringComparer.OrdinalIgnoreCase); | ||
| _ = Task.Run(() => LoadInBackgroundAsync()); | ||
| } | ||
|
Comment on lines
+75
to
+81
|
||
| else | ||
| { | ||
| LoadAsync().ConfigureAwait(false).GetAwaiter().GetResult(); | ||
| } | ||
| } | ||
|
|
||
| private async Task LoadInBackgroundAsync() | ||
| { | ||
| while (!cts.Token.IsCancellationRequested) | ||
| { | ||
| try | ||
| { | ||
| using var tokenSource = new CancellationTokenSource(sidecarWaitTimeout); | ||
| using var linked = CancellationTokenSource.CreateLinkedTokenSource(tokenSource.Token, cts.Token); | ||
| await daprClient.WaitForSidecarAsync(linked.Token); | ||
|
|
||
| await FetchDataAsync(); | ||
| OnReload(); | ||
| return; | ||
| } | ||
| catch (OperationCanceledException) when (cts.Token.IsCancellationRequested) | ||
| { | ||
| return; | ||
| } | ||
| catch (OperationCanceledException) | ||
| { | ||
| // Sidecar wait timed out — retry after delay. | ||
| } | ||
| catch (DaprException) | ||
| { | ||
| // Transient Dapr error — retry after delay. | ||
| } | ||
|
|
||
| try | ||
| { | ||
| await Task.Delay(sidecarWaitTimeout, cts.Token); | ||
| } | ||
| catch (OperationCanceledException) | ||
| { | ||
| return; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| private async Task LoadAsync() | ||
| { | ||
|
|
@@ -77,6 +131,11 @@ private async Task LoadAsync() | |
| await daprClient.WaitForSidecarAsync(tokenSource.Token); | ||
| } | ||
|
|
||
| await FetchDataAsync(); | ||
| } | ||
|
|
||
| private async Task FetchDataAsync() | ||
| { | ||
| if (isStreaming) | ||
| { | ||
| subscribeTask = Task.Run(async () => | ||
|
Comment on lines
+137
to
141
|
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Dispose()cancels and disposes the sharedCancellationTokenSource, but background tasks started viaTask.Run(and the streaming subscription task) may still be executing and attempting to register withcts.Token. Disposing the CTS while work is still running can causeObjectDisposedExceptionraces inside token registration APIs. Consider storing the background task(s) and waiting for completion after cancellation (or avoid disposing the CTS if you can’t guarantee completion).