Repository navigation
Build the TLS context once for an ssl object instead of for every connection - #1280
Merged
Merged
Conversation
Owner
|
Ah - that's a nice optimization! Thank you |
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.
When
sslis an object, every connection passes it totls.connect, which builds a new TLS context from it each time. Building one parses every certificate inca, so with a CA bundle, which managed databases ask clients to pass, each new connection spends milliseconds of CPU on the same work, and a burst of new connections, as when a pool fills up, blocks the event loop while it builds one for each. 3.4.9 does the same.This builds the context once, with
tls.createSecureContext, from ansslobject that has aca, and gives it to every connection assecureContexttogether with the object's other options, so those that belong to a connection, such asservername,checkServerIdentityandrejectUnauthorized, still reachtls.connect. The context is kept for eachsslobject and built again when one of the object's values is replaced, so a renewedcertonsql.options.sslis picked up by the next connection as before; a change made inside a value, such as a push onto the samecaarray, is not. An object without acais passed on as before, so each connection still builds its own context from Node's default certificate authorities as they are then, and followstls.setDefaultCACertificates(). AsecureContextgiven in the object is used as it is. It applies on Node.js only, so Bun, Deno and Cloudflare Workers connect as before. The README says so in its SSL section, and a test counts the contexts built for an object without aca, with one, and after a value in it is replaced.Reproduction
Against PostgreSQL 17 with TLS, on Node.js 22, with Node's 116 root certificates and the server's own CA as
ca. On 4.0.0:With this change: