Skip to content

Commit 40a26c9

Browse files
authored
Support disabling load balancing with SocketsHttpHandler.Properties (#2274)
1 parent 246c43d commit 40a26c9

File tree

1 file changed

+15
-22
lines changed

1 file changed

+15
-22
lines changed

src/Grpc.Net.Client/GrpcChannel.cs

Lines changed: 15 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -266,17 +266,9 @@ private static HttpHandlerContext CalculateHandlerContext(ILogger logger, Uri ad
266266
}
267267
if (HttpRequestHelpers.HasHttpHandlerType(channelOptions.HttpHandler, "System.Net.Http.SocketsHttpHandler"))
268268
{
269-
HttpHandlerType type;
270-
TimeSpan? connectTimeout;
271-
TimeSpan? connectionIdleTimeout;
272-
273269
#if NET5_0_OR_GREATER
274270
var socketsHttpHandler = HttpRequestHelpers.GetHttpHandlerType<SocketsHttpHandler>(channelOptions.HttpHandler)!;
275271

276-
type = HttpHandlerType.SocketsHttpHandler;
277-
connectTimeout = socketsHttpHandler.ConnectTimeout;
278-
connectionIdleTimeout = GetConnectionIdleTimeout(socketsHttpHandler);
279-
280272
// Check if the SocketsHttpHandler is being shared by channels.
281273
// It has already been setup by another channel (i.e. ConnectCallback is set) then
282274
// additional channels can use advanced connectivity features.
@@ -286,33 +278,34 @@ private static HttpHandlerContext CalculateHandlerContext(ILogger logger, Uri ad
286278
// This channel can't support advanced connectivity features.
287279
if (socketsHttpHandler.ConnectCallback != null)
288280
{
289-
type = HttpHandlerType.Custom;
290-
connectTimeout = null;
291-
connectionIdleTimeout = null;
281+
return new HttpHandlerContext(HttpHandlerType.Custom);
292282
}
293283
}
294284

285+
// Load balancing has been disabled on the SocketsHttpHandler.
286+
if (socketsHttpHandler.Properties.TryGetValue("__GrpcLoadBalancingDisabled", out var value)
287+
&& value is bool loadBalancingDisabled && loadBalancingDisabled)
288+
{
289+
return new HttpHandlerContext(HttpHandlerType.Custom);
290+
}
291+
295292
// If a proxy is specified then requests could be sent via an SSL tunnel.
296293
// A CONNECT request is made to the proxy to establish the transport stream and then
297294
// gRPC calls are sent via stream. This feature isn't supported by load balancer.
298295
// Proxy can be specified via:
299296
// - SocketsHttpHandler.Proxy. Set via app code.
300297
// - HttpClient.DefaultProxy. Set via environment variables, e.g. HTTPS_PROXY.
301-
if (type == HttpHandlerType.SocketsHttpHandler)
298+
if (IsProxied(socketsHttpHandler, address, isSecure))
302299
{
303-
if (IsProxied(socketsHttpHandler, address, isSecure))
304-
{
305-
logger.LogInformation("Proxy configuration is detected. How the gRPC client creates connections can cause unexpected behavior when a proxy is configured. " +
306-
"To ensure the client correctly uses a proxy, configure GrpcChannelOptions.HttpHandler to use HttpClientHandler. " +
307-
"Note that HttpClientHandler isn't compatible with load balancing.");
308-
}
300+
logger.LogInformation("Proxy configuration is detected. How the gRPC client creates connections can cause unexpected behavior when a proxy is configured. " +
301+
"To ensure the client correctly uses a proxy, configure GrpcChannelOptions.HttpHandler to use HttpClientHandler. " +
302+
"Note that HttpClientHandler isn't compatible with load balancing.");
309303
}
304+
305+
return new HttpHandlerContext(HttpHandlerType.SocketsHttpHandler, socketsHttpHandler.ConnectTimeout, GetConnectionIdleTimeout(socketsHttpHandler));
310306
#else
311-
type = HttpHandlerType.SocketsHttpHandler;
312-
connectTimeout = null;
313-
connectionIdleTimeout = null;
307+
return new HttpHandlerContext(HttpHandlerType.SocketsHttpHandler);
314308
#endif
315-
return new HttpHandlerContext(type, connectTimeout, connectionIdleTimeout);
316309
}
317310
if (HttpRequestHelpers.GetHttpHandlerType<HttpClientHandler>(channelOptions.HttpHandler) != null)
318311
{

0 commit comments

Comments
 (0)