Skip to content

Commit

Permalink
Add TCPKeepAlive to SqlClient Sockets (dotnet/corefx#33024)
Browse files Browse the repository at this point in the history
* 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


Commit migrated from dotnet/corefx@0db2274
  • Loading branch information
AfsanehR-zz authored and saurabh500 committed Nov 6, 2018
1 parent 40bb0fa commit a64e7fe
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
<AssemblyVersion Condition="'$(TargetGroup)' == 'netstandard1.2'">4.0.0.0</AssemblyVersion>
<AssemblyVersion Condition="'$(TargetGroup)' == 'netstandard1.3'">4.1.0.0</AssemblyVersion>
<DefineConstants Condition="'$(TargetsNetCoreApp)' == 'true'">$(DefineConstants);netcoreapp</DefineConstants>
<DefineConstants Condition="'$(TargetGroup)' == 'netcoreapp'">$(DefineConstants);FEATURE_TCPKEEPALIVE</DefineConstants>
<Configurations>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</Configurations>
</PropertyGroup>
<ItemGroup Condition="'$(TargetGroup)' == 'netstandard' OR '$(TargetsNetCoreApp)' == 'true' OR '$(IsUAPAssembly)' == 'true' ">
Expand Down Expand Up @@ -273,10 +274,12 @@
<!-- Manage the SNI toggle for Windows netstandard and UWP -->
<ItemGroup Condition="('$(TargetGroup)' == 'netstandard' OR '$(TargetsNetCoreApp)' == 'true') AND '$(TargetsWindows)' == 'true'">
<!-- Manage the SNI toggle for Windows netstandard and UWP -->
<Compile Include="System\Data\SqlClient\SNI\SNITcpHandle.Windows.cs" />
<Compile Include="System\Data\SqlClient\TdsParserStateObjectFactory.Windows.cs" />
<AdditionalFiles Include="$(MSBuildProjectDirectory)/*.analyzerdata.windows" />
</ItemGroup>
<ItemGroup Condition="'$(IsUAPAssembly)' == 'true'">
<Compile Include="System\Data\SqlClient\SNI\SNITcpHandle.Windows.cs" />
<Compile Include="System\Data\SqlClient\TdsParserStateObjectFactory.Managed.cs" />
<Compile Include="System\Data\SqlClient\LocalDBAPI.uap.cs" />
<Compile Include="System\Data\SqlClient\SNI\LocalDB.uap.cs" />
Expand Down Expand Up @@ -475,6 +478,7 @@
<Compile Include="System\Data\SqlClient\TdsParser.Unix.cs" />
<Compile Include="System\Data\SqlClient\LocalDBAPI.Unix.cs" />
<Compile Include="System\Data\SqlClient\SNI\LocalDB.Unix.cs" />
<Compile Include="System\Data\SqlClient\SNI\SNITcpHandle.Unix.cs" />
</ItemGroup>
<ItemGroup Condition="'$(TargetsWindows)' == 'true' And '$(IsPartialFacadeAssembly)' != 'true' and '$(IsUAPAssembly)' != 'true'">
<Reference Include="Microsoft.Win32.Registry" />
Expand Down
Original file line number Diff line number Diff line change
@@ -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
}
}
}
Original file line number Diff line number Diff line change
@@ -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
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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()
{
Expand Down

0 comments on commit a64e7fe

Please sign in to comment.