New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
perf: Optimize resolving X-Forwarded-For addresses #5458
Conversation
Signed-off-by: Susan <25956407+dmkng@users.noreply.github.com>
Signed-off-by: Susan <25956407+dmkng@users.noreply.github.com>
can you adjust the pr title according to the rules? |
how much perf does this give us? |
I added
I made a simple test that sends 11 addresses in X-Forwarded-For to the Fastify server with Without the optimization:
With the optimization:
|
Co-authored-by: Gürgün Dayıoğlu <hey@gurgun.day> Signed-off-by: Susan <25956407+dmkng@users.noreply.github.com>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
lgtm
Signed-off-by: Susan <25956407+dmkng@users.noreply.github.com>
ip: { | ||
get () { | ||
return proxyAddr(this.raw, proxyFn) | ||
const addrs = proxyAddr.all(this.raw, proxyFn) | ||
return addrs[addrs.length - 1] | ||
} | ||
}, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just leaving a note here that this won't slow down the performance if only one IP exists, as proxyAddr
itself also calls proxyAddr.all
, and returns the last element of the returned array, so it does the same thing
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
lgtm
Hello, I have a suggestion. When looking up how trustProxy option in Fastify works in detail, I found out that the proxy-addr dependency used by Fastify, specifically the
.all()
function, checks if the trust argument is provided, and if it's not, then it just returns all adresses, skipping the trust checking logic alltogether, which can speed up things greatly, especially if there's a lot of X-Forwarded-For addresses. So, I made this PR, replacing thereturn true
function withundefined
, which has the same effect in the.ips
property getter ofRequest
, but because the proxy-addr default function doesn't support skipping trust argument, I replaced it with proxy-addr.all()
function in the.ip
property getter and did the same thing there as the proxy-addr default function does, so everything behaves exactly the same as before. If something needs further explanation please let me know.Checklist
npm run test
andnpm run benchmark
and the Code of conduct