Repository navigation
perf: cache trust proxy lookups in req.ip, protocol and host - #7519
Open
Abdel-Monaam-Aouini wants to merge 2 commits into
Open
Abdel-Monaam-Aouini wants to merge 2 commits into
Abdel-Monaam-Aouini wants to merge 2 commits into
Conversation
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
trust proxyis a subnet list (e.g.loopback, 10.0.0.0/8), the trust check on the socket address is slow (~1us), andreq.protocol,req.hostandreq.hostnameeach repeat it.req.iprunsproxy-addron every access (~2us). Middleware like logging or rate limiting often reads these more than once per request.This keeps the result on the request and reuses it while its inputs are the same:
protocol,secure,host,hostnameandsubdomains(key: trust function and socket address)req.ip(key: trust function, socket address andX-Forwarded-For)If any of those change, it is computed again, so the returned values are the same as before.
req.ipsis not changed because it returns an array.Micro benchmark per access with
trust proxyset toloopback, 10.0.0.0/8:req.ip: ~2150ns -> ~64nsreq.protocol: ~1080ns -> ~140nsreq.host: ~1110ns -> ~100nsWith
trust proxyoff ortruethese were already cheap (10-250ns), so there is not much difference there.Added tests for repeated reads, the trust function being called once, and the cache refreshing when the socket address, headers or
trust proxysetting change.