Skip to content

Allow binding multiple addresses#79

Merged
FranzBusch merged 8 commits into
mainfrom
multiple-addresses
May 18, 2026
Merged

Allow binding multiple addresses#79
FranzBusch merged 8 commits into
mainfrom
multiple-addresses

Conversation

@gjcairo

@gjcairo gjcairo commented May 13, 2026

Copy link
Copy Markdown
Collaborator

This PR adds support for binding a NIOHTTPServer to multiple addresses simultaneously.

The configuration API now also accepts an array of BindTargets. All resulting channels are served concurrently using the same request handler.

The server treats its listening addresses as a unit: if any channel stops serving for any reason, all remaining channels are are also cancelled. This behaviour matches the current behaviour with a single listening address.

The listeningAddress property has been replaced by listeningAddresses, an array of addresses.

@gjcairo gjcairo added the 🆕 semver/minor Adds new public API. label May 13, 2026
@gjcairo gjcairo requested a review from FranzBusch May 13, 2026 15:25

@ehaydenr ehaydenr left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lgtm!

Comment thread Sources/NIOHTTPServer/NIOHTTPServer+HTTP1_1.swift Outdated
Comment thread Sources/NIOHTTPServer/NIOHTTPServer+ListeningAddress.swift Outdated
Comment thread Sources/NIOHTTPServer/NIOHTTPServer.swift Outdated
Comment thread Tests/NIOHTTPServerTests/NIOHTTPServerTests.swift Outdated
@gjcairo gjcairo force-pushed the multiple-addresses branch from 76df44e to 4b38661 Compare May 14, 2026 13:30
@gjcairo gjcairo requested a review from aryan-25 May 14, 2026 13:30
@gjcairo gjcairo force-pushed the multiple-addresses branch from 7db71c8 to 1726688 Compare May 15, 2026 16:06

@aryan-25 aryan-25 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some small suggestions, LGTM otherwise :)

Comment thread Tests/NIOHTTPServerTests/NIOHTTPServerTests.swift Outdated
Comment thread Sources/NIOHTTPServer/NIOHTTPServer+HTTP1_1.swift Outdated
Comment thread Tests/NIOHTTPServerTests/NIOHTTPServerTests.swift Outdated
@FranzBusch FranzBusch merged commit f8b5141 into main May 18, 2026
25 of 29 checks passed
@FranzBusch FranzBusch deleted the multiple-addresses branch May 18, 2026 12:41
aryan-25 added a commit that referenced this pull request Jun 3, 2026
Motivation:

In #79, we introduced support for binding to multiple addresses.
However, since all bound channels share a single
`ServerQuiescingHelper`, this means only the *first* bound server
channel supports graceful shutdown. In addition to this, due to the way
#79 handles the cleanup of server channels, as soon as one channel
finishes, all other channels are immediately shut down. This means that
in-flight requests on every other listener are dropped immediately when
`_serve` returns after the first listener finished. Since different
channels may complete draining at different times, we should also fix
this.

Modifications:

- Modified the HTTP/1.1 and secure upgrade channel setup methods to set
up a `ServerQuiescingHelper` for each bound channel.
- Modified the cases of the `ServerChannel` enum to store the associated
`ServerQuiescingHelper`.
- Modified the `beginGracefulShutdown` method to now iterate every
listener and initiate shutdown on each.
- Refactored the `_serve` task-group to wait for *all* listeners to
finish, and only tear the whole server down on an error.
- Added associated tests.

Result:

Graceful shutdown is now supported on every bound channel.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

🆕 semver/minor Adds new public API.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants