C++ · Calling

Calling

#include <ucall/client.hpp>#include <ucall/types.hpp> 1.0.0-rc.1 · b7577426

Ask your coding agent about this page, or add the docs server to it

curl -s 'https://www.unum.cloud/docs/ucall/cpp/calling.md' | claude -p "How would this fit my code?"

A client_t submits calls without driving the network, and polls to drive them and collect what finished. It holds up to max_concurrent_connections connections, reusing them across calls, and carries up to max_pipelined_calls calls on each.

auto [client, client_status] = uc::client_t::make({.max_concurrent_connections = 4, .max_pipelined_calls = 16});
std::array<std::uint64_t, 64> contexts {};
std::array<char, 64> bodies {};
for (std::size_t index = 0; index != contexts.size(); ++index) {
    contexts[index] = index, bodies[index] = char(index);
    uc::client_call_t const call {.verb = ucall_verb_post_k, .url = "http://127.0.0.1:8545/infer",
                                  .body = {&bodies[index], 1}};
    uc::status_t const submitted = client.submit(call, &contexts[index]);
}
std::array<ucall_completed_call_t, 16> storage {};
for (std::size_t remaining = contexts.size(); remaining != 0;) {
    auto [finished, finished_status] = client.take_completed(storage, std::chrono::seconds(1));
    for (uc::completed_t done : finished) {
        auto [response, response_status] = std::move(done.response);
        std::printf("call %llu answered %u\n", *static_cast<unsigned long long*>(done.context),
                    response_status.failed() ? 0u : unsigned(response.status_code()));
    }
    remaining -= finished.size();
}

auto [health, health_status] = client.get("http://127.0.0.1:8545/health");
auto [cache_control, cache_control_status] = health.header("cache-control");

submit takes a client_call_t, whose URL, header lines and body the caller keeps until the call completes, and tags it with a void* context. A call admitted once completes exactly once, and a client already holding its call budget of unretrieved calls refuses more with status_code_t::contended_k. take_completed drives every admitted call and fills the front of the storage the caller passes with finished ones, waiting up to its timeout for the first, and a zero timeout performs one nonblocking round. It answers a completed_view_t, which hands each completed_t over once by take or by iterating and frees the responses never taken. get, post, put, delete_, patch and call submit one call and wait for it alone, while the library keeps the completions of other calls queued for the next take_completed. client_call_t::options carries the call’s own client_call_options_t: a timeout in place of the client’s, retries, redirect hops, security, and the client and server keys.

Every received response is a response_t, whatever its HTTP status, and only a failed exchange is a failed status. A response_t owns its received bytes, frees them when destroyed, stays readable after its client_t is destroyed and may move to another thread. Destroying the client cancels the calls still in flight and discards completions not yet retrieved.

§ Streamed Response Bodies

Download a body larger than the client buffers by pulling it chunk by chunk as it lands:

uc::client_call_t const call {.url = "http://127.0.0.1:8545/dataset.bin",
                              .options = {.body_delivery = ucall_body_delivery_streamed_k}};
auto [response, response_status] = client.call(call);
std::size_t received_bytes = 0;
for (;;) {
    auto [chunk, chunk_status] = response.body_chunk();
    if (chunk_status == uc::status_code_t::would_block_k) {
        auto [advanced, advanced_status] = client.take_completed({}, std::chrono::milliseconds(1));
        continue;
    }
    if (chunk_status.failed() || chunk.empty()) break;
    received_bytes += chunk.size();
}

A call whose body_delivery is ucall_body_delivery_streamed_k completes once its head arrived, so a download larger than max_response_bytes is pulled through response_t::body_chunk as it lands, while response_t::body reads empty. A pull never waits: with nothing decoded yet it answers status_code_t::would_block_k, and an empty take_completed advances the client, moving the next bytes in. Every streamed response is pulled to its end or destroyed, as its connection carries no other call meanwhile; destroying it early closes that connection and fails the calls pipelined behind it.

API

Types

client_options_tWhat a client may hold open at once, how long it waits, and who it is under Noise; zero fields take their documented defaults, except the connection budget, which is required.
client.hpp:27 ↗
struct client_options_t

Fields

max_concurrent_connectionsstd::size_tConnections open at once; zero is refused.
max_pipelined_callsstd::size_tCalls one connection may carry at once; zero means one.
connect_timeoutstd::chrono::microsecondsTime budget for establishing a connection; zero means 5 seconds.
call_timeoutstd::chrono::microsecondsTime budget for a full exchange, queueing and redirects included; zero means 30 seconds.
noise_private_keyucall_noise_key_t const *The client identity for Noise handshakes.
peer_authorizerpeer_authorizer_tVets each server's authenticated key, needed by Noise XX calls.
loglog_tWhere log lines go; empty keeps the client silent.
max_concurrent_callsstd::size_tCalls admitted at once, unretrieved completions included; zero means connections times pipelined calls.
backenducall_io_backend_tThe requested I/O backend.
cpu_class_ceilingucall_cpu_class_tA ceiling on the CPU class; unknown sets none.
max_response_bytesstd::size_tThe largest response a call buffers; zero takes the largest receive buffer.
serverserver_t const *A server whose event loop drives this client, polled on that loop's thread only.
event_loopstd::size_tWhich of server's event loops.
client_call_options_tOne call's time budget, retries, redirects, security and body delivery; zero fields take their defaults.
client.hpp:72 ↗
struct client_call_options_t

Fields

timeoutstd::chrono::microsecondsTime budget including queueing and redirects; zero takes the client's.
max_retriesstd::size_tAdditional connection attempts; zero means 3.
max_redirectsstd::size_tRedirect hops followed; zero means 5.
securityucall_security_tTransport security for this call.
noise_private_keyucall_noise_key_t const *The client's identity, needed by Noise KK.
noise_peer_keyucall_noise_key_t const *The server's expected key, needed by Noise IK and KK.
noise_pre_shared_keyucall_noise_key_t const *The secret the server's interface also holds, mixed first into the handshake; refused on plaintext.
key_exchangeucall_key_exchange_tThe key exchange of the handshake, which the server must match.
body_deliveryucall_body_delivery_tWhether the call completes once the body is buffered, or once the head arrived.
client_call_tOne call to submit, whose URL, header lines and body the caller keeps until it completes.
client.hpp:104 ↗
struct client_call_t

Fields

verbucall_verb_tThe HTTP method.
urlstd::string_viewThe full URL, with an http or http+noise scheme.
headersstd::span<ucall_bytes_view_t const>Full header lines; the host, length and user agent lines are generated.
bodystd::string_viewThe request body; empty sends none.
optionsclient_call_options_tThe call's own budget, security and body delivery.
completed_tOne finished call: the context it was submitted with, and its response or why none arrived.
client.hpp:204 ↗
struct completed_t

Fields

contextvoid *The context passed to client_t::submit.
responseexpected<response_t>The received response, whatever its HTTP status, or the failed exchange's status.
completed_view_tThe calls one client_t::take_completed finished, in the caller's storage and the order they finished, each handed over once by take or by iterating; the responses never taken are freed with the view.
client.hpp:230 ↗
class completed_view_t
outputsWhere a call writes what value_type_ is made of.
types.hpp:85 ↗
template <typename value_type_>
struct outputs

Fields

valuevalue_type_The answer, which the call writes in place.
outputs< void >A call that outputs nothing but its status.
types.hpp:95 ↗
template <>
struct outputs< void >

Fields

valuevoidThe answer, which the call writes in place.
awaiterA deferred being awaited, living in the awaiting coroutine's frame: each poll reads the status word, and one not yet final posts itself again.
types.hpp:174 ↗
template <typename value_type_,
          a_scheduler scheduler_type_>
class awaiter
answer_valueWhat a final answer_type_ carries beside its status, void for a bare status_t.
types.hpp:203 ↗
template <typename answer_type_>
struct answer_value
Classclient_tAn HTTP client, made, submitted to, polled and destroyed by one owner at a time.8
client.hpp:287 ↗
class client_t

It owns its handle alone: completions land in storage the caller passes, and a blocking call takes its own completion out of the library, leaving the others queued there.

makeStarts a client, which holds no connection until its first call is polled.
client.hpp:292 ↗
expected<client_t> make(client_options_t const & options) noexcept;
submitAdmits call without driving the network; its one completion carries context.
client.hpp:322 ↗
status_t submit(client_call_t const & call, void *context) noexcept;

A full client refuses with status_code_t::contended_k until completions are taken.

take_completedDrives every admitted call on this thread and fills the front of storage with finished ones, waiting up to timeout for the first; empty storage only advances the network.
client.hpp:349 ↗
expected<completed_view_t> take_completed(
    std::span<ucall_completed_call_t> storage,
    std::chrono::microseconds timeout) noexcept;
callSubmits call and polls until it finishes, leaving the completions of other calls queued for take_completed.
client.hpp:367 ↗
expected<response_t> call(client_call_t const & call) noexcept;

The response arrives whatever its HTTP status.

getFetches url and polls until the response arrives.
client.hpp:383 ↗
expected<response_t> get(std::string_view url) noexcept;
postPosts body to url and polls until the response arrives.
client.hpp:386 ↗
expected<response_t> post(
    std::string_view url,
    std::string_view body) noexcept;
putPuts body at url and polls until the response arrives.
client.hpp:391 ↗
expected<response_t> put(
    std::string_view url,
    std::string_view body) noexcept;
patchPatches url with body and polls until the response arrives.
client.hpp:402 ↗
expected<response_t> patch(
    std::string_view url,
    std::string_view body) noexcept;
Classresponse_tA received HTTP response, whatever its status, freed on destruction and readable after its client is gone.7
client.hpp:124 ↗
class response_t
response_t
client.hpp:129 ↗
response_t() noexcept=default;
response_t(
    ucall_client_t client,
    ucall_received_http_response_t response) noexcept;
status_codeThe status code the server sent.
client.hpp:137 ↗
ucall_http_status_t status_code() const noexcept;
bodyThe body, decoded from chunked framing if it arrived that way; a streamed call refuses it, as body_chunk pulls its body.
client.hpp:141 ↗
expected<std::string_view> body() const noexcept;
fields_countHeader field lines, repeated names apart.
client.hpp:167 ↗
std::size_t fields_count() const noexcept;
fieldHeader field line index in arrival order.
client.hpp:170 ↗
expected<field_t> field(std::size_t index) const noexcept;
body_chunkPulls the body bytes of a streamed call decoded since the last pull, empty once the body ended, valid until the next pull.
client.hpp:191 ↗
expected<std::string_view> body_chunk() noexcept;

Pulls run on the client's thread and never wait: with nothing decoded yet the pull answers status_code_t::would_block_k, and polling the client moves the next bytes in.