[Libwebsockets] count_threads in client side

Andy Green andy at warmcat.com
Thu Oct 22 18:21:46 CEST 2020



On 10/22/20 4:44 PM, Peiffer Eric wrote:
> Hi,
> 
> My application use libwebsockets 4.1 on debian 10 and handle thousands 
> of web socket connections.
> We have notice that when we approach the 15000 connections de thread 
> that run the message loop take almost 100%  of the CPU.
> This is because client connections receive xmpp ping each minute, not in 
> the same time but may be 1000 connexions receive xmpp ping then 1 second 
> later the next 2000, and so on ,...

Right... it sounds pretty busy.

> In order to decrease the CPU usage is it possible to use a pool of 
> several threads maybe 2 or 3 in order to handle the 15000 client 
> connections. Can I use the count_threads paramaters in the info struct 
> in order to handle more than one thread in the context that handle 
> client connections?
> If the answer is yes how are managed the connections:
> When a connection is opened does it always stay in the same thread?

It's possible to have n event loops, by building with, eg, LWS_MAX_SMP=8 
and following the approach here

https://libwebsockets.org/git/libwebsockets/tree/minimal-examples/http-server/minimal-http-server-smp

As a server, all threads can listen on the same port (at least on 
Linux), the incoming connections check which event loop is the least 
busy, and then migrate to that.

LWS_MAX_SMP > 1 enables various internal lws pthread locking, but you 
will have to adapt your user code to understand that although in a 
particular event loop thread all the lws calls are serialized as usual, 
for multicore or multi-hardware-thread machines the same user protocol 
handler code is in truly simultaneous use by multiple event loop threads 
then.

That means that you must also add locking to protect your data 
structures from simultaneous change from multiple event loops, the 
example shows the general way.

-Andy


More information about the Libwebsockets mailing list