C Collections

chttpclient · Section 3

chttpcli_do_async

chttpcli_do_async - submit an HTTP request for asynchronous execution

C Collections (libccollections, -lccollections)

#include <ccollections/chttpclient.h>
 
cthrpool_future *chttpcli_do_async(chttpcli cli,
                                     const chttp_request *req);

This is the non-blocking counterpart of chttpcli_do. It queues req
on chttpclient's shared reactor engine and returns at once; the library
starts that engine the first time it is needed. The whole request and
response cycle, every redirect hop included, runs on the engine's
threads.
 
The engine has a small pool of reactor threads plus a companion worker
pool for name lookups, which block, both sized to the CPU count. A
request to an address literal or to a unix socket makes no lookup and
never waits for that pool, so lookups that hang delay none of those
requests. A request whose lookup outlives its connect or request
timeout ends with ccol_timed_out at its deadline while the lookup goes
on in the background. Until the lookup returns, chttpcli_destroy of
that client waits for it, and the exit handler described below counts
it as a request in flight. The same holds for a request whose deadline
passes while the engine cannot run its work (see
chttpcli_set_request_timeout(3)). The engine starts on the first
call to chttpcli_do_async or chttpcli_do_async_streaming anywhere
in the process, and stops again when no request is in flight and no
connection is left in the async idle pool of any chttpcli. The stop
runs on a thread of its own, because the last reference often goes
away inside a reactor callback and the teardown joins the very threads
that run such callbacks. If the process can't provide that thread,
because of a ceiling on threads or processes such as RLIMIT_NPROC or a
cgroup pids.max, the engine stays up and the library retries the stop
every 100 ms until a thread is available; a call in the meantime reuses
the engine.
 
There are two supported ways to use fork(2) with this engine:
  - Call fork(2) before you create a client, so that each process
    makes its own clients and, on first use, its own engine.
  - Call fork(2) and then call exec(3) in the child immediately, as
    popen and system do.
A child that carries on without exec(3) while a client created before
the fork is live is not supported, because fork(2) does not copy the
engine's threads.
 
A process can destroy every client that used Tier 2 or Tier 3 before
it forks, and the child can then use both tiers and starts its own
engine on first use. This works even if the parent's engine had not
finished stopping at the time of the fork. See chttpclient(7).
 
The engine is completely separate from the synchronous connection work
of chttpcli_do. It is also independent of chttpserver's reactor,
which sits on ceventloop, so one process can run chttpserver
together with chttpcli_do_async and chttpcli_do_async_streaming.
Stopping one engine, whether with chttpsvr_engine_stop() or through
this engine's automatic teardown, has no effect on the other.
 
Unlike with chttpcli_do, req does not have to stay valid after this
call returns, because the library copies or serializes everything it
needs before returning.
 
A server can send interim informational responses with a 1xx status.
The client drops each of them and keeps reading on the same connection
for the real response, so the future is never fulfilled with a 1xx
status as the final answer. RFC 8297's "103 Early Hints" is the most
common example in practice. "101 Switching Protocols" is the
exception: This client never asks for an upgrade, so a 101 fulfills
the future with ccol_http_transfer_aborted, as in chttpcli_do. A
response that arrives in the same read as a "100 Continue" is read
before any byte of the body goes out, as in chttpcli_do(3). A
request with req->expect_continue set sends "Expect: 100-continue"
(see
chttp_request_create_mp(3))andholdsthebodybackuntil
the server's interim response, exactly as chttpcli_do does. That
wait is part of this function's non-blocking dispatch and never a
blocking call. The client does not accept an endless stream of interim
responses either: Once it has dropped 64 interim responses in a row on
one hop, the future is fulfilled with ccol_http_transfer_aborted
instead of waiting for ever. The cap counts separately in two phases,
the wait for "100 Continue" and the read of the final response after
it. A "100 Continue" is a real outcome and so is the end of the wait
without a response; neither counts as a dropped response, and each one
resets the count to zero. So a hop that uses req->expect_continue can
drop up to 64 interim responses while it waits for "100 Continue" and
up to 64 more while it reads the final response, and the total is not
64.
 
Two more cases behave differently. The first is a request header whose
value holds a byte below 0x20 other than a horizontal tab (CR and LF
among them) or 0x7F, and the same for body.content_type on a request
with a body. The second is a request that carries a body (POST, PUT or
PATCH) and whose caller-set Content-Length header holds a bad value. A
valid value is a plain unsigned string of decimal digits, with no
leading '+' or '-' and no whitespace in it, and it must also match the
real length of the body. The library can only find these two problems
when it serializes the request for the wire, which happens after the
future exists; a malformed URL and an unusable TLS configuration are
checked earlier, up front. So such a request gives a future that
is not NULL, and its result holds rv == ccol_invalid_args instead of a
response. See chttpcli_async_result_get(3). chttpcli_do returns
the same code synchronously for the equivalent request, and
chttp_request_set_header(3) and chttpcli_do(3) explain why the
client rejects it.

cli The client handle. It must not be CHTTPCLI_NULL.
req The request to run. It must not be NULL. Unlike with
    chttpcli_do, req does not have to stay valid after this call
    returns, because the library copies everything it needs before
    returning.

A future, or NULL if the request could not even be queued. That is
the case for a NULL req, for a cli that is CHTTPCLI_NULL and for a
stale cli of a client that has been destroyed, and also for a
malformed URL, for TLS that cannot be used, when memory runs out and
when the engine fails to start. After a success the caller owns the
future and must call chttpcli_async_result_get, then
chttpcli_async_result_free, and then exactly one cthrpool_future_free.

The library leaves the SIGPIPE disposition to the application and
never changes it, in any tier. No write of the client raises that
signal, because every socket write, TLS records and the close_notify of
a TLS teardown included, is a send(2) with MSG_NOSIGNAL. A server that
closes or resets the connection while the client writes a request makes
that request fail and never ends the process.
 
The engine stops its threads before the process exits. The library
registers a handler with atexit(3) after OpenSSL's own, so that it
runs first. That handler waits for a stop in progress, destroys the
default client when no call is running on it and stops an engine that
nothing holds, so that no engine thread is left to race OpenSSL's
cleanup. Two cases keep the engine running at exit, and the handler
then does nothing: A request in flight (this includes an exit() from
inside a write_fn and a request whose lookup is running), and a client
that the application never destroyed, whose pooled connections hold
the engine. Destroy every client before the process returns from main.
 
The engine stops on its own thread after the last client lets go of
it. chttpcli_engine_wait blocks until that stop is complete, and an
application needs that before it releases the allocator of
chttpcli_set_engine_mem_mgmt_procs or the logger of
chttpcli_set_engine_logger.

chttpcli_do(3), chttpcli_do_async_streaming(3), chttpcli_do_pooled(3), chttpcli_async_result_get(3), chttpcli_async_result_free(3), chttpcli_engine_wait(3), chttpcli_set_engine_logger(3), chttpcli_set_engine_mem_mgmt_procs(3), cthrpool_future_get(3), cthrpool_future_free(3), chttpclient(7)