Skip to content
This repository has been archived by the owner on Jan 23, 2023. It is now read-only.
/ corefx Public archive

Add TCPKeepAlive to SqlClient Sockets #33024

Merged
merged 13 commits into from
Nov 6, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/System.Data.SqlClient/src/System.Data.SqlClient.csproj
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)
{
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add a comment in the function saying why it has been left blank. Possible add the link to the issue, that we discussed should be opened.

//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