Skip to content

Commit 40ab5f9

Browse files
committed
Add port sharing support to PlatformBenchmarks on Linux
1 parent 958bf20 commit 40ab5f9

File tree

1 file changed

+29
-0
lines changed
  • src/BenchmarksApps/TechEmpower/PlatformBenchmarks

1 file changed

+29
-0
lines changed

src/BenchmarksApps/TechEmpower/PlatformBenchmarks/Program.cs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,13 @@
22
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
33

44
using System;
5+
using System.Net;
6+
using System.Net.Sockets;
57
using System.Runtime.InteropServices;
68
using System.Text;
79
using System.Threading.Tasks;
810
using Microsoft.AspNetCore.Hosting;
11+
using Microsoft.AspNetCore.Server.Kestrel.Transport.Sockets;
912
using Microsoft.Extensions.Configuration;
1013
#if DATABASE
1114
using Npgsql;
@@ -106,6 +109,32 @@ public static IWebHost BuildWebHost(string[] args)
106109
if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
107110
{
108111
options.UnsafePreferInlineScheduling = Environment.GetEnvironmentVariable("DOTNET_SYSTEM_NET_SOCKETS_INLINE_COMPLETIONS") == "1";
112+
113+
// Allow multiple processes bind to the same port. This also "works" on Windows in that it will
114+
// prevent address in use errors and hand off to another process if no others are available,
115+
// but it wouldn't round-robin new connections between processes like it will on Linux.
116+
options.CreateBoundListenSocket = endpoint =>
117+
{
118+
if (endpoint is not IPEndPoint ip)
119+
{
120+
return SocketTransportOptions.CreateDefaultBoundListenSocket(endpoint);
121+
}
122+
123+
// Normally, we'd call CreateDefaultBoundListenSocket for the IPEndpoint too, but we need
124+
// to set ReuseAddress before calling bind, and CreateDefaultBoundListenSocket calls bind.
125+
var listenSocket = new Socket(ip.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
126+
127+
// Kestrel expects IPv6Any to bind to both IPv6 and IPv4
128+
if (ip.Address.Equals(IPAddress.IPv6Any))
129+
{
130+
listenSocket.DualMode = true;
131+
}
132+
133+
listenSocket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
134+
listenSocket.Bind(ip);
135+
136+
return listenSocket;
137+
};
109138
}
110139
});
111140

0 commit comments

Comments
 (0)