Skip to content

Commit 5514d04

Browse files
committed
[add] full dispose pattern
[fix] SRP in methods [add] false warnings skip
1 parent 8ad613b commit 5514d04

File tree

1 file changed

+51
-13
lines changed

1 file changed

+51
-13
lines changed

src/Etcd.Microsoft.Extensions.Configuration/Client/EtcdKeyValueClient.cs

Lines changed: 51 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ public class EtcdKeyValueClient : IEtcdKeyValueClient
3131
private string? _userName;
3232
private string? _token;
3333

34+
private bool _isDisposed;
35+
3436
/// <summary>
3537
/// Initializes a new instance of the <see cref="EtcdKeyValueClient" /> class.
3638
/// </summary>
@@ -41,6 +43,7 @@ public class EtcdKeyValueClient : IEtcdKeyValueClient
4143
/// <exception cref="ArgumentNullException">clientFactory
4244
/// or
4345
/// credentials</exception>
46+
// ReSharper disable once TooManyDependencies
4447
public EtcdKeyValueClient(IEtcdClientFactory clientFactory, ICredentials credentials, bool enableWatch = true, bool unwatchOnDispose = true)
4548
{
4649
if (clientFactory == null) throw new ArgumentNullException(nameof(clientFactory));
@@ -57,7 +60,7 @@ public EtcdKeyValueClient(IEtcdClientFactory clientFactory, ICredentials credent
5760
/// <summary>
5861
/// Finalizes an instance of the <see cref="EtcdKeyValueClient"/> class.
5962
/// </summary>
60-
~EtcdKeyValueClient() => Dispose();
63+
~EtcdKeyValueClient() => Dispose(false);
6164

6265
/// <summary>
6366
/// Occurs when watch callback is received.
@@ -82,24 +85,17 @@ public EtcdKeyValueClient(IEtcdClientFactory clientFactory, ICredentials credent
8285
try
8386
{
8487
var roles = GetRoles();
85-
var permissions = new List<Permission>();
86-
var keys = new List<Mvccpb.KeyValue>();
87-
88-
foreach (var role in roles)
89-
permissions.AddRange(GetPermissions(role));
90-
91-
foreach (var permission in permissions)
92-
keys.AddRange(GetKeys(permission.Key.ToStringUtf8()));
88+
var permissions = GetAllPermissions(roles);
89+
var keys = GetAllKeys(permissions);
9390

9491
if (_enableWatch)
95-
foreach (var item in keys)
96-
Watch(item.Key.ToStringUtf8());
92+
WatchAll(keys);
9793

9894
return Convert.ToDictionary(keys);
9995
}
10096
catch (RpcException e)
10197
{
102-
throw new EtcdException($"Error reading all keys from etcd provider", e);
98+
throw new EtcdException("Error reading all keys from etcd provider", e);
10399
}
104100
}
105101

@@ -126,10 +122,26 @@ public EtcdKeyValueClient(IEtcdClientFactory clientFactory, ICredentials credent
126122
/// </summary>
127123
public void Dispose()
128124
{
125+
Dispose(true);
126+
GC.SuppressFinalize(this);
127+
}
128+
129+
/// <summary>
130+
/// Releases unmanaged and - optionally - managed resources.
131+
/// </summary>
132+
/// <param name="disposing"><c>true</c> to release both managed and unmanaged resources; <c>false</c> to release only unmanaged resources.</param>
133+
// ReSharper disable once FlagArgument
134+
protected virtual void Dispose(bool disposing)
135+
{
136+
if (!disposing || _isDisposed)
137+
return;
138+
129139
if (_unwatchOnDispose)
130140
StopWatchAll();
131141

132-
_client?.Dispose();
142+
_client.Dispose();
143+
144+
_isDisposed = true;
133145
}
134146

135147
/// <summary>
@@ -163,6 +175,32 @@ private IEnumerable<string> GetRoles() =>
163175
Name = _userName
164176
}, GetMetadata()).Roles;
165177

178+
private IList<Permission> GetAllPermissions(IEnumerable<string> roles)
179+
{
180+
var permissions = new List<Permission>();
181+
182+
foreach (var role in roles)
183+
permissions.AddRange(GetPermissions(role));
184+
185+
return permissions;
186+
}
187+
188+
private IList<Mvccpb.KeyValue> GetAllKeys(IEnumerable<Permission> permissions)
189+
{
190+
var keys = new List<Mvccpb.KeyValue>();
191+
192+
foreach (var permission in permissions)
193+
keys.AddRange(GetKeys(permission.Key.ToStringUtf8()));
194+
195+
return keys;
196+
}
197+
198+
private void WatchAll(IList<Mvccpb.KeyValue> keys)
199+
{
200+
foreach (var item in keys)
201+
Watch(item.Key.ToStringUtf8());
202+
}
203+
166204
private IEnumerable<Permission> GetPermissions(string role) =>
167205
_client.RoleGet(new AuthRoleGetRequest
168206
{

0 commit comments

Comments
 (0)