-
Notifications
You must be signed in to change notification settings - Fork 10.3k
Add AcceptSocketAsync to SocketTransportOptions #34345
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
Add AcceptSocketAsync to SocketTransportOptions #34345
Conversation
behavior of accepting a new connection
Can you show an example of how this addresses your original issue? If you call DuplicateAndClose on the accepted socket then what do you return? Or do you not return and just loop back to another Accept internally? |
@Tratcher yes, in the existing process if we marshal the socket to another process we DuplicateAndClose it and loop back to accept another. More or less this: while (true)
{
cancellationToken.ThrowIfCancellationRequested();
var newConnection = await listenSocket.AcceptAsync();
if (!await TryRerouteSocketAsync(newConnection))
{
// we decided to handle the request in this process
return newConnection;
}
} In the new process(es) we'd use some IPC mechanism to read marshalled Sockets and return them A little more complicated, but at a high level something like: this.EnsureReceiving(listenSocket); // spins up some connection to receive sockets, and newly received Sockets are written to receiveChannel.Writer
var socket = await receiveChannel.Reader.ReadAsync(cancellationToken);
return socket; |
@JunTaoLuo Did you ever prototype your RateLimiting stuff with the socket accept loop? Will this compose well with your prototype? Do you have any concerns? |
I didn't have a chance to prototype with the socket accept loop specifically, but looking at this change, it would compose with the rate limiter APIs. Specifically, the AcceptSocketAsync method can be made to resolve a limiter to use when making the decision to when to accept the socket. |
This seems related to #13295. I think if we did something like this, we'd probably want something like the @kevin-montrose As a tactical way to achieve something like this, could you register a custom IConnectionListenerFactory that wraps SocketTransportFactory? You'd have to wrap the factory and the listener itself, but that doesn't seem too bad. This solution is already specific to the SocketTransportFactory anyway. |
@halter73 unfortunately, wrapping The closest we can get to the underlying |
@kevin-montrose, I'm curious how you will peek at the TLS ClientHello? Do you intend to only do synchronous IO to peek at the TLS ClientHello? If the clients are slow to send data, this could be a scalability bottleneck. |
@mconnew That is a concern in general but in our particular case we can rely on SNI already being seen and parsed upstream of our app (but still on our infrastructure [unfortunately, upstream cannot send to multiple ports/processes for us]). If we could not rely on that, we could still peek synchronously and move |
I'm currently on the same page as @halter73. Maybe we can avoid doing any socket operations until the first call to ReadAsync/WriteAsync on the SocketConnection as a way to work around that. Would that be enough? |
@davidfowl With that the Not calling |
The socket is exposed via the IConnectionSocketFeature.
It requires a bunch of wrapping. |
@davidfowl ooo, I did not know With that, then yes if no async operations have happened on the That would be much cleaner than this random delegate. |
Closing this in favor of #34458. The idea is that you should be able to write a connection middleware that runs first and grabs the |
Add AcceptSocketAsync to SocketTransportOptions
Introduces a new option on
SocketTransportOptions
that allows customizing what happens between wanting to accept a new connection, and actually wrapping it up in aSocketConnection
.The motivating use case for this is to conditionally shove certain sockets to different processes based on peeking at the first packet or two.
Addresses #34344