C Collections

chttpclient · Section 3

chttpcli_destroy

chttpcli_destroy - destroy an HTTP client and invalidate the handle

C Collections (libccollections, -lccollections)

#include <ccollections/chttpclient.h>
 
chttpcli_destroy(cli)

This macro waits until every request in flight has finished, then frees
every resource and sets cli to CHTTPCLI_NULL.
 
"Every request in flight" covers all three tiers: A synchronous
chttpcli_do or chttpcli_do_streaming call running on another
thread, and a request from chttpcli_do_async,
chttpcli_do_async_streaming, chttpcli_do_pooled or
chttpcli_do_pooled_streaming for as long as the shared reactor
engine is working on it. An idle connection in the pool does not
count. The macro is safe even if nobody has waited yet on a
cthrpool_future from chttpcli_do_async or
chttpcli_do_async_streaming.
 
cli must be a live handle, or CHTTPCLI_NULL, which is a silent
no-op. Three other kinds of value are a FATAL ERROR: A stale handle that an
earlier call destroyed, a stale handle that another thread is
destroying at the same moment, and a forged or garbage value. In each
of those cases the macro calls abort() and raises SIGABRT, which is
safer than risking a silent double free or a use-after-free.
 
Destroying cli from inside a request of that same client is a FATAL
ERROR too. A call on cli from the write_fn of chttpcli_do_streaming,
chttpcli_do_async_streaming or chttpcli_do_pooled_streaming would
wait for ever for the very request that runs it, so the macro calls
abort() instead, with a message that names the misuse.
 
Destroying ANY client on an async engine thread, that is, from the
write_fn of chttpcli_do_async_streaming or
chttpcli_do_pooled_streaming, is also a FATAL ERROR. The destroy
waits for that client's Tier 2 and Tier 3 requests and for the
teardown of its idle connections, and the engine threads are the ones
that run that work. With one or two reactor threads, the thread that
would do the work is the one that waits, so the destroy would never
return. Destroying another client from the write_fn of
chttpcli_do_streaming, which runs on the caller's thread, is an
ordinary destroy.
 
When the last client that used Tier 2 or Tier 3 is destroyed, the
engine stops on its own thread, and chttpcli_engine_wait waits for
that to finish.

cli   An lvalue of the type chttpcli.

The macro evaluates cli exactly once. cli must be a modifiable lvalue,
such as a variable or an array element, and an expression with a side
effect such as handles[i++] destroys and clears exactly one element.

chttpcli_create(3), chttpcli_engine_wait(3), chttpcli_do_streaming(3), chttp_default_client(3)