Skip to content

Commit

Permalink
grpcChannelOptions теперь передаются в конструктор а не генерятся вну…
Browse files Browse the repository at this point in the history
…три либы
  • Loading branch information
setood committed Jul 31, 2023
1 parent 8f74c9d commit 52764b6
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 60 deletions.
17 changes: 4 additions & 13 deletions dotnet-etcd/etcdClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,11 @@
using dotnet_etcd.interfaces;
using dotnet_etcd.multiplexer;
using Grpc.Core.Interceptors;
using Grpc.Net.Client;
using Microsoft.Extensions.Logging;

namespace dotnet_etcd
{
public class SocketsHttpHandlerOptions
{
public TimeSpan KeepAlivePingDelay { get; set; }
public TimeSpan KeepAlivePingTimeout { get; set; }
public TimeSpan ConnectTimeout { get; set; }
public bool EnableMultipleHttp2Connections { get; set; }
public bool KeepAlivePingPolicyWithActiveRequests { get; set; }
}

/// <summary>
/// Etcd client is the entrypoint for this library.
Expand All @@ -41,10 +34,8 @@ public partial class EtcdClient : IDisposable, IEtcdClient
#region Initializers

public EtcdClient(string connectionString, int port = 2379,
HttpMessageHandler handler = null, bool ssl = false,
bool useLegacyRpcExceptionForCancellation = false,
SocketsHttpHandlerOptions handlerOptions = null,
ILoggerFactory grpcLoggerFactory = null,
bool ssl = false,
GrpcChannelOptions? grpcChannelOptions = null,

Check warning on line 38 in dotnet-etcd/etcdClient.cs

View workflow job for this annotation

GitHub Actions / build-lib

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.

Check warning on line 38 in dotnet-etcd/etcdClient.cs

View workflow job for this annotation

GitHub Actions / build-lib

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.
params Interceptor[] interceptors
)
{
Expand Down Expand Up @@ -141,7 +132,7 @@ params Interceptor[] interceptors
nodes.Add(new Uri(host));
}

_balancer = new Balancer(nodes, handler, ssl, useLegacyRpcExceptionForCancellation, handlerOptions, grpcLoggerFactory, interceptors);
_balancer = new Balancer(nodes, grpcChannelOptions, interceptors);
}


Expand Down
6 changes: 2 additions & 4 deletions dotnet-etcd/multiplexer/Balancer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,7 @@ internal class Balancer
/// </summary>
private static readonly Random s_random = new Random();

internal Balancer(List<Uri> nodes, HttpMessageHandler handler = null, bool ssl = false,
bool useLegacyRpcExceptionForCancellation = false,SocketsHttpHandlerOptions handlerOptions = null,
ILoggerFactory grpcLoggerFactory = null,
internal Balancer(List<Uri> nodes, GrpcChannelOptions? grpcChannelOptions,

Check warning on line 39 in dotnet-etcd/multiplexer/Balancer.cs

View workflow job for this annotation

GitHub Actions / build-lib

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.

Check warning on line 39 in dotnet-etcd/multiplexer/Balancer.cs

View workflow job for this annotation

GitHub Actions / build-lib

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.

Check warning on line 39 in dotnet-etcd/multiplexer/Balancer.cs

View workflow job for this annotation

GitHub Actions / build-lib

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.
params Interceptor[] interceptors)
{
_numNodes = nodes.Count;
Expand All @@ -48,7 +46,7 @@ internal Balancer(List<Uri> nodes, HttpMessageHandler handler = null, bool ssl =

foreach (Uri node in nodes)
{
Connection connection = new Connection(node, handler, ssl, useLegacyRpcExceptionForCancellation,handlerOptions,grpcLoggerFactory , interceptors);
Connection connection = new Connection(node, grpcChannelOptions, interceptors);
_healthyNode.Add(connection);
}
}
Expand Down
68 changes: 26 additions & 42 deletions dotnet-etcd/multiplexer/Connection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@
// The .NET Foundation licenses this file to you under the MIT license.

using System;
using System.Collections.Generic;
using System.Net.Http;
using Etcdserverpb;
using Grpc.Core;
using Grpc.Core.Interceptors;
using Grpc.Net.Client;
using Grpc.Net.Client.Configuration;
using Microsoft.Extensions.Logging;
using V3Lockpb;

Expand All @@ -30,55 +32,37 @@ internal class Connection

private Func<CallInvoker> createNewCallInvoker;

public Connection(Uri node, HttpMessageHandler handler = null, bool ssl = false,
bool useLegacyRpcExceptionForCancellation = false, SocketsHttpHandlerOptions handlerOptions = null, ILoggerFactory grpcLoggerFactory = null, params Interceptor[] interceptors)
public Connection(Uri node, GrpcChannelOptions? grpcChannelOptions, params Interceptor[] interceptors)

Check warning on line 35 in dotnet-etcd/multiplexer/Connection.cs

View workflow job for this annotation

GitHub Actions / build-lib

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.

Check warning on line 35 in dotnet-etcd/multiplexer/Connection.cs

View workflow job for this annotation

GitHub Actions / build-lib

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.

Check warning on line 35 in dotnet-etcd/multiplexer/Connection.cs

View workflow job for this annotation

GitHub Actions / build-lib

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.
{
createNewCallInvoker = () =>
{
GrpcChannel channel;
#if NET5_0 || NET6_0
if (handlerOptions != null)
{
handler = new SocketsHttpHandler()
{
KeepAlivePingDelay = handlerOptions.KeepAlivePingDelay,
KeepAlivePingTimeout = handlerOptions.KeepAlivePingTimeout,
ConnectTimeout = handlerOptions.ConnectTimeout,
EnableMultipleHttp2Connections = handlerOptions.EnableMultipleHttp2Connections,
KeepAlivePingPolicy =
handlerOptions.KeepAlivePingPolicyWithActiveRequests
? HttpKeepAlivePingPolicy.WithActiveRequests
: HttpKeepAlivePingPolicy.Always,
};
}
#endif
if (ssl)
{
channel = GrpcChannel.ForAddress(node, new GrpcChannelOptions
{
Credentials = new SslCredentials(),
HttpHandler = handler,
ThrowOperationCanceledOnCancellation = !useLegacyRpcExceptionForCancellation,
MaxReceiveMessageSize = null,
LoggerFactory = grpcLoggerFactory
});
}
else
{
// #if NET5_0 || NET6_0
// if (handlerOptions != null)
// {
// handler = new SocketsHttpHandler()
// {
// KeepAlivePingDelay = handlerOptions.KeepAlivePingDelay,
// KeepAlivePingTimeout = handlerOptions.KeepAlivePingTimeout,
// ConnectTimeout = handlerOptions.ConnectTimeout,
// EnableMultipleHttp2Connections = handlerOptions.EnableMultipleHttp2Connections,
// KeepAlivePingPolicy =
// handlerOptions.KeepAlivePingPolicyWithActiveRequests
// ? HttpKeepAlivePingPolicy.WithActiveRequests
// : HttpKeepAlivePingPolicy.Always,
// };
// }
// #endif
#if NETCOREAPP3_1 || NETCOREAPP3_0
AppContext.SetSwitch("System.Net.Http.SocketsHttpHandler.Http2UnencryptedSupport", true);
#endif
var options = new GrpcChannelOptions
{
Credentials = ChannelCredentials.Insecure,
HttpHandler = handler,
ThrowOperationCanceledOnCancellation = !useLegacyRpcExceptionForCancellation,
MaxReceiveMessageSize = null,
LoggerFactory = grpcLoggerFactory
};
channel = GrpcChannel.ForAddress(node, options);
}
channel = grpcChannelOptions == null
? GrpcChannel.ForAddress(node)
: GrpcChannel.ForAddress(
node,
grpcChannelOptions);
CallInvoker callInvoker;
Expand Down
1 change: 0 additions & 1 deletion tests/Integration/Framework.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ public static class Framework

public static EtcdClient Client { get; } = new EtcdClient(
"http://localhost:23790,http://localhost:23791,http://localhost:23792", //todo: вытащить в конфигурацию
useLegacyRpcExceptionForCancellation: false,
interceptors: new GrpcLogsInterceptor(
Logger,
new LogsInterceptorOptions
Expand Down

0 comments on commit 52764b6

Please sign in to comment.