Skip to content

Avoid initializing NIOSSLContext on every connection#84

Merged
gjcairo merged 2 commits into
swift-server:mainfrom
aryan-25:update-nio-ssl-context-initialization
May 27, 2026
Merged

Avoid initializing NIOSSLContext on every connection#84
gjcairo merged 2 commits into
swift-server:mainfrom
aryan-25:update-nio-ssl-context-initialization

Conversation

@aryan-25
Copy link
Copy Markdown
Collaborator

Motivation:

In the secure upgrade pipeline, we currently initialize a NIOSSLContext on every connection. However, constructing a NIOSSLContext is a very expensive operation.

We can easily avoid this by just initializing a NIOSSLContext once and using the same instance for all connections.

Modifications:

  • Replaced the makeServerConfiguration helper extension on NIOSSL.TLSConfiguration to instead be in terms of NIOSSLContext (the new helper method is named makeServerContext).
  • Refactored the secure upgrade channel setup to use the new makeServerContext helper to initialize a NIOSSLContext just once, and plumbed that through setupSecureUpgradeServerChannels -> setupSecureUpgradeConnectionChildChannel -> makeSSLServerHandler.
  • Updated associated test cases.

Result:

We now only create a new NIOSSLContext instance once upon server initialization rather than on every connection.

Motivation:

In the secure upgrade pipeline, we currently initialize a [`NIOSSLContext`](https://github.com/apple/swift-nio-ssl/blob/8e3d34d5b6f1be4c1da71cd3f4b86c85f4da99b2/Sources/NIOSSL/SSLContext.swift#L288) on every connection. However, [constructing a `NIOSSLContext` is a very expensive operation](https://github.com/apple/swift-nio-ssl/blob/8e3d34d5b6f1be4c1da71cd3f4b86c85f4da99b2/Sources/NIOSSL/SSLContext.swift#L283-L285).

We can easily avoid this by just initializing a `NIOSSLContext` once and using the same instance for all connections.

Modifications:

- Replaced the `makeServerConfiguration` helper extension on `NIOSSL.TLSConfiguration` to instead be in terms of `NIOSSLContext` (the new helper method is named `makeServerContext`).
- Refactored the secure upgrade channel setup to use the new `makeServerContext` helper to initialize a `NIOSSLContext` just once, and plumb that through `setupSecureUpgradeServerChannels` -> `setupSecureUpgradeConnectionChildChannel` -> `makeSSLServerHandler`.
- Updated associated test cases.

Result:

We now only create a new `NIOSSLContext` instance once upon server initialization rather than on every connection.
@aryan-25 aryan-25 added the 🔨 semver/patch No public API change. label May 27, 2026
@aryan-25 aryan-25 requested a review from gjcairo May 27, 2026 10:26
@gjcairo gjcairo enabled auto-merge (squash) May 27, 2026 16:56
@gjcairo gjcairo merged commit 877b826 into swift-server:main May 27, 2026
18 of 20 checks passed
@aryan-25 aryan-25 deleted the update-nio-ssl-context-initialization branch May 27, 2026 17:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

🔨 semver/patch No public API change.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants