Skip to content

Commit

Permalink
Fix
Browse files Browse the repository at this point in the history
  • Loading branch information
JamesNK committed Aug 4, 2023
1 parent a47f871 commit e966cbb
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 8 deletions.
23 changes: 22 additions & 1 deletion src/Grpc.Net.Client/GrpcChannel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,7 @@ private static HttpHandlerContext CalculateHandlerContext(ILogger logger, Uri ad

type = HttpHandlerType.SocketsHttpHandler;
connectTimeout = socketsHttpHandler.ConnectTimeout;
connectionIdleTimeout = socketsHttpHandler.PooledConnectionIdleTimeout;
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
Expand Down Expand Up @@ -312,6 +312,27 @@ private static HttpHandlerContext CalculateHandlerContext(ILogger logger, Uri ad
}

return new HttpHandlerContext(HttpHandlerType.Custom);

#if NET5_0_OR_GREATER
static TimeSpan? GetConnectionIdleTimeout(SocketsHttpHandler socketsHttpHandler)
{
// Check if either TimeSpan is InfiniteTimeSpan, and return the other one.
if (socketsHttpHandler.PooledConnectionIdleTimeout == Timeout.InfiniteTimeSpan)
{
return socketsHttpHandler.PooledConnectionLifetime;
}

if (socketsHttpHandler.PooledConnectionLifetime == Timeout.InfiniteTimeSpan)
{
return socketsHttpHandler.PooledConnectionIdleTimeout;
}

// Return the bigger TimeSpan.
return socketsHttpHandler.PooledConnectionIdleTimeout > socketsHttpHandler.PooledConnectionLifetime
? socketsHttpHandler.PooledConnectionIdleTimeout
: socketsHttpHandler.PooledConnectionLifetime;
}
#endif
}

#if NET5_0_OR_GREATER
Expand Down
32 changes: 25 additions & 7 deletions test/Grpc.Net.Client.Tests/GrpcChannelTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -222,17 +222,35 @@ public void Build_ConnectTimeout_ReadFromSocketsHttpHandler()
Assert.AreEqual(TimeSpan.FromSeconds(1), channel.ConnectTimeout);
}

[Test]
public void Build_ConnectionIdleTimeout_ReadFromSocketsHttpHandler()
[TestCase(-1, -1, -1)]
[TestCase(0, 0, 0)]
[TestCase(0, -1, 0)]
[TestCase(-1, 0, 0)]
[TestCase(1000, -1, 1000)]
[TestCase(-1, 1000, 1000)]
[TestCase(500, 1000, 1000)]
[TestCase(1000, 500, 1000)]
public void Build_ConnectionIdleTimeout_ReadFromSocketsHttpHandler(
int? pooledConnectionIdleTimeoutMs,
int? pooledConnectionLifetimeMs,
int expectedConnectionIdleTimeoutMs)
{
// Arrange & Act
var channel = GrpcChannel.ForAddress("https://localhost", CreateGrpcChannelOptions(o => o.HttpHandler = new SocketsHttpHandler
// Arrange
var handler = new SocketsHttpHandler();
if (pooledConnectionIdleTimeoutMs != null)
{
PooledConnectionIdleTimeout = TimeSpan.FromSeconds(1)
}));
handler.PooledConnectionIdleTimeout = TimeSpan.FromMilliseconds(pooledConnectionIdleTimeoutMs.Value);
}
if (pooledConnectionLifetimeMs != null)
{
handler.PooledConnectionLifetime = TimeSpan.FromMilliseconds(pooledConnectionLifetimeMs.Value);
}

// Act
var channel = GrpcChannel.ForAddress("https://localhost", CreateGrpcChannelOptions(o => o.HttpHandler = handler));

// Assert
Assert.AreEqual(TimeSpan.FromSeconds(1), channel.ConnectionIdleTimeout);
Assert.AreEqual(TimeSpan.FromMilliseconds(expectedConnectionIdleTimeoutMs), channel.ConnectionIdleTimeout);
}
#endif

Expand Down

0 comments on commit e966cbb

Please sign in to comment.