-
Notifications
You must be signed in to change notification settings - Fork 44
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
bf4c723
commit 1c36012
Showing
2 changed files
with
56 additions
and
10 deletions.
There are no files selected for viewing
53 changes: 53 additions & 0 deletions
53
source/Halibut/Transport/Protocol/TcpClientTimeoutExtensionMethods.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,53 @@ | ||
using System; | ||
using System.IO; | ||
using System.Net.Sockets; | ||
using System.Threading.Tasks; | ||
using Halibut.Diagnostics; | ||
|
||
namespace Halibut.Transport.Protocol | ||
{ | ||
public static class TcpClientTimeoutExtensionMethods | ||
{ | ||
public static async Task WithTimeout(this TcpClient stream, HalibutTimeoutsAndLimits halibutTimeoutsAndLimits, MessageExchangeStreamTimeout timeout, Func<Task> func) | ||
{ | ||
var currentReadTimeout = stream.Client.ReceiveTimeout; | ||
var currentWriteTimeout = stream.Client.SendTimeout; | ||
|
||
try | ||
{ | ||
stream.SetReadAndWriteTimeouts(timeout, halibutTimeoutsAndLimits); | ||
await func(); | ||
} | ||
finally | ||
{ | ||
stream.ReceiveTimeout = currentReadTimeout; | ||
stream.SendTimeout = currentWriteTimeout; | ||
} | ||
} | ||
|
||
public static void SetReadAndWriteTimeouts(this TcpClient stream, MessageExchangeStreamTimeout timeout, HalibutTimeoutsAndLimits halibutTimeoutsAndLimits) | ||
{ | ||
switch (timeout) | ||
{ | ||
case MessageExchangeStreamTimeout.NormalTimeout: | ||
stream.Client.SendTimeout = (int)halibutTimeoutsAndLimits.TcpClientSendTimeout.TotalMilliseconds; | ||
stream.Client.ReceiveTimeout = (int)halibutTimeoutsAndLimits.TcpClientReceiveTimeout.TotalMilliseconds; | ||
break; | ||
case MessageExchangeStreamTimeout.ControlMessageExchangeShortTimeout: | ||
stream.Client.SendTimeout = (int)halibutTimeoutsAndLimits.TcpClientHeartbeatSendTimeout.TotalMilliseconds; | ||
stream.Client.ReceiveTimeout = (int)halibutTimeoutsAndLimits.TcpClientHeartbeatReceiveTimeout.TotalMilliseconds; | ||
break; | ||
case MessageExchangeStreamTimeout.AuthenticationShortTimeout: | ||
stream.Client.SendTimeout = (int)halibutTimeoutsAndLimits.TcpClientAuthenticationSendTimeout.TotalMilliseconds; | ||
stream.Client.ReceiveTimeout = (int)halibutTimeoutsAndLimits.TcpClientAuthenticationReceiveTimeout.TotalMilliseconds; | ||
break; | ||
case MessageExchangeStreamTimeout.PollingForNextRequestShortTimeout: | ||
stream.Client.SendTimeout = (int)halibutTimeoutsAndLimits.TcpClientPollingForNextRequestSendTimeout.TotalMilliseconds; | ||
stream.Client.ReceiveTimeout = (int)halibutTimeoutsAndLimits.TcpClientPollingForNextRequestReceiveTimeout.TotalMilliseconds; | ||
break; | ||
default: | ||
throw new ArgumentOutOfRangeException(nameof(timeout), timeout, null); | ||
} | ||
} | ||
} | ||
} |
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