diff --git a/docs/ReleaseNotes.md b/docs/ReleaseNotes.md index 21f0d45e8..5a6d3baec 100644 --- a/docs/ReleaseNotes.md +++ b/docs/ReleaseNotes.md @@ -10,6 +10,7 @@ Current package versions: - Add experimental Redis 8.8 array support, including array APIs on `IDatabase`/`IDatabaseAsync`, array helper types, `RedisType.Array`, and array delete keyspace notification event types. ([#3076 by @mgravell](https://github.com/StackExchange/StackExchange.Redis/pull/3076)) +- Enable TCP keep-alives ([#3078 by @mgravell](https://github.com/StackExchange/StackExchange.Redis/pull/3078)) - `ConfigurationOptions` : don't persist `Protocol` when it comes from the defaults-provider. ([#3082 by @mgravell](https://github.com/StackExchange/StackExchange.Redis/pull/3082)) ## 2.13.1 diff --git a/src/StackExchange.Redis/SocketManager.cs b/src/StackExchange.Redis/SocketManager.cs index 146e576ff..7c521c93e 100644 --- a/src/StackExchange.Redis/SocketManager.cs +++ b/src/StackExchange.Redis/SocketManager.cs @@ -1,4 +1,5 @@ using System; +using System.Diagnostics; using System.IO.Pipelines; using System.Net; using System.Net.Sockets; @@ -223,7 +224,18 @@ internal static Socket CreateSocket(EndPoint endpoint) ? new Socket(SocketType.Stream, protocolType) : new Socket(addressFamily, SocketType.Stream, protocolType); SocketConnection.SetRecommendedClientOptions(socket); - // socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.Linger, false); + if (protocolType is ProtocolType.Tcp) + { + try + { + // enable TCP keep-alive (best effort only) + socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.KeepAlive, true); + } + catch (Exception ex) + { + Debug.WriteLine(ex.Message); + } + } return socket; }