From 0db2274434fc8c398191c5419e850af89419b3e6 Mon Sep 17 00:00:00 2001 From: Afsaneh Rafighi Date: Mon, 5 Nov 2018 16:31:29 -0800 Subject: [PATCH] Add TCPKeepAlive to SqlClient Sockets (#33024) * Updating version files * Enable keep alive on SqlClient TCP sockets * make KeepAlive netcoreapp specific * added condition for uapassembly * added a new defineConstants called FEATURE_TCPKEEPALIVE * update TCPKeepAliveInterval value * set KeepAlive values in unix only * add documentation and add link to Github Issue * move SNITcpHandle.Windows.cs available in UAP * updating csproj for SNITcpHandle * fix in csproj for CI failure --- .../src/System.Data.SqlClient.csproj | 4 ++++ .../Data/SqlClient/SNI/SNITcpHandle.Unix.cs | 19 +++++++++++++++++++ .../SqlClient/SNI/SNITcpHandle.Windows.cs | 16 ++++++++++++++++ .../System/Data/SqlClient/SNI/SNITcpHandle.cs | 2 ++ 4 files changed, 41 insertions(+) create mode 100644 src/System.Data.SqlClient/src/System/Data/SqlClient/SNI/SNITcpHandle.Unix.cs create mode 100644 src/System.Data.SqlClient/src/System/Data/SqlClient/SNI/SNITcpHandle.Windows.cs diff --git a/src/System.Data.SqlClient/src/System.Data.SqlClient.csproj b/src/System.Data.SqlClient/src/System.Data.SqlClient.csproj index 2c0e366c9167..a288a3ed5c6a 100644 --- a/src/System.Data.SqlClient/src/System.Data.SqlClient.csproj +++ b/src/System.Data.SqlClient/src/System.Data.SqlClient.csproj @@ -9,6 +9,7 @@ 4.0.0.0 4.1.0.0 $(DefineConstants);netcoreapp + $(DefineConstants);FEATURE_TCPKEEPALIVE net461-Windows_NT-Debug;net461-Windows_NT-Release;netcoreapp-Debug;netcoreapp-Release;netcoreapp-Unix-Debug;netcoreapp-Unix-Release;netcoreapp-Windows_NT-Debug;netcoreapp-Windows_NT-Release;netcoreapp2.1-Debug;netcoreapp2.1-Release;netcoreapp2.1-Unix-Debug;netcoreapp2.1-Unix-Release;netcoreapp2.1-Windows_NT-Debug;netcoreapp2.1-Windows_NT-Release;netfx-Windows_NT-Debug;netfx-Windows_NT-Release;netstandard-Debug;netstandard-Release;netstandard-Unix-Debug;netstandard-Unix-Release;netstandard-Windows_NT-Debug;netstandard-Windows_NT-Release;netstandard1.2-Debug;netstandard1.2-Release;netstandard1.3-Debug;netstandard1.3-Release;uap-Windows_NT-Debug;uap-Windows_NT-Release;uap10.0.16299-Windows_NT-Debug;uap10.0.16299-Windows_NT-Release @@ -273,10 +274,12 @@ + + @@ -475,6 +478,7 @@ + diff --git a/src/System.Data.SqlClient/src/System/Data/SqlClient/SNI/SNITcpHandle.Unix.cs b/src/System.Data.SqlClient/src/System/Data/SqlClient/SNI/SNITcpHandle.Unix.cs new file mode 100644 index 000000000000..be2a26abcb68 --- /dev/null +++ b/src/System.Data.SqlClient/src/System/Data/SqlClient/SNI/SNITcpHandle.Unix.cs @@ -0,0 +1,19 @@ +using System; +using System.Collections.Generic; +using System.Net.Sockets; +using System.Text; + +namespace System.Data.SqlClient.SNI +{ + internal partial class SNITcpHandle + { + internal static void SetKeepAliveValues(ref Socket socket) + { +#if FEATURE_TCPKEEPALIVE + socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.KeepAlive, true); + socket.SetSocketOption(SocketOptionLevel.Tcp, SocketOptionName.TcpKeepAliveInterval, 1); + socket.SetSocketOption(SocketOptionLevel.Tcp, SocketOptionName.TcpKeepAliveTime, 30); +#endif + } + } +} diff --git a/src/System.Data.SqlClient/src/System/Data/SqlClient/SNI/SNITcpHandle.Windows.cs b/src/System.Data.SqlClient/src/System/Data/SqlClient/SNI/SNITcpHandle.Windows.cs new file mode 100644 index 000000000000..15a8a4e98526 --- /dev/null +++ b/src/System.Data.SqlClient/src/System/Data/SqlClient/SNI/SNITcpHandle.Windows.cs @@ -0,0 +1,16 @@ +using System; +using System.Collections.Generic; +using System.Net.Sockets; +using System.Text; + +namespace System.Data.SqlClient.SNI +{ + internal partial class SNITcpHandle + { + internal static void SetKeepAliveValues(ref Socket socket) + { + //This method will later be setting the KeepAlive, TcpKeepAliveInterval and TcpKeepAliveTime based on Windows platform specific checks. + // Link to issue: https://github.com/dotnet/corefx/issues/33209 + } + } +} diff --git a/src/System.Data.SqlClient/src/System/Data/SqlClient/SNI/SNITcpHandle.cs b/src/System.Data.SqlClient/src/System/Data/SqlClient/SNI/SNITcpHandle.cs index 5c49c70cb442..3e42ae78ad68 100644 --- a/src/System.Data.SqlClient/src/System/Data/SqlClient/SNI/SNITcpHandle.cs +++ b/src/System.Data.SqlClient/src/System/Data/SqlClient/SNI/SNITcpHandle.cs @@ -223,6 +223,8 @@ void Cancel() if (ipAddresses[i] != null) { sockets[i] = new Socket(ipAddresses[i].AddressFamily, SocketType.Stream, ProtocolType.Tcp); + // enable keep-alive on socket + SNITcpHandle.SetKeepAliveValues(ref sockets[i]); sockets[i].Connect(ipAddresses[i], port); if (sockets[i] != null) // sockets[i] can be null if cancel callback is executed during connect() {