Skip to content

Commit

Permalink
Support disabling load balancing with SocketsHttpHandler.Properties (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
JamesNK authored Sep 12, 2023
1 parent 246c43d commit 40a26c9
Showing 1 changed file with 15 additions and 22 deletions.
37 changes: 15 additions & 22 deletions src/Grpc.Net.Client/GrpcChannel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -266,17 +266,9 @@ private static HttpHandlerContext CalculateHandlerContext(ILogger logger, Uri ad
}
if (HttpRequestHelpers.HasHttpHandlerType(channelOptions.HttpHandler, "System.Net.Http.SocketsHttpHandler"))
{
HttpHandlerType type;
TimeSpan? connectTimeout;
TimeSpan? connectionIdleTimeout;

#if NET5_0_OR_GREATER
var socketsHttpHandler = HttpRequestHelpers.GetHttpHandlerType<SocketsHttpHandler>(channelOptions.HttpHandler)!;

type = HttpHandlerType.SocketsHttpHandler;
connectTimeout = socketsHttpHandler.ConnectTimeout;
connectionIdleTimeout = GetConnectionIdleTimeout(socketsHttpHandler);

// Check if the SocketsHttpHandler is being shared by channels.
// It has already been setup by another channel (i.e. ConnectCallback is set) then
// additional channels can use advanced connectivity features.
Expand All @@ -286,33 +278,34 @@ private static HttpHandlerContext CalculateHandlerContext(ILogger logger, Uri ad
// This channel can't support advanced connectivity features.
if (socketsHttpHandler.ConnectCallback != null)
{
type = HttpHandlerType.Custom;
connectTimeout = null;
connectionIdleTimeout = null;
return new HttpHandlerContext(HttpHandlerType.Custom);
}
}

// Load balancing has been disabled on the SocketsHttpHandler.
if (socketsHttpHandler.Properties.TryGetValue("__GrpcLoadBalancingDisabled", out var value)
&& value is bool loadBalancingDisabled && loadBalancingDisabled)
{
return new HttpHandlerContext(HttpHandlerType.Custom);
}

// If a proxy is specified then requests could be sent via an SSL tunnel.
// A CONNECT request is made to the proxy to establish the transport stream and then
// gRPC calls are sent via stream. This feature isn't supported by load balancer.
// Proxy can be specified via:
// - SocketsHttpHandler.Proxy. Set via app code.
// - HttpClient.DefaultProxy. Set via environment variables, e.g. HTTPS_PROXY.
if (type == HttpHandlerType.SocketsHttpHandler)
if (IsProxied(socketsHttpHandler, address, isSecure))
{
if (IsProxied(socketsHttpHandler, address, isSecure))
{
logger.LogInformation("Proxy configuration is detected. How the gRPC client creates connections can cause unexpected behavior when a proxy is configured. " +
"To ensure the client correctly uses a proxy, configure GrpcChannelOptions.HttpHandler to use HttpClientHandler. " +
"Note that HttpClientHandler isn't compatible with load balancing.");
}
logger.LogInformation("Proxy configuration is detected. How the gRPC client creates connections can cause unexpected behavior when a proxy is configured. " +
"To ensure the client correctly uses a proxy, configure GrpcChannelOptions.HttpHandler to use HttpClientHandler. " +
"Note that HttpClientHandler isn't compatible with load balancing.");
}

return new HttpHandlerContext(HttpHandlerType.SocketsHttpHandler, socketsHttpHandler.ConnectTimeout, GetConnectionIdleTimeout(socketsHttpHandler));
#else
type = HttpHandlerType.SocketsHttpHandler;
connectTimeout = null;
connectionIdleTimeout = null;
return new HttpHandlerContext(HttpHandlerType.SocketsHttpHandler);
#endif
return new HttpHandlerContext(type, connectTimeout, connectionIdleTimeout);
}
if (HttpRequestHelpers.GetHttpHandlerType<HttpClientHandler>(channelOptions.HttpHandler) != null)
{
Expand Down

0 comments on commit 40a26c9

Please sign in to comment.