chttpserver · Section 3
CHTTPSVR_CONFIG_DEFAULT
NAME
CHTTPSVR_CONFIG_DEFAULT - initializer for chttpsvr_config with reasonable defaults
LIBRARY
C Collections (libccollections, -lccollections)
SYNOPSIS
#include <ccollections/chttpserver.h> chttpsvr_config cfg = CHTTPSVR_CONFIG_DEFAULT;
DESCRIPTION
Expands to a compound literal that initializes a chttpsvr_config
with these defaults:
host = NULL (dual-stack, every interface;
"0.0.0.0" = every IPv4 interface only)
port = 8080
max_body_size = 4 MiB (0 = no limit)
read_timeout_us = 60000000 (60 s; 0 = no idle timeout)
idle_timeout_us = 60000000 (60 s; 0 = use read_timeout_us)
stream_read_timeout_us = 30000000 (30 s; 0 = wait forever)
max_body_read_duration_us = 300000000 (5 min; 0 = no cap on total
read time)
response_write_timeout_us = 0 (0 = use the value of
stream_read_timeout_us)
max_response_write_duration_us = 300000000 (5 min; 0 = no cap)
max_header_read_duration_us = 30000000 (30 s; 0 = no cap)
max_header_bytes = 0 (0 = library default, 64 KiB)
max_connections = 16384 (0 = unlimited)
enable_keepalive = false
enable_reuseport = false
ipv6_only = false
tls = NULL
worker_thread_count = 0 (0 = CPU core count)
worker_queue_capacity = 0 (0 = 1024 * worker_thread_count)
min_transfer_rate_bps = 240 (0 = 240; CHTTPSVR_NO_RATE_FLOOR = off)
min_transfer_rate_grace_us = 5000000 (5 s; 0 = 5 s)
max_partial_body_memory = 256 MiB (0 = 256 MiB;
CHTTPSVR_NO_MEMORY_CAP = no limit)
body_memory_wait_timeout_us = 30000000 (30 s; 0 = 30 s;
CHTTPSVR_NO_DEADLINE = none)
streaming_thread_count = 0 (0 = worker_thread_count;
CHTTPSVR_STREAMING_POOL_OFF = share the
worker pool)
streaming_queue_capacity = 0 (0 = 4 * streaming_thread_count;
CHTTPSVR_QUEUE_UNBOUNDED = no limit)
streaming_queue_timeout_us = 5000000 (5 s; 0 = 5 s;
CHTTPSVR_NO_DEADLINE = none)
Every timeout and duration field is a uint64_t count of
microseconds. The server keeps each one in whole milliseconds and
rounds a value up to the next millisecond, so a value above 0 never
reads as 0. A value longer than the server can keep saturates to the
longest finite limit that it keeps for that field, which is never
shorter than 24 days, and it never turns into 0 or into "no limit".
In the two fields that accept it, CHTTPSVR_NO_DEADLINE is the only
value that means no limit.
All the timeouts and resource limits above are finite, so a server
started from these defaults is already protected against two kinds
of peer: One that opens a connection and then stalls, and one that
sends a request body, or reads a response, one byte at a time. Each
limit can be changed independently. For the fields up to ipv6_only, 0
keeps its documented meaning for a caller that asks for it on
purpose. The fields from min_transfer_rate_bps to
streaming_queue_timeout_us read 0 as "use the default", so a
configuration built with {0}, or with designated initializers that
leave them out, keeps these defenses, and each of them is turned off
only through its own named constant.
host follows the listener rules of the Go net package. NULL or ""
listens on every interface with one dual-stack socket on [::], which
also accepts IPv4 clients through v4-mapped addresses. On a host
without IPv6, and on a system without dual-stack sockets such as
OpenBSD, it listens on 0.0.0.0 instead and so accepts no IPv6 client.
A literal address listens on exactly that address. A host name
listens on its first IPv4 address if it has one and on its first IPv6
address otherwise, so "localhost" listens on 127.0.0.1.
host may also be a "unix://path/to/socket" string, which binds a Unix
domain socket at that path instead of a TCP listener, and port is
then ignored. A socket file at that path on which no process listens
any more is replaced. When the path names anything that is not a
socket, or a socket on which a live server listens, the start fails
and leaves the file alone.
A port of 0 binds an ephemeral port that the system picks on every
start, and chttpsvr_bound_port(3) reports it. enable_reuseport has no
effect with port 0.
read_timeout_us and idle_timeout_us are applied by an idle-timeout
sweep thread of this module. There is one such thread for the whole
process, shared by every chttpsvr instance, and no OS timer per
connection. The two fields bound how long a connection can sit idle
between requests, that is, while it waits for the headers of the
next pipelined or keep-alive request. They don't bound how long a
request can take while a worker thread handles it; that is the job
of max_body_read_duration_us.
stream_read_timeout_us bounds the gap between one batch of body bytes
and the *next* while the server reads a request body, buffered or
streaming. It is separate from read_timeout_us and idle_timeout_us,
which reset on any activity of the connection and so can't stop a
client that sends its bytes just fast enough to always beat them.
Once stream_read_timeout_us expires, chttpsvr_req_read returns -1 and
chttpsvr_req_stream_error reports ccol_timed_out, and a buffered route
gets a 408 from the server.
stream_read_timeout_us also resets on every new byte, so a client
that sends a byte or two just before each gap expires gets past it in
the same way as past read_timeout_us and idle_timeout_us.
max_body_read_duration_us closes that hole with a separate cap on the
*total* wall-clock time that the server spends reading the body of
one request, however many gaps that took. Its default of 300000000
(5 minutes) is large enough for an upload of max_body_size bytes over
a link so slow that the transfer itself is the bottleneck. When it
expires it reports the same ccol_timed_out as an expiry of
stream_read_timeout_us.
A value of 0 in stream_read_timeout_us, max_body_read_duration_us,
response_write_timeout_us or max_response_write_duration_us means
"wait forever". An unresponsive connection still can't hold the
server for good with such a value: A shutdown of the server finishes
in bounded time even when a streaming handler really is blocked
reading such a connection. A shutdown here is a call to
chttpsvr_destroy or to chttpsvr_engine_stop, while a restart with
chttpsvr_stop and chttpsvr_start waits for nothing and cuts nothing
off. The server gets that bound by forcibly closing the connection
once its own bounded, graceful wait for the in-flight requests is
over. This happens only as part of the shutdown and never affects a
request that is still making progress.
response_write_timeout_us bounds the gap between two writes that make
progress while the server sends one response to a client that reads
slowly, so it is the write side of stream_read_timeout_us. Progress
means that the client takes bytes of the response: A send queue of
the socket that shrinks, which the sweep checks about once a second,
counts as much as a write that the kernel accepts. A client that
reads a large response steadily never hits this limit, however large
a send buffer the kernel gives the socket, and a client that stops
reading is cut off between one and two sweep intervals after the
limit passes. For a TCP client, min_transfer_rate_bps judges the
bytes that reached the client and not the bytes that the kernel
accepted.
max_response_write_duration_us is the write side of
max_body_read_duration_us. response_write_timeout_us bounds only
each single call that works like write(2), so a client that reads a
byte or two just before each such call times out can keep a response
going for as long as it likes. max_response_write_duration_us puts a
separate cap on the *total* wall-clock time that the server spends
sending one response, however many calls that took. Its default is
300000000 (5 minutes), for the same reasons as the default of
max_body_read_duration_us. When it expires the send of the response
fails and the server closes the connection, so the client sees a
truncated response or a reset.
Two more writes are always bounded by a small internal ceiling of 2
seconds, whatever you set here, 0 included: A courtesy rejection
response (400, 404, 405, 408, 413, 500, 501, 503 or 505) that the
server generates itself rather than a handler, and the interim
"100 Continue" line for "Expect: 100-continue". Both have a fixed,
small shape, and neither has a good reason to take longer. Neither
holds a thread while it waits for the peer, because a write that
meets a full socket parks the connection and the ceiling bounds how
long it stays parked. The interim line of a streaming route is the
exception: chttpsvr_req_read writes it on the handler's thread, which
waits for the client there just as it waits for the body, and the
ceiling bounds that wait. A value here below that ceiling still
applies in full.
max_header_read_duration_us is the header-phase counterpart of
max_body_read_duration_us. read_timeout_us and idle_timeout_us
measure the gap between one piece of activity and the next, and every
byte that arrives resets them, so a peer that keeps sending its
request headers or its TLS handshake one byte at a time, forever,
without completing them, gets past both. It never gets near
max_header_bytes either, because it never finishes the header block.
max_header_read_duration_us puts a separate cap on the *total*
wall-clock time that one connection can spend in the phase of one
request that the reactor owns, which is the TLS handshake, if there
is one, plus the read of the whole header block. When it expires the
server closes the connection without a response, because the header
block was never complete enough to answer.
The budget is per request and not per connection. It starts at the
first TLS handshake step for a connection that has one, and otherwise
at the first byte of the header block of the request. The next
request of a keep-alive connection gets a fresh budget when its own
first byte arrives, so the time a connection sits idle between two
requests is never charged to either of them. The expiry is noticed
by the shared idle-timeout sweep thread, as for read_timeout_us and
idle_timeout_us. That sweep runs once a second and closes every
expired connection, however many there are, so a connection past its
deadline can live up to about a second longer.
The default of 30 s is generous next to any legitimate client, which
sends its header block right after it connects: Even a full TLS
handshake plus several kilobytes of headers over a lossy, high-latency
link takes only seconds. Raise it for a deployment whose clients
really are slower than that, and set it to 0 to turn the limit off
when something in front of this server already bounds the same phase.
max_header_bytes bounds the combined size of the header block of one
request, which is the request line plus every header line. A request
whose headers go past it gets 400 Bad Request and the connection is
closed, the same way the request parser of this module reports every
other malformed request before routing. The answer is best effort,
and a peer that is still streaming its oversized header block when
the server closes may hit a reset before it reads anything. Two
fixed limits apply next to it, whatever its value, with the same 400:
the request line and each header line must end within 8 KiB, CRLF
included, and a request holds at most 100 header lines.
chttpsvr_start(3) lists the other requests that the server refuses
before routing.
max_connections bounds how many connections this listener can have
open at once. With a finite value one listener can hold only so many
file descriptors, so a peer that opens connections faster than the
idle sweep frees them can't use up the descriptor limit of the
process and take every other listener down with it. Raise it for a
deployment that really serves more simultaneous connections than the
default, and raise RLIMIT_NOFILE to match. At capacity new
connections stay pending in the kernel's listen backlog instead of
being accepted and then rejected, because the server doesn't call
accept(2) again until a connection closes and frees a slot. The
listener stops accepting completely while it is at capacity and
resumes on its own once a slot frees, with a worst case of about one
second between the slot being freed and the listener noticing it.
enable_keepalive sets SO_KEEPALIVE on every connection that the
server accepts. enable_reuseport sets SO_REUSEPORT on the listening
socket, which lets more than one chttpsvr instance, for example one
per worker process, bind the same host and port so that the kernel
spreads the accepts over them. ipv6_only sets IPV6_V6ONLY on an
AF_INET6 listener, so that it doesn't also accept IPv4-mapped
connections, and makes a host name listen on its IPv6 address when it
has one. With ipv6_only off, a NULL or "" host is dual-stack wherever
the system allows it, and an IPv6 address or name keeps the system
default. enable_keepalive and enable_reuseport are best effort: A
failed setsockopt does not make chttpsvr_start fail. None of the
three has any effect on a Unix domain socket listener.
On a buffered route no worker thread waits out any of these read and
write limits. A body that stops arriving, or a response that meets a
full socket, parks its connection without a thread, and a later
worker continues it once the socket is ready. The sweep thread
applies the four limits above to a parked connection once a second,
and the server applies them again when the connection becomes ready.
A streaming handler reads its body on its own thread, where the
limits bound that wait directly.
min_transfer_rate_bps is the floor on the average rate of a body read
and of a response write. The rate is measured from the start of
each, and judged once min_transfer_rate_grace_us of it has passed.
It closes a client that stays just inside every gap limit while
moving almost nothing: A body below the floor gets 408 and a close, a
response below it is cut off and closed, and chttpsvr_req_read() of a
streaming handler fails with ccol_timed_out. 240 bytes a second
after 5 s is the minimum data rate of Kestrel and of IIS. Time in
which the server itself holds a request back, while it waits for body
memory or sits in the streaming queue, never counts against the
client.
max_partial_body_memory caps the memory that the bodies of buffered
requests on this server hold together, from the first byte that the
server reads until the handler returns. A reservation covers the
memory that the buffer of a body really holds and lasts as long as
that buffer. The server frees a body, and gives its reservation
back, as soon as the handler returns and before the response goes
out, so a response that waits for a slow reader holds no body, and a
request that is refused, cut off or closed frees both at once. A
body reserves its buffer as its bytes arrive. A body with a
Content-Length reserves a first buffer of at most 16 KiB before the
server reads it, every body reserves each growth of its buffer before
the read that needs it, and a growth reserves the old buffer next to
the new one until the copy is done. The buffer doubles as it grows
and never grows past the declared length, so a client that declares
a large body and sends little of it holds little memory. A body
whose first buffer doesn't fit waits without a thread: Nothing reads
it and no "100 Continue" invites it until memory frees, and a later
growth that doesn't fit waits in the same way. Waiters go ahead in
the order in which they began to wait. When every body that holds
memory is itself waiting for more, the oldest waiter goes past the
limit, one request at a time, so the limit is passed by less than
twice max_body_size and the server never deadlocks. A body whose
handler is running is not waiting, so a request that a handler waits
for, and whose body doesn't fit next to the bodies that running
handlers hold, waits until its deadline. A waiter that reaches
body_memory_wait_timeout_us gets 503 with "Retry-After: 5" and a
close.
streaming_thread_count sizes the pool that runs streaming handlers,
which the server creates at the first streaming request. Slow
streaming clients can take up at most that pool and never the threads
of buffered routes. A streaming request that finds every streaming
thread busy waits in a queue of streaming_queue_capacity entries
without holding a thread. A full queue answers 503 with
"Retry-After: 5" at once, and a request that waits longer than
streaming_queue_timeout_us gets the same answer.
CHTTPSVR_QUEUE_UNBOUNDED in worker_queue_capacity or
streaming_queue_capacity creates a queue with no capacity limit.
After the macro, change one field at a time:
chttpsvr_config cfg = CHTTPSVR_CONFIG_DEFAULT;
cfg.port = 9090;
cfg.worker_thread_count = 8;