Skip to content

Concurrency & Async Tasks ​

httpx.zig provides explicit primitives for concurrent execution and background task management: parallel client helpers plus a bounded worker pool and queue.

Parallel Requests ​

Execute multiple HTTP requests using getAll / requestAll on an initialized client. They run the requests and wait for all to complete:

zig
const urls = [_][]const u8{
    "https://httpbun.com/get",
    "https://httpbun.com/headers",
};
var results = try client.getAll(urls);
defer {
    for (results) |*r| r.deinit();
    allocator.free(results);
}

For unreachable or slow hosts, set per-request timeoutMs so batch jobs fail fast instead of waiting on default socket budgets:

zig
var r = try client.get("https://slow.example.com", .{ .timeoutMs = 2_000 });
defer r.deinit();

Worker Pool ​

httpx.concurrency.Pool (src/concurrency/workerPool.zig) is a fixed-thread, bounded-queue pool with graceful drain. It is also exported as httpx.client.workerPool:

Pass a live Zig 0.17 std.Io to the pool. It backs blocking queue waits and must outlive the pool. submit applies backpressure when the queue is full; trySubmit fails immediately with QueueFull or QueueBusy.

zig
var pool = try httpx.concurrency.Pool.init(allocator, io, .{
    .workers = 4,
    .queueCapacity = 1024,
    .autoStart = true,
});
defer pool.deinit();

Defaults ​

  • workers = 0 means "choose a sensible default from the host CPU count" (clamped 1..16).
  • queueCapacity = 1024 bounds pending tasks before submission fails.
  • autoStart = true starts worker threads on the first submitted task.

Queue ​

httpx.concurrency.Queue(T) (src/concurrency/queue.zig) is the generic thread-safe bounded queue used internally by the pool. It exposes push, tryPush, pop, tryPop, drain, and close.

Released under the MIT License.