Skip to content

Commit

Permalink
fix #186 - watch reconnects
Browse files Browse the repository at this point in the history
  • Loading branch information
shubhamranjan committed Oct 19, 2024
1 parent a0b9f7e commit 1fa396a
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 9 deletions.
24 changes: 18 additions & 6 deletions dotnet-etcd/etcdClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
using System.Collections.Generic;
using System.Globalization;
using System.Linq;

using System.Net.Http;

using dotnet_etcd.interfaces;
using dotnet_etcd.multiplexer;
Expand Down Expand Up @@ -38,6 +38,8 @@ public partial class EtcdClient : IDisposable, IEtcdClient
private const string DefaultServerName = "my-etcd-server";

private readonly Connection _connection;
private readonly GrpcChannel _channel;


// https://learn.microsoft.com/en-us/aspnet/core/grpc/retries?view=aspnetcore-6.0#configure-a-grpc-retry-policy
private static readonly MethodConfig _defaultGrpcMethodConfig = new()
Expand Down Expand Up @@ -87,6 +89,13 @@ public EtcdClient(string connectionString, int port = 2379, string serverName =
connectionString = DnsPrefix + connectionString;
}

var httpHandler = new SocketsHttpHandler
{
KeepAlivePingDelay = TimeSpan.FromSeconds(30),
KeepAlivePingTimeout = TimeSpan.FromSeconds(30),
KeepAlivePingPolicy = HttpKeepAlivePingPolicy.Always
};

// Connection Configuration
var options = new GrpcChannelOptions
{
Expand All @@ -95,16 +104,18 @@ public EtcdClient(string connectionString, int port = 2379, string serverName =
MethodConfigs = { _defaultGrpcMethodConfig },
RetryThrottling = _defaultRetryThrottlingPolicy,
LoadBalancingConfigs = { new RoundRobinConfig() },
}
},
HttpHandler = httpHandler,
DisposeHttpClient = true,
ThrowOperationCanceledOnCancellation = true,
};

configureChannelOptions?.Invoke(options);

// Channel Configuration
GrpcChannel channel = null;
if (connectionString.StartsWith(DnsPrefix, StringComparison.InvariantCultureIgnoreCase))
{
channel = GrpcChannel.ForAddress(connectionString, options);
_channel = GrpcChannel.ForAddress(connectionString, options);
}
else
{
Expand Down Expand Up @@ -132,10 +143,10 @@ public EtcdClient(string connectionString, int port = 2379, string serverName =
services.AddSingleton<ResolverFactory>(factory);
options.ServiceProvider = services.BuildServiceProvider();

channel = GrpcChannel.ForAddress($"{StaticHostsPrefix}{serverName}", options);
_channel = GrpcChannel.ForAddress($"{StaticHostsPrefix}{serverName}", options);
}

CallInvoker callInvoker = interceptors != null && interceptors.Length > 0 ? channel.Intercept(interceptors) : channel.CreateCallInvoker();
CallInvoker callInvoker = interceptors != null && interceptors.Length > 0 ? _channel.Intercept(interceptors) : _channel.CreateCallInvoker();


// Setup Connection
Expand All @@ -155,6 +166,7 @@ protected virtual void Dispose(bool disposing)
if (disposing)
{
// TODO: dispose managed state (managed objects).
_channel?.Dispose();
}

// TODO: free unmanaged resources (unmanaged objects) and override a finalizer below.
Expand Down
6 changes: 3 additions & 3 deletions dotnet-etcd/leaseClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ await WriteAsync(leaser, request, communicationTimeoutInMilliseconds, cancellati
await cancellationTokenSource.CancelAsync()
.ConfigureAwait(false);
#else
cancellationTokenSource.Cancel();
cancellationTokenSource.Cancel();
#endif
}
}
Expand All @@ -178,7 +178,7 @@ await leaser.RequestStream.CompleteAsync()
await cancellationTokenSource.CancelAsync()
.ConfigureAwait(false);
#else
cancellationTokenSource.Cancel();
cancellationTokenSource.Cancel();
#endif
}
}
Expand All @@ -204,7 +204,7 @@ await Task.Delay(keepAliveTimeout, cancellationToken)
await cancellationTokenSource.CancelAsync()
.ConfigureAwait(false);
#else
cancellationTokenSource.Cancel();
cancellationTokenSource.Cancel();
#endif
}
}
Expand Down

0 comments on commit 1fa396a

Please sign in to comment.