virtkick/http-master

Is there a way to set maxSockets? I want to enable Keep Alive connections

Closed this issue · 11 comments

I would like to enable keep alive and I've seen following solution for http-proxy: http-party/node-http-proxy#767. I wonder if it is a similar solution for http-master and how could it be achieved?

Thank you

Rush commented

Yeah, you are welcome to try messing with https://github.com/encharm/http-master/blob/master/modules/middleware/proxy.js - you mean keep alive between proxy and target server, right?

Correct. Thank you, I'll take a look.

Rush commented

@szarouski let me know your results. Perhaps it should be supported OOTB.

Seems to be working fine if I do following:
add var http = require('http'); at the top
And adjust proxy to include agent:

var agent = new http.Agent({ maxSockets: Number.MAX_VALUE });
var proxy = httpProxy.createProxyServer({xfwd: true, agent: agent});

I wonder if that should be configurable? I don't know how options could be passed into middlewares.

I agree with you that it will be nice to have it OOTB. Let me know what should be next steps to make it happen :)

Rush commented

Just trying to think of disadvantages of this solution. I think the risk is that the server may more easily run out of available sockets. Have you done any benchmarking?

Here is some feedback. I used http-proxy with agent created like above and I setup a load test using loadimpact.com with maximum at 250 connections. I was able to see that number of occupied sockets was growing over time. Once load test finished sockets state changed to TIMED_WAIT. After 1 minute I could see that sockets on my linux machine were closed (indeed tcp_fin_timeout is 60). ulimit on my linux box was set to unlimited. From other comments I can see that TIMED_WAIT and number of available sockets is depending on machine configuration. It seems that for default linux configuration there is a limit of 470 sockets per second. So... I think that it would be nice to be able to configure agent settings. What do you think?

Also something that I forgot to mention - it is not a maxSockets value which is making connection to change to 'keep-alive'. It is enough to just have http.Agent instance. I tried to debug core http module, but stuck on what exactly is causing it to pick keepAlive setting even when it is not set to true on the agent instance. (Note: maxSockets were needed for node < v0.11.6 because of default limit to 5, for 11.6+ maxSockets limit changed to Infinity).

Hi @Rush, I'll have some free time soon and I wonder if you can point me to what you think is the best way to define agent options and if there are any similar examples in the code where http-master takes configuration options. I'll make a PR once I'm done. Thank you sir.

Rush commented

Hey @szarouski, thanks for the offer. Actually I would rather appreciate some more tests to get a good default option. To answer your question, http-master uses dependency injection:

https://github.com/encharm/http-master/blob/master/modules/middleware/proxy.js#L32

Change it to:

module.exports = function ProxyMiddleware(config, portConfig, di) {

and you have access to global config hash, where you could have config.maxSockets

Just note that module.exports for middleware will run once per port and for every worker. If something is meant to run only once per worker it would have to be a service but to just set something simple I wouldn't do a service. :)

Thanks!

Proxy improvements, including agent settings landed here: #103. I'll try to contribute more when I have time. I'm not certain what editor you're using, but there is a great tool for realtime javascript development and testing called wallaby.js, which is available for WebStorm, and soon for other editors. It was easy to get it working with your existing testing tools.

http-master
Open in new window

@szarouski That's huge. Thanks for that. We'll both use it in our RubyMine.