Skip to content

Pass ip, ips, and hostname to Request object#1476

Merged
mcollina merged 20 commits intofastify:masterfrom
shwei:pass-ip-to-request-constructor
Mar 7, 2019
Merged

Pass ip, ips, and hostname to Request object#1476
mcollina merged 20 commits intofastify:masterfrom
shwei:pass-ip-to-request-constructor

Conversation

@shwei
Copy link
Copy Markdown
Contributor

@shwei shwei commented Feb 25, 2019

Checklist

  • run npm run test and npm run benchmark
  • tests and/or benchmarks are included
  • documentation is changed or added
  • commit message and code follows Code of conduct

Add ip, ips, and hostname in Request object. Related to @delvedor's feedback at #1086 (comment). All credits go to @nwoltman . Without his guidance, the work won't be possible.

benchmarks

Copy link
Copy Markdown
Member

@mcollina mcollina left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

Comment thread fastify.js Outdated
nwoltman
nwoltman previously approved these changes Feb 25, 2019
Copy link
Copy Markdown
Member

@delvedor delvedor left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this a breaking change?
We are no longer writing the values in the Node Core request object, but in ours.

@shwei
Copy link
Copy Markdown
Contributor Author

shwei commented Feb 26, 2019

Is this a breaking change?
We are no longer writing the values in the Node Core request object, but in ours.

Hi @delvedor: Do you mean this particular commit a breaking change or this pull request?

The call to handleTrustProxy(req) assigns req.ip to some value depending on the proxy option. Isn't the req object in this case a Node Core one? https://github.com/fastify/fastify/blob/master/fastify.js#L303

Thanks, Tomas. New to fastify.

Comment thread fastify.js Outdated
Comment thread lib/request.js
Comment thread fastify.js Outdated
Comment thread fastify.js Outdated
@nwoltman nwoltman dismissed their stale review February 27, 2019 05:50

Dismissing my "Accept" because I agree with @mcollina's suggestion to refactor the code so that it doesn't allocate any new objects.

@nwoltman
Copy link
Copy Markdown
Contributor

@delvedor

Is this a breaking change?
We are no longer writing the values in the Node Core request object, but in ours

Yes, removing those values makes this a breaking change. Another option would be to add the values to Fastify's Request object now, and remove them from the Node core req object for the next major.

@Ethan-Arrowood
Copy link
Copy Markdown
Member

Changes LGTM ✨

Copy link
Copy Markdown
Member

@mcollina mcollina left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you run a benchmark, like so:

node examples/example
autocannon -c 100 -d 40 -p 10 localhost:3000

And post the results, before and after this PR?

Comment thread fastify.d.ts Outdated
Comment thread fastify.js Outdated
Comment thread fastify.js
@shwei shwei changed the title Pass ip, ips, and hostname to request constructor Pass ip, ips, and hostname to Request object Feb 28, 2019
@shwei
Copy link
Copy Markdown
Contributor Author

shwei commented Feb 28, 2019

master branch:

Switched to branch 'master'
Your branch is ahead of 'origin/master' by 6 commits.
  (use "git push" to publish your local commits)
>$ node examples/example.js 
server listening on 3000
>$ autocannon -c 100 -d 40 -p 10 localhost:3000
Running 40s test @ http://localhost:3000
100 connections with 10 pipelining factor

┌─────────┬──────┬──────┬───────┬───────┬─────────┬──────────┬───────────┐
│ Stat    │ 2.5% │ 50%  │ 97.5% │ 99%   │ Avg     │ Stdev    │ Max       │
├─────────┼──────┼──────┼───────┼───────┼─────────┼──────────┼───────────┤
│ Latency │ 0 ms │ 0 ms │ 47 ms │ 54 ms │ 4.31 ms │ 13.76 ms │ 400.59 ms │
└─────────┴──────┴──────┴───────┴───────┴─────────┴──────────┴───────────┘
┌───────────┬─────────┬─────────┬─────────┬─────────┬─────────┬─────────┬─────────┐
│ Stat      │ 1%      │ 2.5%    │ 50%     │ 97.5%   │ Avg     │ Stdev   │ Min     │
├───────────┼─────────┼─────────┼─────────┼─────────┼─────────┼─────────┼─────────┤
│ Req/Sec   │ 11895   │ 11895   │ 22175   │ 26943   │ 22765.1 │ 3482.72 │ 11890   │
├───────────┼─────────┼─────────┼─────────┼─────────┼─────────┼─────────┼─────────┤
│ Bytes/Sec │ 1.95 MB │ 1.95 MB │ 3.64 MB │ 4.42 MB │ 3.73 MB │ 571 kB  │ 1.95 MB │
└───────────┴─────────┴─────────┴─────────┴─────────┴─────────┴─────────┴─────────┘

Req/Bytes counts sampled once per second.

911k requests in 40.07s, 149 MB read

pass-ip-to-request-constructor:

>$ git checkout pass-ip-to-request-constructor
Switched to branch 'pass-ip-to-request-constructor'
Your branch is ahead of 'origin/pass-ip-to-request-constructor' by 2 commits.
  (use "git push" to publish your local commits)
>$ node examples/example.js 
server listening on 3000
>$ autocannon -c 100 -d 40 -p 10 localhost:3000
Running 40s test @ http://localhost:3000
100 connections with 10 pipelining factor

┌─────────┬──────┬──────┬───────┬───────┬─────────┬──────────┬───────────┐
│ Stat    │ 2.5% │ 50%  │ 97.5% │ 99%   │ Avg     │ Stdev    │ Max       │
├─────────┼──────┼──────┼───────┼───────┼─────────┼──────────┼───────────┤
│ Latency │ 0 ms │ 0 ms │ 47 ms │ 54 ms │ 4.27 ms │ 13.28 ms │ 409.13 ms │
└─────────┴──────┴──────┴───────┴───────┴─────────┴──────────┴───────────┘
┌───────────┬─────────┬─────────┬─────────┬─────────┬─────────┬─────────┬─────────┐
│ Stat      │ 1%      │ 2.5%    │ 50%     │ 97.5%   │ Avg     │ Stdev   │ Min     │
├───────────┼─────────┼─────────┼─────────┼─────────┼─────────┼─────────┼─────────┤
│ Req/Sec   │ 12271   │ 12271   │ 23583   │ 26463   │ 23005.3 │ 2918.96 │ 12270   │
├───────────┼─────────┼─────────┼─────────┼─────────┼─────────┼─────────┼─────────┤
│ Bytes/Sec │ 2.01 MB │ 2.01 MB │ 3.87 MB │ 4.34 MB │ 3.77 MB │ 478 kB  │ 2.01 MB │
└───────────┴─────────┴─────────┴─────────┴─────────┴─────────┴─────────┴─────────┘

Req/Bytes counts sampled once per second.

920k requests in 40.07s, 151 MB read

Per @mcollina 's comment:

Can you run a benchmark, like so:

node examples/example
autocannon -c 100 -d 40 -p 10 localhost:3000

And post the results, before and after this PR?

Copy link
Copy Markdown
Member

@mcollina mcollina left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

Copy link
Copy Markdown
Member

@mcollina mcollina left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you please make this change non semver major? The properties must still be on the raw objects.

Comment thread fastify.js
Comment thread test/trust-proxy.test.js
Copy link
Copy Markdown
Contributor

@nwoltman nwoltman left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

Copy link
Copy Markdown
Member

@mcollina mcollina left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you run a quick autocannon benchmark against the example before & after this change with the option on and off?

Also, the typescript def & test should be updated.

Comment thread docs/Server.md
Comment thread fastify.js
var logger = log.child({ reqId: req.id })
if (optAddToNodeReq) {
req.log = res.log = logger
}
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We might want to add ip here as well.
Would that be useful?

Copy link
Copy Markdown
Contributor Author

@shwei shwei Mar 7, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Something like this?

    if (optAddToNodeReq) {
      req.ip = req.connection.remoteAddress
      req.log = res.log = logger
    }

I don't know in what scenario how it would be useful... so I would not add it.

Copy link
Copy Markdown
Member

@mcollina mcollina left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@mcollina mcollina merged commit 6e57aa3 into fastify:master Mar 7, 2019
Comment thread docs/Server.md
@github-actions
Copy link
Copy Markdown

This pull request has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@github-actions github-actions Bot locked as resolved and limited conversation to collaborators Feb 10, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

semver-minor Issue or PR that should land as semver minor

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants