Cache the flow private key instead of parsing it on every request - #219
Merged
Conversation
default_flow_request_decryptor called load_pem_private_key on every flow data exchange. Parsing a PEM is expensive — cryptography validates the RSA key material on load — and it dominated the cost of the decryption it exists to enable: ~56ms against ~2.8ms for the rest of the request, with or without a password on the key. That put a ceiling of roughly 18 exchanges per second per core on any flow endpoint, in synchronous CPU work, to re-answer a question whose answer never changes for the life of the process. The key is now parsed once per (private_key, password) pair through a bounded lru_cache, taking the same request to ~2.8ms — about 20x, and a ceiling nearer 355/s/core. The cache keys come from the developer's own configuration and never from a request.
Owner
|
Thanks so much for this contribution — really appreciate the effort you put in. Merged into |
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.
The problem
default_flow_request_decryptorcallsload_pem_private_keyon every flow data exchange. Parsing a PEM is expensive —cryptographyvalidates the RSA key material on load — and it turns out to dominate the cost of the decryption it exists to enable.Measured on
master, 2048-bit key, 30 requests each, averaged:Worth noting it is not the password KDF — an unencrypted key costs the same. It affects every flow endpoint.
In practice that is a ceiling of roughly 18 exchanges per second per core, in synchronous CPU work on the event loop, spent re-answering a question whose answer never changes for the life of the process. We found it while load-testing a flow: every screen tap carried ~56ms that had nothing to do with the screen, and it was by far the largest cost in the journey.
The change
The key is parsed once per
(private_key, password)pair, behind a boundedfunctools.lru_cache. Nothing else in the function changes — same OAEP/SHA-256 unwrap, same AES-GCM, same return contract.That takes the same request to ~2.8 ms, and the ceiling to nearer 355/s/core.
Notes
maxsize=8), and its keys come from the developer's own configuration — never from a request — so nothing network-facing can grow it.tests/test_server.py: the key is parsed once, and the password is part of the cache key so a wrong password is not served a key a correct one loaded.tests/test_server.pypasses (4/4). The 5 failures and 14 errors elsewhere in the suite are pre-existing onmaster— identical before and after this change.