-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: adds customization options for SocketsHttpHandler
In `Grpc.Net.Client`, the default HttpHandler on .NET runtimes .NET 5.0 or greater is `SocketsHttpHandler`. In this PR we create a new options class to encapsulate customizations to the handler, most importantly the pooled connection timeout and whether to enable multiple http connections. The default value for the idle connection timeout is 1 minute. We find this too strict in lambda environments, where the container may freeze and thaw in greater than 1 minute spans but less than the server timeout (5 minutes as of writing). Therefore we set the lambda config pooled connection idle timeout higher, to 6 minutes. That way a lambda function will not needlessly reconnect when the connection is still open.
- Loading branch information
Showing
12 changed files
with
161 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
66 changes: 66 additions & 0 deletions
66
src/Momento.Sdk/Config/Transport/SocketsHttpHandlerOptions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
#pragma warning disable 1591 | ||
using System; | ||
using Momento.Sdk.Internal; | ||
namespace Momento.Sdk.Config.Transport; | ||
|
||
public class SocketsHttpHandlerOptions | ||
{ | ||
public static TimeSpan DefaultPooledConnectionIdleTimeout { get; } = TimeSpan.FromMinutes(1); | ||
public TimeSpan PooledConnectionIdleTimeout { get; } = DefaultPooledConnectionIdleTimeout; | ||
public bool EnableMultipleHttp2Connections { get; } = true; | ||
|
||
public SocketsHttpHandlerOptions() { } | ||
public SocketsHttpHandlerOptions(TimeSpan pooledConnectionIdleTimeout) : this(pooledConnectionIdleTimeout, true) { } | ||
public SocketsHttpHandlerOptions(bool enableMultipleHttp2Connections) : this(DefaultPooledConnectionIdleTimeout, enableMultipleHttp2Connections) { } | ||
|
||
public SocketsHttpHandlerOptions(TimeSpan pooledConnectionIdleTimeout, bool enableMultipleHttp2Connections) | ||
{ | ||
Utils.ArgumentStrictlyPositive(pooledConnectionIdleTimeout, nameof(pooledConnectionIdleTimeout)); | ||
PooledConnectionIdleTimeout = pooledConnectionIdleTimeout; | ||
EnableMultipleHttp2Connections = enableMultipleHttp2Connections; | ||
} | ||
|
||
public SocketsHttpHandlerOptions WithPooledConnectionIdleTimeout(TimeSpan pooledConnectionIdleTimeout) | ||
{ | ||
return new SocketsHttpHandlerOptions(pooledConnectionIdleTimeout, EnableMultipleHttp2Connections); | ||
} | ||
|
||
public SocketsHttpHandlerOptions WithEnableMultipleHttp2Connections(bool enableMultipleHttp2Connections) | ||
{ | ||
return new SocketsHttpHandlerOptions(PooledConnectionIdleTimeout, enableMultipleHttp2Connections); | ||
} | ||
|
||
public static SocketsHttpHandlerOptions Of(TimeSpan pooledConnectionIdleTimeout) | ||
{ | ||
return new SocketsHttpHandlerOptions(pooledConnectionIdleTimeout); | ||
} | ||
|
||
public static SocketsHttpHandlerOptions Of(bool enableMultipleHttp2Connections) | ||
{ | ||
return new SocketsHttpHandlerOptions(enableMultipleHttp2Connections); | ||
} | ||
|
||
public static SocketsHttpHandlerOptions Of(TimeSpan pooledConnectionIdleTimeout, bool enableMultipleHttp2Connections) | ||
{ | ||
return new SocketsHttpHandlerOptions(pooledConnectionIdleTimeout, enableMultipleHttp2Connections); | ||
} | ||
|
||
public override bool Equals(object obj) | ||
{ | ||
if (obj == null || GetType() != obj.GetType()) | ||
{ | ||
return false; | ||
} | ||
|
||
var other = (SocketsHttpHandlerOptions)obj; | ||
return PooledConnectionIdleTimeout.Equals(other.PooledConnectionIdleTimeout) && | ||
EnableMultipleHttp2Connections.Equals(other.EnableMultipleHttp2Connections); | ||
} | ||
|
||
public override int GetHashCode() | ||
{ | ||
return PooledConnectionIdleTimeout.GetHashCode() * 17 + EnableMultipleHttp2Connections.GetHashCode(); | ||
} | ||
|
||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.