first request is always slow #171

Closed
steve220685 opened this Issue Jan 1, 2012 · 15 comments

Projects

None yet

6 participants

Hello,

I am having an issue where the first request to any domain which is handled by the proxy appears to take 2-3 seconds to load before it actually does.

This was all running on a dedicated server, so I tried it out locally and had the same issue, so I set it up locally on another pc (for sanity sake) and the issue is still happening.

This is the code I am using:

var proxy = require("http-proxy");

proxy.createServer({
hostnameOnly: true,
router: {
"admin.local": "localhost:8081",
"web.local": "localhost:8080"
}
}).listen(80);

Seems to be happening on every version of node I have tested with (0.6.6, 0.6)

Is this a known issue with 0.6 compatibility, is this a known performance tradeoff when proxying like this? Have I done something silly? I am not sure!

Some clarity as to why this issue might be happening and would be great.

Thanks!

edit: The platforms I have tested this with are (osx 10.6, 10.7 and freebsd 8.2)

Contributor
Marak commented Jan 1, 2012

Can you try using node's core http.request method or use mikeal's request library?

You should be able to get the same functionality, and be able to possibly isolate the performance bottleneck.

Hi,

Ultimately, I would prefer to use a plugin that has been developed and tested by a community (and is known to perform well for the exact purpose I am using it for) and does not reproduce existing functionality within express.

That said, to try and narrow down what could be causing this issue, I have revised my sample to include a simple core http server that does a simple text dump and the same issue is still happening.

Here is my revised sample.

var http = require("http"),
proxy = require("http-proxy");

proxy.createServer({
hostnameOnly: true,
router: {
"web.local": "localhost:3000"
}
}).listen(80);

http.createServer(function(req, res) {
res.writeHead(200, {
"Content-Type": "text/plain"
});
res.write("OK");
res.end();
}).listen(3000);

Hi,

I am having the same issue on node 0.6.7, also using a simple core http server and a proxy with the same kind of config as above.

Contributor

Bumping priority to medium.

Some infos after more tests:

It doesn't seem to be directly related to http-proxy as I could reproduce the same problem with simple node http server without proxy.

Also, I've got this issue only locally on OSX Lion. I could not reproduce it on the Debian machine I have.

And then, let's say I can access my local http server through localhost, 127.0.0.1, myhostname1.local and myhostname2.local, the issue appears only at the first request of myhostname1.local and myhostname2.local.

No idea of what is going on.

Upon further investigation myself, it does indeed appear to be a Mac only issue, my freebsd servers have since fixed themselves up (Perhaps it was network latency as I am located in Australia and my servers are located in the USA and piped through locally via an SSH tunnel)

In addition to this, it does indeed appear to be not directly related to node-http-proxy as I have since tested bouncy, which is another node http proxy library, and it has the same fault.

In short, I have the exact same problem as jeremy, would be interesting to see if other mac users are having this problem too.

@mmalecki mmalecki was assigned Feb 12, 2012
Contributor

I can confirm that this happens only on Mac OS X. Also, it happens only for domains with dots in names:

[maciej@pc07 node-http-proxy (171-fix-slow-request)]$ time curl sub.local:8000
Hello from sub.local:8000
real    0m5.086s
user    0m0.008s
sys 0m0.004s
[maciej@pc07 node-http-proxy (171-fix-slow-request)]$ time curl local:8000
Hello from local:8000
real    0m0.019s
user    0m0.007s
sys 0m0.004s

Same holds true for node's http client.

I've got some interesting output from dtruss:

I've used following script:

var http = require('http'),
    proxy = require("./");

function helloWorld(req, res) {
  res.end('Hello, ' + req.headers.host);
}

proxy.createServer({
  hostnameOnly: true,
  router: {
    "local": "localhost:8001",
    "sub.local": "localhost:8002"
  }
}).listen(8000);

http.createServer(function (req, res) {
  console.log('8001');
  res.end('Hello from ' + req.headers.host);
}).listen(8001);
http.createServer(function (req, res) {
  console.log('8002');
  res.end('Hello from ' + req.headers.host);
}).listen(8002);

And /etc/hosts to map sub.local and local to 127.0.0.1.

However, it's not related to node-http-proxy in any way - same happens for pretty much every server (tried with http-server, luvit's example server and nginx).

If there's anyone who can explain it, please do.

einaros commented Feb 13, 2012

Dns or arp caching? Did you check for intermittent network requests with wireshark?

Something triggering IPv6 name resolution?

einaros commented Feb 13, 2012

@marak, yep. First request does a forced dns lookup, subsequent use cache.

Thanks for the link! It is probably related. For now I will replace my ".local" extensions to ".dev" instead...

Cool, thanks for the information, I too will replace .local with .dev and let you guys know if it resolves my problems.

I confirm I do not have the issue anymore when I'm using *.dev addresses.
I guess we could tweak into OSX Lion in order to use *.local hostnames without 5s latency, but I think it will be wiser to use another extension.

Contributor

Awesome. Good to know it's not our fault. Thanks for heads up guys! @Marak, you won internets for finding it!

@mmalecki mmalecki closed this Feb 14, 2012
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment