Add local port binding to HTTPClient.Configuration - #914
Open
VictorDebray wants to merge 2 commits into
Open
Conversation
Adds a `localPort: Int` knob on `HTTPClient.Configuration` that pins the TCP source port the connection binds to before connecting, plus a per-request `HTTPClientRequest.localPort` override. `0` (the default) keeps the previous behaviour of letting the OS assign an ephemeral port. `localPort` is threaded through `RequestOptions` and `ConnectionPool.Key` alongside the existing `localAddress`, so connections with different `(localAddress, localPort)` pairs are pooled separately. Both the NIOTS (`requiredLocalEndpoint`) and NIOPosix (`bind(to:)`) bootstraps in the plain and TLS paths honour it.
fabianfett
requested changes
Aug 6, 2026
Member
There was a problem hiding this comment.
This looks like a solid PR. Thank you. However before merging, we will need tests, that validate that the outgoing request has actually been made from the specified port. We will need this for the old (EventLoopFuture) API as well as the new one. Overwriting the settings also needs to be tested.
Reject localPort values outside 0...65535 with a new invalidLocalPort error instead of failing silently or misbehaving at bind time.
Author
|
I have added the needed tests. Thanks for the review! |
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Adds a
localPort: Intknob onHTTPClient.Configurationthat pins the TCP source port the connection binds to before connecting, plus a per-requestHTTPClientRequest.localPortoverride.0(the default) keeps the previous behaviour of letting the OS assign an ephemeral port.localPortis threaded throughRequestOptionsandConnectionPool.Keyalongside the existinglocalAddress, so connections with different(localAddress, localPort)pairs are pooled separately. Both the NIOTS (requiredLocalEndpoint) and NIOPosix (bind(to:)) bootstraps in the plain and TLS paths honour it.