Skip to content

Very slow request #7

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

Open
ptorrent opened this issue Apr 10, 2025 · 3 comments
Open

Very slow request #7

ptorrent opened this issue Apr 10, 2025 · 3 comments

Comments

@ptorrent
Copy link

ptorrent commented Apr 10, 2025

Hello,

I was facing an issue on node 20 with the proxy. Very slow proxy. I found the solution here:

I changed this in common.js setupOutgoing :

  //REMOVE THIS LINE outgoing.agent = options.agent || false;
  //AND REPLACE WITH:
  if(options.agent){
	outgoing.agent = options.agent 
  }

Regarding the doc, setting the agent to false seems to create a new one each time...

Image

I'm not a HTTP agent expert, but does it make sense to create a new one each time ?

After that change, proxy are really really fast... 10x time faster.

@Jimbly
Copy link
Owner

Jimbly commented Apr 10, 2025

Sharing an agent will share the TCP connections to the server. This means that multiple different clients hitting the proxy will share the same TCP connection back to the server (possibly sending their requests interleaved), which, depending on your back end, may have significant security implications (e.g. if you use middleware that only does authorization checks on a connection once and caches the response). I think you can get this performance on the user-end by just passing in an agent parameter (probably just new http.Agent() or something), presumably that's why that's in the options.

Is your back-end HTTPS (not HTTP) by any chance? HTTPS has a huge per-connection overhead, but an HTTP backend should be relatively fast even with a new connection/agent each time (although, of course, still slower than not doing extra work). As long as your proxy-to-backend traffic flows only through a private network, you can avoid using HTTPS.

@ptorrent
Copy link
Author

ptorrent commented Apr 11, 2025

Thanks for your answer. This is my code :

var httpProxy = require('http-proxy');

 var fs = require('fs')
 var path = require('path')
var proxy = httpProxy.createProxyServer(); 
var serv =  require('https').createServer({
    key: fs.readFileSync(path.join(__dirname, "privkey.pem")),
    cert: fs.readFileSync(path.join(__dirname, "cert.pem")),
  },function(req, res) {
	proxy.web(req, res, { 
		  target: {
			protocol: 'https:',
			host: '192.0.16.23',
			port: 8888,
		  },
		  secure : false
	});

});
serv.listen(8888)

remote certifcates are not signed, and local too. Seems faster when https -> http, but https -> https is just very slow. And there is not a lot of request... I'm the only one using this (localy by setting 127.0.0.1:8888 in the browser).

Do you know the difference between:

req.agent = undefined 
and
req.agent = false 

Because it's the only think I change in common.js

@Jimbly
Copy link
Owner

Jimbly commented Apr 11, 2025

Yeah, I'm not surprised https -> https proxying is slow, but there's no need for it in most cases (if you're using a proxy, then probably the back end is not exposed to the internet, so then there's no reason to use https between two of your own private services that aren't exposed to the internet).

I think req.agent = false ensures not using an agent / not re-using connections for subsequent requests (needs to be the default here), req.agent = undefined will use the default behavior (which, on the current Node versions, I think uses a single global agent for the process by default). If you know your back-end doesn't cache anything at the connection level and you want to re-use connections, then you should just be able to pass an agent in, from what I'm understanding from glancing at the code you posted above (I didn't write this, I just maintain the Node 16+ bug fixes 😆):

var httpProxy = require('http-proxy');

var fs = require('fs')
var path = require('path')
var proxy = httpProxy.createProxyServer(); 
var agent = new require('https').Agent();
var serv =  require('https').createServer({
    key: fs.readFileSync(path.join(__dirname, "privkey.pem")),
    cert: fs.readFileSync(path.join(__dirname, "cert.pem")),
  },function(req, res) {
	proxy.web(req, res, { 
		  target: {
			protocol: 'https:',
			host: '192.0.16.23',
			port: 8888,
		  },
		  secure : false,
                  agent: agent,
	});

});
serv.listen(8888)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants