aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJP Dillingham <jpdillingham@gmail.com>2021-11-26 15:51:53 -0600
committerGitHub <noreply@github.com>2021-11-26 15:51:53 -0600
commitf792beb409b4fa8ee1cc8cd74bc314f2556df93d (patch)
tree542b84bcd8ac0ed14bc39da3e80290bce77677f1
parent66ed49ce621e081061b8e8a3b8d97e9cc9f658c7 (diff)
parenteb662b4f198e60d54e278e5ab56ad1f49f4200b0 (diff)
downloadslskd-f792beb409b4fa8ee1cc8cd74bc314f2556df93d.tar.gz
Merge pull request #282 from slskd/keepalive
Configure TCP KeepAlive for server and distributed connections
-rw-r--r--config/slskd.example.yml1
-rw-r--r--src/slskd/Application.cs57
-rw-r--r--src/slskd/Common/Extensions.cs32
-rw-r--r--src/slskd/Options.cs9
-rw-r--r--src/slskd/slskd.csproj2
5 files changed, 67 insertions, 34 deletions
diff --git a/config/slskd.example.yml b/config/slskd.example.yml
index 04122c3f..39659cbf 100644
--- a/config/slskd.example.yml
+++ b/config/slskd.example.yml
@@ -51,7 +51,6 @@
# connection:
# timeout:
# connect: 10000
-# write: 5000
# inactivity: 15000
# buffer:
# read: 16384
diff --git a/src/slskd/Application.cs b/src/slskd/Application.cs
index c4dfa46d..3914483e 100644
--- a/src/slskd/Application.cs
+++ b/src/slskd/Application.cs
@@ -24,6 +24,7 @@ namespace slskd
using System.IO;
using System.Linq;
using System.Net;
+ using System.Net.Sockets;
using System.Reflection;
using System.Threading;
using System.Threading.Tasks;
@@ -199,19 +200,23 @@ namespace slskd
writeBufferSize: OptionsAtStartup.Soulseek.Connection.Buffer.Write,
writeQueueSize: OptionsAtStartup.Soulseek.Connection.Buffer.WriteQueue,
connectTimeout: OptionsAtStartup.Soulseek.Connection.Timeout.Connect,
- writeTimeout: OptionsAtStartup.Soulseek.Connection.Timeout.Write,
inactivityTimeout: OptionsAtStartup.Soulseek.Connection.Timeout.Inactivity,
proxyOptions: proxyOptions);
- var transferOptions = new ConnectionOptions(
+ var configureKeepAlive = new Action<Socket>(socket =>
+ {
+ socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.KeepAlive, true);
+ socket.SetSocketOption(SocketOptionLevel.Tcp, SocketOptionName.TcpKeepAliveTime, OptionsAtStartup.Soulseek.Connection.Timeout.Inactivity / 1000);
+ socket.SetSocketOption(SocketOptionLevel.Tcp, SocketOptionName.TcpKeepAliveInterval, OptionsAtStartup.Soulseek.Connection.Timeout.Inactivity / 1000);
+ socket.SetSocketOption(SocketOptionLevel.Tcp, SocketOptionName.TcpKeepAliveRetryCount, 3);
+ });
+
+ var serverOptions = connectionOptions.With(configureSocketAction: configureKeepAlive);
+ var distributedOptions = connectionOptions.With(configureSocketAction: configureKeepAlive);
+
+ var transferOptions = connectionOptions.With(
readBufferSize: OptionsAtStartup.Soulseek.Connection.Buffer.Transfer,
- writeBufferSize: OptionsAtStartup.Soulseek.Connection.Buffer.Transfer,
- writeQueueSize: OptionsAtStartup.Soulseek.Connection.Buffer.WriteQueue,
- connectTimeout: OptionsAtStartup.Soulseek.Connection.Timeout.Connect,
- writeTimeout: 0,
- inactivityTimeout: -1,
- keepAlive: true,
- proxyOptions: proxyOptions);
+ writeBufferSize: OptionsAtStartup.Soulseek.Connection.Buffer.Transfer);
var patch = new SoulseekClientOptionsPatch(
listenPort: OptionsAtStartup.Soulseek.ListenPort,
@@ -222,10 +227,10 @@ namespace slskd
acceptDistributedChildren: !OptionsAtStartup.Soulseek.DistributedNetwork.DisableChildren,
autoAcknowledgePrivateMessages: false,
acceptPrivateRoomInvitations: true,
- serverConnectionOptions: connectionOptions,
+ serverConnectionOptions: serverOptions,
peerConnectionOptions: connectionOptions,
transferConnectionOptions: transferOptions,
- distributedConnectionOptions: connectionOptions,
+ distributedConnectionOptions: distributedOptions,
userInfoResponseResolver: UserInfoResponseResolver,
browseResponseResolver: BrowseResponseResolver,
directoryContentsResponseResolver: DirectoryContentsResponseResolver,
@@ -598,6 +603,8 @@ namespace slskd
var connectionDiff = old.Connection.DiffWith(update.Connection);
ConnectionOptions connectionPatch = null;
+ ConnectionOptions serverPatch = null;
+ ConnectionOptions distributedPatch = null;
ConnectionOptions transferPatch = null;
if (connectionDiff.Any())
@@ -620,19 +627,23 @@ namespace slskd
writeBufferSize: connection.Buffer.Write,
writeQueueSize: connection.Buffer.WriteQueue,
connectTimeout: connection.Timeout.Connect,
- writeTimeout: connection.Timeout.Write,
inactivityTimeout: connection.Timeout.Inactivity,
proxyOptions: proxyPatch);
- transferPatch = new ConnectionOptions(
- readBufferSize: connection.Buffer.Transfer,
- writeBufferSize: connection.Buffer.Transfer,
- writeQueueSize: connection.Buffer.WriteQueue,
- connectTimeout: connection.Timeout.Connect,
- writeTimeout: 0,
- inactivityTimeout: -1,
- keepAlive: true,
- proxyOptions: proxyPatch);
+ var configureKeepAlive = new Action<Socket>(socket =>
+ {
+ socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.KeepAlive, true);
+ socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.TcpKeepAliveTime, connection.Timeout.Inactivity / 1000);
+ socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.TcpKeepAliveInterval, connection.Timeout.Inactivity / 1000);
+ socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.TcpKeepAliveRetryCount, 3);
+ });
+
+ serverPatch = connectionPatch.With(configureSocketAction: configureKeepAlive);
+ distributedPatch = connectionPatch.With(configureSocketAction: configureKeepAlive);
+
+ transferPatch = connectionPatch.With(
+ readBufferSize: OptionsAtStartup.Soulseek.Connection.Buffer.Transfer,
+ writeBufferSize: OptionsAtStartup.Soulseek.Connection.Buffer.Transfer);
}
var patch = new SoulseekClientOptionsPatch(
@@ -640,11 +651,11 @@ namespace slskd
enableDistributedNetwork: old.DistributedNetwork.Disabled == update.DistributedNetwork.Disabled ? null : !update.DistributedNetwork.Disabled,
distributedChildLimit: old.DistributedNetwork.ChildLimit == update.DistributedNetwork.ChildLimit ? null : update.DistributedNetwork.ChildLimit,
acceptDistributedChildren: old.DistributedNetwork.DisableChildren == update.DistributedNetwork.DisableChildren ? null : !update.DistributedNetwork.DisableChildren,
- serverConnectionOptions: connectionPatch,
+ serverConnectionOptions: serverPatch,
peerConnectionOptions: connectionPatch,
transferConnectionOptions: transferPatch,
incomingConnectionOptions: connectionPatch,
- distributedConnectionOptions: connectionPatch);
+ distributedConnectionOptions: distributedPatch);
Logger.Debug("Patching Soulseek options with {Patch}", patch.ToJson());
diff --git a/src/slskd/Common/Extensions.cs b/src/slskd/Common/Extensions.cs
index 4d463092..32a9b26e 100644
--- a/src/slskd/Common/Extensions.cs
+++ b/src/slskd/Common/Extensions.cs
@@ -21,10 +21,12 @@ namespace slskd
using System.Collections.Generic;
using System.IO;
using System.Linq;
+ using System.Net.Sockets;
using System.Reflection;
using System.Text.Json;
using System.Text.Json.Serialization;
using System.Text.RegularExpressions;
+ using Soulseek;
using YamlDotNet.Serialization;
using YamlDotNet.Serialization.NamingConventions;
@@ -283,6 +285,36 @@ namespace slskd
}
/// <summary>
+ /// Creates a copy of this instance with the specified parameters changed.
+ /// </summary>
+ /// <param name="o">The options instance to copy.</param>
+ /// <param name="readBufferSize">The read buffer size for underlying TCP connections.</param>
+ /// <param name="writeBufferSize">The write buffer size for underlying TCP connections.</param>
+ /// <param name="writeQueueSize">The size of the write queue for double buffered writes.</param>
+ /// <param name="connectTimeout">The connection timeout, in milliseconds, for client and peer TCP connections.</param>
+ /// <param name="inactivityTimeout">The inactivity timeout, in milliseconds, for peer TCP connections.</param>
+ /// <param name="proxyOptions">Optional SOCKS 5 proxy configuration options.</param>
+ /// <param name="configureSocketAction">
+ /// The delegate invoked during instantiation to configure the server Socket instance.
+ /// </param>
+ /// <returns>The new instance.</returns>
+ public static ConnectionOptions With(
+ this ConnectionOptions o,
+ int? readBufferSize = null,
+ int? writeBufferSize = null,
+ int? writeQueueSize = null,
+ int? connectTimeout = null,
+ int? inactivityTimeout = null,
+ ProxyOptions proxyOptions = null,
+ Action<Socket> configureSocketAction = null) => new ConnectionOptions(
+ readBufferSize: readBufferSize ?? o.ReadBufferSize,
+ writeBufferSize: writeBufferSize ?? o.WriteBufferSize,
+ writeQueueSize: writeQueueSize ?? o.WriteQueueSize,
+ connectTimeout: connectTimeout ?? o.ConnectTimeout,
+ inactivityTimeout: inactivityTimeout ?? o.InactivityTimeout,
+ configureSocketAction: configureSocketAction ?? o.ConfigureSocketAction);
+
+ /// <summary>
/// Deserializes this string from json to an object of type <typeparamref name="T"/>.
/// </summary>
/// <typeparam name="T">The type to which to deserialize the string.</typeparam>
diff --git a/src/slskd/Options.cs b/src/slskd/Options.cs
index 66cb0e19..4a9df941 100644
--- a/src/slskd/Options.cs
+++ b/src/slskd/Options.cs
@@ -569,15 +569,6 @@ namespace slskd
public int Connect { get; private set; } = 10000;
/// <summary>
- /// Gets the write timeout value, in milliseconds.
- /// </summary>
- [Argument(default, "slsk-write-timeout")]
- [EnvironmentVariable("SLSK_WRITE_TIMEOUT")]
- [Description("write timeout, in milliseconds")]
- [Range(500, int.MaxValue)]
- public int Write { get; private set; } = 5000;
-
- /// <summary>
/// Gets the connection inactivity timeout, in milliseconds.
/// </summary>
[Argument(default, "slsk-inactivity-timeout")]
diff --git a/src/slskd/slskd.csproj b/src/slskd/slskd.csproj
index 2b81d689..75d7b90d 100644
--- a/src/slskd/slskd.csproj
+++ b/src/slskd/slskd.csproj
@@ -68,7 +68,7 @@
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
- <PackageReference Include="Soulseek" Version="3.1.2" />
+ <PackageReference Include="Soulseek" Version="3.2.1" />
<PackageReference Include="StyleCop.Analyzers" Version="1.2.0-beta.354">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>