-
Notifications
You must be signed in to change notification settings - Fork 10.4k
Add support for customising the creation of Kestrel listen sockets #32827
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
halter73
merged 9 commits into
dotnet:main
from
deanward81:add-listen-accept-socket-configuration
May 28, 2021
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
6ac27d9
Add support for configuring Kestrel listen and accept sockets
deanward81 47b9a6c
`ConfigureAcceptSocket` => `ConfigureAcceptedSocket`
deanward81 c1ba647
Update public API bits
deanward81 52deeee
Update src/Servers/Kestrel/Transport.Sockets/src/SocketTransportOptio…
deanward81 7a4110d
Move to using API from triage
deanward81 99676bb
Make `CreateListenSocket` non-nullable and initialize to `CreateDefau…
deanward81 5374ff4
Update test to use a time-based path for `UnixDomainSocketEndPoint`
deanward81 15497a1
Add clarifying comment
deanward81 f4a38b7
Tweak to match approved API - `CreateListenSocket` => `CreateBoundLis…
deanward81 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
104 changes: 104 additions & 0 deletions
104
src/Servers/Kestrel/test/Sockets.BindTests/SocketTransportOptionsTests.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Net; | ||
using System.Net.Sockets; | ||
using System.Runtime.InteropServices; | ||
using System.Threading.Tasks; | ||
using Microsoft.AspNetCore.Builder; | ||
using Microsoft.AspNetCore.Connections; | ||
using Microsoft.AspNetCore.Hosting; | ||
using Microsoft.AspNetCore.Http; | ||
using Microsoft.AspNetCore.Server.Kestrel.FunctionalTests; | ||
using Microsoft.AspNetCore.Server.Kestrel.Transport.Sockets; | ||
using Microsoft.AspNetCore.Testing; | ||
using Microsoft.Extensions.Hosting; | ||
using Xunit; | ||
|
||
namespace Sockets.BindTests | ||
{ | ||
public class SocketTransportOptionsTests : LoggedTestBase | ||
{ | ||
[Theory] | ||
[MemberData(nameof(GetEndpoints))] | ||
public async Task SocketTransportCallsCreateBoundListenSocket(EndPoint endpointToTest) | ||
{ | ||
var wasCalled = false; | ||
|
||
Socket CreateListenSocket(EndPoint endpoint) | ||
{ | ||
wasCalled = true; | ||
return SocketTransportOptions.CreateDefaultBoundListenSocket(endpoint); | ||
} | ||
|
||
using var host = CreateWebHost( | ||
endpointToTest, | ||
options => | ||
{ | ||
options.CreateBoundListenSocket = CreateListenSocket; | ||
} | ||
); | ||
|
||
await host.StartAsync(); | ||
Assert.True(wasCalled, $"Expected {nameof(SocketTransportOptions.CreateBoundListenSocket)} to be called."); | ||
await host.StopAsync(); | ||
} | ||
|
||
[Theory] | ||
[MemberData(nameof(GetEndpoints))] | ||
public void CreateDefaultBoundListenSocket_BindsForAllEndPoints(EndPoint endpoint) | ||
{ | ||
using var listenSocket = SocketTransportOptions.CreateDefaultBoundListenSocket(endpoint); | ||
Assert.NotNull(listenSocket.LocalEndPoint); | ||
} | ||
|
||
// static to ensure that the underlying handle doesn't get disposed | ||
// when a local reference is GCed by the iterator in GetEndPoints | ||
private static Socket _fileHandleSocket; | ||
|
||
public static IEnumerable<object[]> GetEndpoints() | ||
{ | ||
// IPv4 | ||
yield return new object[] {new IPEndPoint(IPAddress.Loopback, 0)}; | ||
// IPv6 | ||
yield return new object[] {new IPEndPoint(IPAddress.IPv6Loopback, 0)}; | ||
// Unix sockets | ||
if (!RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) | ||
{ | ||
yield return new object[] | ||
{ | ||
new UnixDomainSocketEndPoint($"/tmp/{DateTime.UtcNow:yyyyMMddTHHmmss.fff}.sock") | ||
}; | ||
} | ||
|
||
// file handle | ||
// slightly messy but allows us to create a FileHandleEndPoint | ||
// from the underlying OS handle used by the socket | ||
_fileHandleSocket = new( | ||
AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp | ||
); | ||
_fileHandleSocket.Bind(new IPEndPoint(IPAddress.Loopback, 0)); | ||
yield return new object[] | ||
{ | ||
new FileHandleEndPoint((ulong) _fileHandleSocket.Handle, FileHandleType.Auto) | ||
}; | ||
|
||
// TODO: other endpoint types? | ||
} | ||
|
||
private IHost CreateWebHost(EndPoint endpoint, Action<SocketTransportOptions> configureSocketOptions) => | ||
TransportSelector.GetHostBuilder() | ||
.ConfigureWebHost( | ||
webHostBuilder => | ||
{ | ||
webHostBuilder | ||
.UseSockets(configureSocketOptions) | ||
.UseKestrel(options => options.Listen(endpoint)) | ||
.Configure( | ||
app => app.Run(ctx => ctx.Response.WriteAsync("Hello World")) | ||
); | ||
} | ||
) | ||
.ConfigureServices(AddTestLogging) | ||
.Build(); | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.