Skip to content

Commit 2497f98

Browse files
committed
CSHARP-1994: Enable and configure TCP Keepalive by default.
1 parent 45700f7 commit 2497f98

File tree

3 files changed

+46
-1
lines changed

3 files changed

+46
-1
lines changed
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/* Copyright 2018-present MongoDB Inc.
2+
*
3+
* Licensed under the Apache License, Version 2.0 (the "License");
4+
* you may not use this file except in compliance with the License.
5+
* You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software
10+
* distributed under the License is distributed on an "AS IS" BASIS,
11+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
* See the License for the specific language governing permissions and
13+
* limitations under the License.
14+
*/
15+
16+
using System;
17+
18+
namespace MongoDB.Driver.Core.Connections
19+
{
20+
internal struct KeepAliveValues
21+
{
22+
public ulong OnOff { get; set; }
23+
public ulong KeepAliveTime { get; set; }
24+
public ulong KeepAliveInterval { get; set; }
25+
26+
public byte[] ToBytes()
27+
{
28+
var bytes = new byte[24];
29+
Array.Copy(BitConverter.GetBytes(OnOff), 0, bytes, 0, 8);
30+
Array.Copy(BitConverter.GetBytes(KeepAliveTime), 0, bytes, 8, 8);
31+
Array.Copy(BitConverter.GetBytes(KeepAliveInterval), 0, bytes, 16, 8);
32+
return bytes;
33+
}
34+
}
35+
}

src/MongoDB.Driver.Core/Core/Connections/TcpStreamFactory.cs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -249,7 +249,16 @@ private Socket CreateSocket(EndPoint endPoint)
249249
addressFamily = _settings.AddressFamily;
250250
}
251251

252-
return new Socket(addressFamily, SocketType.Stream, ProtocolType.Tcp);
252+
var socket = new Socket(addressFamily, SocketType.Stream, ProtocolType.Tcp);
253+
var keepAliveValues = new KeepAliveValues
254+
{
255+
OnOff = 1,
256+
KeepAliveTime = 300000, // 300 seconds in milliseconds
257+
KeepAliveInterval = 10000 // 10 seconds in milliseconds
258+
};
259+
socket.IOControl(IOControlCode.KeepAliveValues, keepAliveValues.ToBytes(), null);
260+
261+
return socket;
253262
}
254263

255264
private async Task<EndPoint[]> ResolveEndPointsAsync(EndPoint initial)

src/MongoDB.Driver.Core/MongoDB.Driver.Core.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,7 @@
122122
<Compile Include="Core\Configuration\SdamLoggingSettings.cs" />
123123
<Compile Include="Core\Connections\ClientDocumentHelper.cs" />
124124
<Compile Include="Core\Connections\IsMasterHelper.cs" />
125+
<Compile Include="Core\Connections\KeepAliveValues.cs" />
125126
<Compile Include="Core\Events\Diagnostics\TraceSourceSdamEventSubscriber.cs" />
126127
<Compile Include="Core\Events\SdamInformationEvent.cs" />
127128
<Compile Include="Core\Misc\ArrayFiltersFeature.cs" />

0 commit comments

Comments
 (0)