From 52764b697133b15caa7f294224a33375b45543b5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=92=D1=81=D0=B5=D0=B2=D0=BE=D0=BB=D0=BE=D0=B4?= Date: Mon, 31 Jul 2023 10:47:38 +0600 Subject: [PATCH] =?UTF-8?q?grpcChannelOptions=20=D1=82=D0=B5=D0=BF=D0=B5?= =?UTF-8?q?=D1=80=D1=8C=20=D0=BF=D0=B5=D1=80=D0=B5=D0=B4=D0=B0=D1=8E=D1=82?= =?UTF-8?q?=D1=81=D1=8F=20=D0=B2=20=D0=BA=D0=BE=D0=BD=D1=81=D1=82=D1=80?= =?UTF-8?q?=D1=83=D0=BA=D1=82=D0=BE=D1=80=20=D0=B0=20=D0=BD=D0=B5=20=D0=B3?= =?UTF-8?q?=D0=B5=D0=BD=D0=B5=D1=80=D1=8F=D1=82=D1=81=D1=8F=20=D0=B2=D0=BD?= =?UTF-8?q?=D1=83=D1=82=D1=80=D0=B8=20=D0=BB=D0=B8=D0=B1=D1=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- dotnet-etcd/etcdClient.cs | 17 ++----- dotnet-etcd/multiplexer/Balancer.cs | 6 +-- dotnet-etcd/multiplexer/Connection.cs | 68 ++++++++++----------------- tests/Integration/Framework.cs | 1 - 4 files changed, 32 insertions(+), 60 deletions(-) diff --git a/dotnet-etcd/etcdClient.cs b/dotnet-etcd/etcdClient.cs index d136545..89bd392 100644 --- a/dotnet-etcd/etcdClient.cs +++ b/dotnet-etcd/etcdClient.cs @@ -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; } - } /// /// Etcd client is the entrypoint for this library. @@ -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, params Interceptor[] interceptors ) { @@ -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); } diff --git a/dotnet-etcd/multiplexer/Balancer.cs b/dotnet-etcd/multiplexer/Balancer.cs index 334d1b0..9440e8f 100644 --- a/dotnet-etcd/multiplexer/Balancer.cs +++ b/dotnet-etcd/multiplexer/Balancer.cs @@ -36,9 +36,7 @@ internal class Balancer /// private static readonly Random s_random = new Random(); - internal Balancer(List nodes, HttpMessageHandler handler = null, bool ssl = false, - bool useLegacyRpcExceptionForCancellation = false,SocketsHttpHandlerOptions handlerOptions = null, - ILoggerFactory grpcLoggerFactory = null, + internal Balancer(List nodes, GrpcChannelOptions? grpcChannelOptions, params Interceptor[] interceptors) { _numNodes = nodes.Count; @@ -48,7 +46,7 @@ internal Balancer(List 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); } } diff --git a/dotnet-etcd/multiplexer/Connection.cs b/dotnet-etcd/multiplexer/Connection.cs index 6155692..077d8b4 100644 --- a/dotnet-etcd/multiplexer/Connection.cs +++ b/dotnet-etcd/multiplexer/Connection.cs @@ -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; @@ -30,55 +32,37 @@ internal class Connection private Func 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) { 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; diff --git a/tests/Integration/Framework.cs b/tests/Integration/Framework.cs index d54a6cb..05fee82 100644 --- a/tests/Integration/Framework.cs +++ b/tests/Integration/Framework.cs @@ -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