Skip to content

Commit ac0d0a4

Browse files
authored
Add DotSettings file and run R# code cleanup. (#29)
1 parent 630d445 commit ac0d0a4

25 files changed

Lines changed: 943 additions & 440 deletions

Winton.Extensions.Configuration.Consul.sln.DotSettings

Lines changed: 483 additions & 0 deletions
Large diffs are not rendered by default.

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ internal sealed class ConfigQueryResult : IConfigQueryResult
1111
public ConfigQueryResult(QueryResult<KVPair> kvPairQueryResult)
1212
{
1313
Exists = kvPairQueryResult?.StatusCode != HttpStatusCode.NotFound
14-
&& kvPairQueryResult?.Response?.Value != null
15-
&& kvPairQueryResult?.Response?.Value.Length != 0;
14+
&& kvPairQueryResult?.Response?.Value != null
15+
&& kvPairQueryResult.Response?.Value.Length != 0;
1616
Value = kvPairQueryResult?.Response?.Value;
1717
}
1818

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

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,34 +8,47 @@
88
namespace Winton.Extensions.Configuration.Consul
99
{
1010
/// <summary>
11-
/// Extensions for the <see cref="IConfigurationBuilder"/> that provide syntactic sugar for
12-
/// using the <see cref="IConsulConfigurationSource"/>.
11+
/// Extensions for the <see cref="IConfigurationBuilder" /> that provide syntactic sugar for
12+
/// using the <see cref="IConsulConfigurationSource" />.
1313
/// </summary>
1414
public static class ConfigurationBuilderExtensions
1515
{
1616
/// <summary>
17-
/// Adds Consul as a configuration source to the <see cref="IConfigurationBuilder"/>
18-
/// using the default settings in <see cref="IConsulConfigurationSource"/>.
17+
/// Adds Consul as a configuration source to the <see cref="IConfigurationBuilder" />
18+
/// using the default settings in <see cref="IConsulConfigurationSource" />.
1919
/// </summary>
2020
/// <param name="builder">The builder to add consul to.</param>
2121
/// <param name="key">The key in consul where the configuration is located.</param>
22-
/// <param name="cancellationToken">The <see cref="CancellationToken"/> used to cancel any open Consul connections or watchers.</param>
22+
/// <param name="cancellationToken">
23+
/// The <see cref="CancellationToken" /> used to cancel any open Consul connections or
24+
/// watchers.
25+
/// </param>
2326
/// <returns>The builder</returns>
24-
public static IConfigurationBuilder AddConsul(this IConfigurationBuilder builder, string key, CancellationToken cancellationToken)
27+
public static IConfigurationBuilder AddConsul(
28+
this IConfigurationBuilder builder,
29+
string key,
30+
CancellationToken cancellationToken)
2531
{
2632
return builder.AddConsul(key, cancellationToken, options => { });
2733
}
2834

2935
/// <summary>
30-
/// Adds Consul as a configuration source to the <see cref="IConfigurationBuilder"/>
31-
/// and applies the given overrides to the <see cref="IConsulConfigurationSource"/>.
36+
/// Adds Consul as a configuration source to the <see cref="IConfigurationBuilder" />
37+
/// and applies the given overrides to the <see cref="IConsulConfigurationSource" />.
3238
/// </summary>
3339
/// <param name="builder">The builder to add consul to</param>
3440
/// <param name="key">The key in consul where the configuration is located</param>
35-
/// <param name="cancellationToken">The <see cref="CancellationToken"/> used to cancel any open Consul connections or watchers.</param>
36-
/// <param name="options">An action used to configure the options of the <see cref="IConsulConfigurationSource"/></param>
41+
/// <param name="cancellationToken">
42+
/// The <see cref="CancellationToken" /> used to cancel any open Consul connections or
43+
/// watchers.
44+
/// </param>
45+
/// <param name="options">An action used to configure the options of the <see cref="IConsulConfigurationSource" /></param>
3746
/// <returns>The builder</returns>
38-
public static IConfigurationBuilder AddConsul(this IConfigurationBuilder builder, string key, CancellationToken cancellationToken, Action<IConsulConfigurationSource> options)
47+
public static IConfigurationBuilder AddConsul(
48+
this IConfigurationBuilder builder,
49+
string key,
50+
CancellationToken cancellationToken,
51+
Action<IConsulConfigurationSource> options)
3952
{
4053
var consulConfigSource = new ConsulConfigurationSource(key, cancellationToken);
4154
options(consulConfigSource);

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

Lines changed: 27 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ internal sealed class ConsulConfigurationClient : IConsulConfigurationClient
1717
private readonly object _lastIndexLock = new object();
1818
private readonly IConsulConfigurationSource _source;
1919

20-
private ConfigurationReloadToken _reloadToken = new ConfigurationReloadToken();
2120
private ulong _lastIndex;
21+
private ConfigurationReloadToken _reloadToken = new ConfigurationReloadToken();
2222

2323
public ConsulConfigurationClient(IConsulClientFactory consulClientFactory, IConsulConfigurationSource source)
2424
{
@@ -28,7 +28,7 @@ public ConsulConfigurationClient(IConsulClientFactory consulClientFactory, ICons
2828

2929
public async Task<IConfigQueryResult> GetConfig()
3030
{
31-
var result = await GetKVPair().ConfigureAwait(false);
31+
QueryResult<KVPair> result = await GetKvPair().ConfigureAwait(false);
3232
UpdateLastIndex(result);
3333
return new ConfigQueryResult(result);
3434
}
@@ -39,23 +39,40 @@ public IChangeToken Watch(Action<ConsulWatchExceptionContext> onException)
3939
return _reloadToken;
4040
}
4141

42-
private async Task<QueryResult<KVPair>> GetKVPair(QueryOptions queryOptions = null)
42+
private async Task<QueryResult<KVPair>> GetKvPair(QueryOptions queryOptions = null)
4343
{
4444
using (IConsulClient consulClient = _consulClientFactory.Create())
4545
{
46-
QueryResult<KVPair> result = await consulClient.KV.Get(_source.Key, queryOptions, _source.CancellationToken)
47-
.ConfigureAwait(false);
46+
QueryResult<KVPair> result =
47+
await consulClient
48+
.KV
49+
.Get(_source.Key, queryOptions, _source.CancellationToken)
50+
.ConfigureAwait(false);
51+
4852
switch (result.StatusCode)
4953
{
5054
case HttpStatusCode.OK:
5155
case HttpStatusCode.NotFound:
5256
return result;
5357
default:
54-
throw new Exception($"Error loading configuration from consul. Status code: {result.StatusCode}.");
58+
throw new Exception(
59+
$"Error loading configuration from consul. Status code: {result.StatusCode}.");
5560
}
5661
}
5762
}
5863

64+
private async Task<bool> HasValueChanged()
65+
{
66+
QueryOptions queryOptions;
67+
lock (_lastIndexLock)
68+
{
69+
queryOptions = new QueryOptions { WaitIndex = _lastIndex };
70+
}
71+
72+
QueryResult<KVPair> result = await GetKvPair(queryOptions).ConfigureAwait(false);
73+
return result != null && UpdateLastIndex(result);
74+
}
75+
5976
private async Task PollForChanges(Action<ConsulWatchExceptionContext> onException)
6077
{
6178
while (!_source.CancellationToken.IsCancellationRequested)
@@ -64,7 +81,9 @@ private async Task PollForChanges(Action<ConsulWatchExceptionContext> onExceptio
6481
{
6582
if (await HasValueChanged().ConfigureAwait(false))
6683
{
67-
var previousToken = Interlocked.Exchange(ref _reloadToken, new ConfigurationReloadToken());
84+
ConfigurationReloadToken previousToken = Interlocked.Exchange(
85+
ref _reloadToken,
86+
new ConfigurationReloadToken());
6887
previousToken.OnReload();
6988
return;
7089
}
@@ -77,19 +96,7 @@ private async Task PollForChanges(Action<ConsulWatchExceptionContext> onExceptio
7796
}
7897
}
7998

80-
private async Task<bool> HasValueChanged()
81-
{
82-
QueryOptions queryOptions;
83-
lock (_lastIndexLock)
84-
{
85-
queryOptions = new QueryOptions { WaitIndex = _lastIndex };
86-
}
87-
88-
var result = await GetKVPair(queryOptions).ConfigureAwait(false);
89-
return result != null && UpdateLastIndex(result);
90-
}
91-
92-
private bool UpdateLastIndex(QueryResult<KVPair> queryResult)
99+
private bool UpdateLastIndex(QueryResult queryResult)
93100
{
94101
lock (_lastIndexLock)
95102
{

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

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@ internal sealed class ConsulConfigurationProvider : ConfigurationProvider
1515
private readonly IConsulConfigurationClient _consulConfigClient;
1616
private readonly IConsulConfigurationSource _source;
1717

18-
public ConsulConfigurationProvider(IConsulConfigurationSource source, IConsulConfigurationClient consulConfigClient)
18+
public ConsulConfigurationProvider(
19+
IConsulConfigurationSource source,
20+
IConsulConfigurationClient consulConfigClient)
1921
{
2022
if (source.Parser == null)
2123
{
@@ -31,7 +33,7 @@ public ConsulConfigurationProvider(IConsulConfigurationSource source, IConsulCon
3133
() => _consulConfigClient.Watch(_source.OnWatchException),
3234
async () =>
3335
{
34-
await DoLoad(reloading: true).ConfigureAwait(false);
36+
await DoLoad(true).ConfigureAwait(false);
3537
OnReload();
3638
});
3739
}
@@ -41,7 +43,7 @@ public override void Load()
4143
{
4244
try
4345
{
44-
DoLoad(reloading: false).Wait();
46+
DoLoad(false).Wait();
4547
}
4648
catch (AggregateException aggregateException)
4749
{
@@ -58,13 +60,12 @@ private async Task DoLoad(bool reloading)
5860
{
5961
if (!reloading)
6062
{
61-
throw new Exception($"The configuration for key {_source.Key} was not found and is not optional.");
62-
}
63-
else
64-
{
65-
// Don't overwrite mandatory config with empty data if not found when reloading
66-
return;
63+
throw new Exception(
64+
$"The configuration for key {_source.Key} was not found and is not optional.");
6765
}
66+
67+
// Don't overwrite mandatory config with empty data if not found when reloading
68+
return;
6869
}
6970

7071
LoadIntoMemory(configQueryResult);
@@ -75,12 +76,21 @@ private async Task DoLoad(bool reloading)
7576
}
7677
}
7778

79+
private void HandleLoadException(Exception exception)
80+
{
81+
var exceptionContext = new ConsulLoadExceptionContext(_source, exception);
82+
_source.OnLoadException?.Invoke(exceptionContext);
83+
if (!exceptionContext.Ignore)
84+
{
85+
throw exception;
86+
}
87+
}
88+
7889
private void LoadIntoMemory(IConfigQueryResult configQueryResult)
7990
{
8091
if (!configQueryResult.Exists)
8192
{
8293
Data = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
83-
return;
8494
}
8595
else
8696
{
@@ -91,15 +101,5 @@ private void LoadIntoMemory(IConfigQueryResult configQueryResult)
91101
}
92102
}
93103
}
94-
95-
private void HandleLoadException(Exception exception)
96-
{
97-
var exceptionContext = new ConsulLoadExceptionContext(_source, exception);
98-
_source.OnLoadException?.Invoke(exceptionContext);
99-
if (!exceptionContext.Ignore)
100-
{
101-
throw exception;
102-
}
103-
}
104104
}
105105
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,10 @@ public ConsulConfigurationSource(string key, CancellationToken cancellationToken
2929

3030
public Action<ConsulClientConfiguration> ConsulConfigurationOptions { get; set; }
3131

32-
public Action<HttpClient> ConsulHttpClientOptions { get; set; }
33-
3432
public Action<HttpClientHandler> ConsulHttpClientHandlerOptions { get; set; }
3533

34+
public Action<HttpClient> ConsulHttpClientOptions { get; set; }
35+
3636
public string Key { get; }
3737

3838
public Action<ConsulLoadExceptionContext> OnLoadException { get; set; }

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
namespace Winton.Extensions.Configuration.Consul
77
{
88
/// <summary>
9-
/// Contains information about exceptions that occur during a configuration load from Consul.
9+
/// Contains information about exceptions that occur during a configuration load from Consul.
1010
/// </summary>
1111
public sealed class ConsulLoadExceptionContext
1212
{
@@ -17,19 +17,19 @@ internal ConsulLoadExceptionContext(IConsulConfigurationSource source, Exception
1717
}
1818

1919
/// <summary>
20-
/// Gets the <see cref="Exception"/> that occured in Load.
20+
/// Gets the <see cref="Exception" /> that occured in Load.
2121
/// </summary>
2222
public Exception Exception { get; }
2323

2424
/// <summary>
25-
/// Gets or sets a value indicating whether the exception should be ignored.
26-
/// Set to true to prevent the exception from being thrown.
27-
/// I.e. if the exception has been handled.
25+
/// Gets or sets a value indicating whether the exception should be ignored.
26+
/// Set to true to prevent the exception from being thrown.
27+
/// I.e. if the exception has been handled.
2828
/// </summary>
2929
public bool Ignore { get; set; }
3030

3131
/// <summary>
32-
/// Gets the <see cref="IConsulConfigurationSource"/> of the provider that caused the exception.
32+
/// Gets the <see cref="IConsulConfigurationSource" /> of the provider that caused the exception.
3333
/// </summary>
3434
public IConsulConfigurationSource Source { get; }
3535
}

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
namespace Winton.Extensions.Configuration.Consul
88
{
99
/// <summary>
10-
/// Contains information about a consul load exception.
10+
/// Contains information about a consul load exception.
1111
/// </summary>
1212
public sealed class ConsulWatchExceptionContext
1313
{
@@ -18,13 +18,13 @@ internal ConsulWatchExceptionContext(IConsulConfigurationSource source, Exceptio
1818
}
1919

2020
/// <summary>
21-
/// Gets the <see cref="Exception"/> that occured.
21+
/// Gets the <see cref="Exception" /> that occured.
2222
/// </summary>
2323
public Exception Exception { get; }
2424

2525
/// <summary>
26-
/// Gets the <see cref="IConsulConfigurationSource"/> of the provider that caused the exception.
27-
/// Can be used to access the <see cref="CancellationToken"/> which can terminate the watcher.
26+
/// Gets the <see cref="IConsulConfigurationSource" /> of the provider that caused the exception.
27+
/// Can be used to access the <see cref="CancellationToken" /> which can terminate the watcher.
2828
/// </summary>
2929
public IConsulConfigurationSource Source { get; }
3030
}

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@
55

66
namespace Winton.Extensions.Configuration.Consul
77
{
8-
/// <summary>A factory responsible for creating <see cref="IConsulClient"/> objects.</summary>
8+
/// <summary>A factory responsible for creating <see cref="IConsulClient" /> objects.</summary>
99
internal interface IConsulClientFactory
1010
{
11-
/// <summary>Creates a new instance of an <see cref="IConsulClient"/>.</summary>
12-
/// <returns>A new <see cref="IConsulClient"/>.</returns>
11+
/// <summary>Creates a new instance of an <see cref="IConsulClient" />.</summary>
12+
/// <returns>A new <see cref="IConsulClient" />.</returns>
1313
IConsulClient Create();
1414
}
1515
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ internal interface IConsulConfigurationClient
1616

1717
/// <summary>Watches the config for changes.</summary>
1818
/// <param name="onException">An action to be invoked if an exception occurs during the watch.</param>
19-
/// <returns>An <see cref="IChangeToken"/> that will indicated when changes have occured.</returns>
19+
/// <returns>An <see cref="IChangeToken" /> that will indicated when changes have occured.</returns>
2020
IChangeToken Watch(Action<ConsulWatchExceptionContext> onException);
2121
}
2222
}

0 commit comments

Comments
 (0)