Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

first request is always slow #171

Closed
s1985 opened this Issue · 15 comments

6 participants

s1985 Marak Jérémy Faivre Maciej Małecki Einar Otto Stangvik Brian Panulla
s1985

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)

Marak

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.

s1985

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);

Jérémy Faivre

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.

Maciej Małecki

Bumping priority to medium.

Jérémy Faivre

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.

s1985

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.

Maciej Małecki mmalecki was assigned
Maciej Małecki

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.

Einar Otto Stangvik

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

Brian Panulla

Something triggering IPv6 name resolution?

Einar Otto Stangvik

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

Jérémy Faivre

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

s1985

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

Jérémy Faivre

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.

Maciej Małecki

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

Maciej Małecki mmalecki closed this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.