Skip to content

Commit b6b5c84

Browse files
committed
Set TCP keepalive for all OSes. Fixes #746
1 parent 8faa26d commit b6b5c84

File tree

1 file changed

+6
-1
lines changed

1 file changed

+6
-1
lines changed

src/MySqlConnector/Utilities/SocketExtensions.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,8 @@ public static void SetKeepAlive(this Socket socket, uint keepAliveTimeSeconds)
4848
return;
4949

5050
// If keepAliveTimeSeconds > 0, override keepalive options on the socket
51-
const uint keepAliveIntervalMillis = 1000;
51+
const int keepAliveIntervalMillis = 1000;
52+
#if !NETCOREAPP3_0
5253
if (Utility.IsWindows())
5354
{
5455
// http://stackoverflow.com/a/11834055/1419658
@@ -63,6 +64,10 @@ public static void SetKeepAlive(this Socket socket, uint keepAliveTimeSeconds)
6364
// Unix not supported: The appropriate socket options to set Keepalive options are not exposd in .NET
6465
// https://github.com/dotnet/corefx/issues/14237
6566
// Unix will still respect the OS Default Keepalive settings
67+
#else
68+
socket.SetSocketOption(SocketOptionLevel.Tcp, SocketOptionName.TcpKeepAliveInterval, keepAliveIntervalMillis / 1000);
69+
socket.SetSocketOption(SocketOptionLevel.Tcp, SocketOptionName.TcpKeepAliveTime, (int) keepAliveTimeSeconds);
70+
#endif
6671
}
6772
}
6873
}

0 commit comments

Comments
 (0)