Guiness
Guiness

Reputation: 277

node.js server limiting new connections

I'm using node.js to create a server that is used for sending server-sent events to the clients. I'm running the server on a Windows 7 machine.

The way I connect is by sending HTTP get requests with apache httpclient, establish TCP connection and receive the events from the server. What I notice is that after I connect 11 clients at the same time, the previous connections close.

Is there a limit in Node.js? Or is this system-specific? I've been googling about this and I couldn't find anything.

Here is a sample code from the requests I'm sending:

HttpClient httpClient = HttpClientBuilder.create().build(); 

String domain =    "..."; 

HttpUriRequest httpGet = new HttpGet(domain); String encoding = "..."

httpGet.setHeader(new BasicHeader("Authorization", "Basic " + encoding)); 
httpGet.setHeader(new BasicHeader("Content-Type", "text/event-stream")); 

HttpResponse resp = httpClient.execute(httpGet); 
HttpEntity entity = resp.getEntity(); 

if (resp.getStatusLine().getStatusCode() != 200){ String 
   responseString = EntityUtils.toString(entity, "UTF-8"); 
   System.out.println(responseString); 
}

and I'm doing this many times in my test code.

Thanks for your help!

Upvotes: 2

Views: 3121

Answers (2)

JohnKiller
JohnKiller

Reputation: 2008

Since node js v0.12 the http.globalAgent.maxSockets has been set to Infinity, so it is a client side problem.

This should set the limit for the client side:

http.globalAgent.maxSockets = 1000;

Another option is to pass an Agent object to the http request. Have a look at this other question: How to set maximum http client connections in Node.js

Upvotes: 1

Lucio Paiva
Lucio Paiva

Reputation: 20756

Take a look at this property and see if it helps you:

https://nodejs.org/api/http.html#http_agent_maxsockets

Depending on the version of Node.js you're using, it may be limiting the amount of open connections. Try raising it:

http.globalAgent.maxSockets = 128; // or whatever suits your app

Upvotes: 1

Related Questions