diff --git a/src/Winton.Extensions.Configuration.Consul/ConsulConfigurationProvider.cs b/src/Winton.Extensions.Configuration.Consul/ConsulConfigurationProvider.cs index 9fa3dfb..373536d 100644 --- a/src/Winton.Extensions.Configuration.Consul/ConsulConfigurationProvider.cs +++ b/src/Winton.Extensions.Configuration.Consul/ConsulConfigurationProvider.cs @@ -41,6 +41,16 @@ public ConsulConfigurationProvider( _source = source; _consulClientFactory = consulClientFactory; _cancellationTokenSource = new CancellationTokenSource(); + if (_source.WatchCancellationTokenSource != null) + { + _source.WatchCancellationTokenSource.Token.Register(() => + { + if (!_disposed && !_cancellationTokenSource.IsCancellationRequested) + { + _cancellationTokenSource.Cancel(); + } + }); + } } public void Dispose() @@ -143,7 +153,7 @@ private async Task PollingLoop(CancellationToken cancellationToken) SetLastIndex(result); consecutiveFailureCount = 0; } - catch (Exception exception) + catch (Exception exception) when (!cancellationToken.IsCancellationRequested) { var wait = _source.OnWatchException?.Invoke( diff --git a/src/Winton.Extensions.Configuration.Consul/ConsulConfigurationSource.cs b/src/Winton.Extensions.Configuration.Consul/ConsulConfigurationSource.cs index 7d9c43c..1cdd7d1 100644 --- a/src/Winton.Extensions.Configuration.Consul/ConsulConfigurationSource.cs +++ b/src/Winton.Extensions.Configuration.Consul/ConsulConfigurationSource.cs @@ -4,6 +4,7 @@ using System; using System.Collections.Generic; using System.Net.Http; +using System.Threading; using Consul; using Microsoft.Extensions.Configuration; using Winton.Extensions.Configuration.Consul.Extensions; @@ -47,6 +48,8 @@ public string KeyToRemove public Func? OnWatchException { get; set; } + public CancellationTokenSource? WatchCancellationTokenSource { get; set; } + public bool Optional { get; set; } = false; public IConfigurationParser Parser { get; set; } diff --git a/src/Winton.Extensions.Configuration.Consul/IConsulConfigurationSource.cs b/src/Winton.Extensions.Configuration.Consul/IConsulConfigurationSource.cs index 98017a6..0097f24 100644 --- a/src/Winton.Extensions.Configuration.Consul/IConsulConfigurationSource.cs +++ b/src/Winton.Extensions.Configuration.Consul/IConsulConfigurationSource.cs @@ -81,6 +81,16 @@ public interface IConsulConfigurationSource : IConfigurationSource /// Func? OnWatchException { get; set; } + /// + /// Gets or sets a which can be used to cancel the background task that watches Consul for changes to this config. + /// + /// + /// Cancelling the watch task by calling Cancel() on this CancellationTokenSource will completely terminate the watching process and it is not possible to restart it after this. + /// By default this configuration provider will terminate the watch task itself when the ConsulConfigurationProvider is disposed. + /// 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. + /// + CancellationTokenSource? WatchCancellationTokenSource { get; set; } + /// /// Gets or sets a value indicating whether the config is optional. ///