[Libwebsockets] Max connections

Andy Green andy at warmcat.com
Thu Oct 13 23:59:23 CEST 2016

On October 14, 2016 5:22:02 AM GMT+08:00, Srikar Vadlamani <SrikarVadlamani at trafficware.com> wrote:
>I am using lws for its http functionality. Only have 1 protocol
>I want to only allow n connections due to limited memory.
>I do not want to close the connection, but keep it in pending/wait
>state, as a browser when loading a webpage with css and other js will
>request multiple connections at once,
>If I close the connection webpage will fail to load properly.

It sounds like something else is the problem.

>I am currently using lws_http_mount to serve static pages. 
>I respond to any POST requests in the http_callback.
>After every LWS_CALLBACK_HTTP_FILE_COMPLETION event, I can return (-1)
>to close the connection. The logs show that connection is closed, but

That's not what you usually want to do.  Browsers use http/1.1 keepalive / header pipelining, instead of simply killing the connection when you finished one http transaction, you should take this kind of approach


	if (lws_http_transaction_completed(wsi))
		return -1;

return 0;

If the browser didn't want to keep the connection alive, you close it.  Otherwise you informed lws the http *transaction* completed, the http header parsing state of the connection is reset and it waits for new headers / a new http transaction on the existing connection.

>netstat shows me it is in TIMED_WAIT state.
>What is the best way to know number of open/pending http connections?
>Instead of closing the connection is there a way to put that connection
>is wait state, if it exceeds a certain number.
>Can I expect LWS_CALLBACK_CLOSED_HTTP every time a socket is closed?

I think you might need to look closer at the problem and assumptions it is about "''limited memory" or you need to tightly count connections to solve it; you might need to overall but not the the point that one client can't get all his page elements.  Lws can do that on ESP8266 and he's insanely resource-constrained, 80KB SRAM for everything including an 80211 stack!

Support keepalive as above may change the situation.


>-----Original Message-----
>From: Andy Green [mailto:andy at warmcat.com] 
>Sent: Tuesday, October 11, 2016 9:01 AM
>To: Srikar Vadlamani <SrikarVadlamani at trafficware.com>;
>libwebsockets at ml.libwebsockets.org
>Subject: Re: [Libwebsockets] Max connections
>On October 11, 2016 9:47:20 PM GMT+08:00, Srikar Vadlamani
><SrikarVadlamani at trafficware.com> wrote:
>>Is there a way to limit max number of connections?
>You can just refuse connections as soon as they are accepted at the
>network level, if you don't want them to proceed as http[s]/ws[s]
>logical connections, for whatever reason.
>	/**< called when a client connects to
>	 * the server at network level; the connection is accepted but then
>	 * passed to this callback to decide whether to hang up immediately
>	 * or not, based on the client IP.  in contains the connection
>	 * socket's descriptor. Since the client connection information is
>	 * not available yet, wsi still pointing to the main server socket.
>	 * Return non-zero to terminate the connection before sending or
>	 * receiving anything. Because this happens immediately after the
>	 * network connection from the client, there's no websocket protocol
>	 * selected yet so this callback is issued only to protocol 0. */
>Notice though an attacker can lock you out if his connections can be
>accepted first, and you refuse all after the first n connections.  You
>might want to make, eg, connections from always accepted.
>>Libwebsockets mailing list
>>Libwebsockets at ml.libwebsockets.org

More information about the Libwebsockets mailing list