Avoid initializing NIOSSLContext on every connection#84
Merged
gjcairo merged 2 commits intoMay 27, 2026
Merged
Conversation
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.
FranzBusch
approved these changes
May 27, 2026
gjcairo
approved these changes
May 27, 2026
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.
Motivation:
In the secure upgrade pipeline, we currently initialize a
NIOSSLContexton every connection. However, constructing aNIOSSLContextis a very expensive operation.We can easily avoid this by just initializing a
NIOSSLContextonce and using the same instance for all connections.Modifications:
makeServerConfigurationhelper extension onNIOSSL.TLSConfigurationto instead be in terms ofNIOSSLContext(the new helper method is namedmakeServerContext).makeServerContexthelper to initialize aNIOSSLContextjust once, and plumbed that throughsetupSecureUpgradeServerChannels->setupSecureUpgradeConnectionChildChannel->makeSSLServerHandler.Result:
We now only create a new
NIOSSLContextinstance once upon server initialization rather than on every connection.