C Collections

chttpserver · Section 3

chttpsvr_set_engine_num_reactor_threads

chttpsvr_set_engine_num_reactor_threads - set the OS thread count for the shared engine's reactor

C Collections (libccollections, -lccollections)

#include <ccollections/chttpserver.h>
 
ccol_retval chttpsvr_set_engine_num_reactor_threads(size_t num_threads);

Sets how many OS threads the shared ceventloop reactor uses for
its own polling and dispatch, with the meaning of the
num_reactor_threads parameter of ceventloop_create_mp (see
cthreadcomm.h).  With 1, one thread polls and also dispatches inline.
With a larger value there is one dedicated polling thread plus
(num_threads - 1) dispatch worker threads.
 
The reactor uses exactly 1 thread by default, which is what you get
if you never call this function or call it with num_threads == 0.  A
larger num_threads starts that many OS threads instead, and 0 puts
the default of 1 back.
 
The reactor reads this value when it is constructed, as it does for
chttpsvr_set_engine_mem_mgmt_procs, so you can call this function
only before the first chttpsvr_start of the process.  It may be
called again after the reactor has stopped completely, which means
after chttpsvr_engine_wait returns, and before the next
chttpsvr_start.

ccol_success on success.
ccol_not_permitted when the reactor is already running.  Stop it
                  first.

The default is 1 and not a detected CPU count, because 1 is faster
for the common traffic:
 
  Plain HTTP on reused connections, which is what a browser or an
  API client usually sends, has its best throughput with one thread.
  Header parsing is fast, so the dispatch phase of plain HTTP has
  almost no CPU-bound work, and more threads only add the cost of a
  hand-off with nothing to run in parallel.
 
  TLS on reused connections, which is typical HTTPS traffic once the
  connection pool of a client comes into play, is a real trade-off
  either way.  One thread gives lower throughput but a clearly better
  and steadier p99 latency than several dispatch threads.  Most
  requests on a TLS connection take the same cheap path as plain
  HTTP.  Only the handshake of the connection is expensive, and that
  cost is spread over every request the connection then serves.
 
  TLS with no connection reuse at all, where every request pays for
  a new handshake, gets both higher throughput and lower p99 latency
  from more than one dispatch thread.  This is an extreme case on
  purpose and not typical traffic.  The asymmetric-crypto cost of a
  TLS handshake is the private-key operation of the server, which is
  CPU-bound work that gains from being spread over several cores when
  there is enough of it.
 
A larger num_threads is worth its cost in one case only: A steady
churn of TLS connections, where many separate clients open a
connection for one request or a few and then close it, so that a
large part of the traffic pays the CPU cost of a handshake instead of
spreading it out.  Some
deployments do look like that: A public API with many one-off
anonymous clients, a gateway for IoT devices whose unreliable
networks reconnect often, or a webhook receiver that many different
external services call.  But it is the less common shape.  Most
HTTP client software (browsers, most serious HTTP client libraries
and the chttpclient of this library) pools and reuses connections to
avoid paying for the handshake again, and a server behind a load
balancer or a reverse proxy often never sees raw handshake churn
from the public internet at all.  A deployment that knows its own
traffic churns a lot should raise num_threads, and this function is
there for that.

chttpsvr_set_engine_mem_mgmt_procs(3), chttpsvr_set_engine_logger(3), chttpsvr_start(3), chttpsvr_engine_stop(3), chttpsvr_engine_wait(3), chttpcli_set_engine_num_reactor_threads(3)