Skip to content

Commit bf77bb3

Browse files
authored
Added WatchCancellationTokenSource (#135)
1 parent 23bb0c3 commit bf77bb3

File tree

3 files changed

+24
-1
lines changed

3 files changed

+24
-1
lines changed

src/Winton.Extensions.Configuration.Consul/ConsulConfigurationProvider.cs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,16 @@ public ConsulConfigurationProvider(
4141
_source = source;
4242
_consulClientFactory = consulClientFactory;
4343
_cancellationTokenSource = new CancellationTokenSource();
44+
if (_source.WatchCancellationTokenSource != null)
45+
{
46+
_source.WatchCancellationTokenSource.Token.Register(() =>
47+
{
48+
if (!_disposed && !_cancellationTokenSource.IsCancellationRequested)
49+
{
50+
_cancellationTokenSource.Cancel();
51+
}
52+
});
53+
}
4454
}
4555

4656
public void Dispose()
@@ -143,7 +153,7 @@ private async Task PollingLoop(CancellationToken cancellationToken)
143153
SetLastIndex(result);
144154
consecutiveFailureCount = 0;
145155
}
146-
catch (Exception exception)
156+
catch (Exception exception) when (!cancellationToken.IsCancellationRequested)
147157
{
148158
var wait =
149159
_source.OnWatchException?.Invoke(

src/Winton.Extensions.Configuration.Consul/ConsulConfigurationSource.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using System;
55
using System.Collections.Generic;
66
using System.Net.Http;
7+
using System.Threading;
78
using Consul;
89
using Microsoft.Extensions.Configuration;
910
using Winton.Extensions.Configuration.Consul.Extensions;
@@ -47,6 +48,8 @@ public string KeyToRemove
4748

4849
public Func<ConsulWatchExceptionContext, TimeSpan>? OnWatchException { get; set; }
4950

51+
public CancellationTokenSource? WatchCancellationTokenSource { get; set; }
52+
5053
public bool Optional { get; set; } = false;
5154

5255
public IConfigurationParser Parser { get; set; }

src/Winton.Extensions.Configuration.Consul/IConsulConfigurationSource.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,16 @@ public interface IConsulConfigurationSource : IConfigurationSource
8181
/// </remarks>
8282
Func<ConsulWatchExceptionContext, TimeSpan>? OnWatchException { get; set; }
8383

84+
/// <summary>
85+
/// Gets or sets a <see cref="CancellationTokenSource" /> which can be used to cancel the background task that watches Consul for changes to this config.
86+
/// </summary>
87+
/// <remarks>
88+
/// Cancelling the watch task by calling <c>Cancel()</c> on this <c>CancellationTokenSource</c> will completely terminate the watching process and it is not possible to restart it after this.
89+
/// By default this configuration provider will terminate the watch task itself when the <c>ConsulConfigurationProvider</c> is disposed.
90+
/// This functionality is only useful if you need to terminate the watch task early in response to some other event or want explicit control over when it is cancelled for some other reasons.
91+
/// </remarks>
92+
CancellationTokenSource? WatchCancellationTokenSource { get; set; }
93+
8494
/// <summary>
8595
/// Gets or sets a value indicating whether the config is optional.
8696
/// </summary>

0 commit comments

Comments
 (0)