How to "reserve first" units of concurrency

The following test fails on 0.16.0, 0.17.1, and master:

const std = @import("std");

fn noop() void {}

test {
    var threaded: std.Io.Threaded = .init(std.testing.allocator, .{
        .concurrent_limit = .limited(1),
    });
    defer threaded.deinit();
    const io = threaded.io();

    for (0..10_000) |i| {
        var f = io.concurrent(noop, .{}) catch |err| switch (err) {
            error.ConcurrencyUnavailable => |e| {
                std.log.err("failed at i: {}", .{i});
                return e;
            },
        };
        defer f.cancel(io);

        f.await(io);
    }
}
[default] (err): failed at i: 967
1/1 test2.test_0...FAIL (ConcurrencyUnavailable)
zig/0.18.0-dev.1180+2e9bb03df/lib/std/Io/Threaded.zig:2198:9: 0x11d79fe in concurrent (std.zig)
        return error.ConcurrencyUnavailable;
        ^
zig/0.18.0-dev.1180+2e9bb03df/lib/std/Io.zig:2620:25: 0x124c76f in concurrent__func_1011 (std.zig)
    future.any_future = try io.vtable.concurrent(
                        ^
test2.zig:16:17: 0x124c54c in test_0 (test2.zig)
                return e;
                ^
0 passed; 0 skipped; 1 failed.
1 errors were logged.
error: the following test command failed with exit code 1:
.zig-cache/o/da7898f856eaa4da1db1a72e88d5c3bc/test --seed=0xfd24701

How do I reserve units of concurrency before I use them so I can ensure concurrency is never unavailable?

My use case is that I have an HTTP client that is repeatedly making requests to a database and I would like that client to never experience ConcurrencyUnavailable. I have already gone to great lengths reserve all its memory too… in the interest of TigerStyle.
I need timeouts on the HTTP request so I am using Io.Select with a concurrent client.fetch and concurrent sleep.

The only thing I can really do right now is set my concurrency limit extra high.

Why not start concurrent tasks that immediately wait on a Semaphore, blocking until tasks are ready? Basically implementing a job/event queue.

There are a few ways you could do this, I guess.

fn work(io: std.Io, queue: *std.Io.Queue(Item)) void {
    while (queue.getOne()) |item| {
        // handle item
        // you can use Select in here
    } else |err| {
        std.log.info("received {t} from queue", .{err});
    }
}

pub fn main(init: std.process.Init) !void {
    var queue_buffer: [8]Item = undefined;
    var queue: std.Io.Queue(Item) = .init(&queue_buffer);
    var group: std.Io.Group = .init;
    defer group.await(init.io);
    errdefer group.cancel(init.io);
    for (0..std.Thread.getCpuCount()) |_| {  // there might be a want to be able to request this in general from std.Io since this assumes threads
        try group.concurrent(init.io, work, .{init.io, &queue});
    }
    defer queue.close(init.io);
    while (...) {
        ... generate item
        queue.putOneUncancelable(init.io, item);
    }
}

another possibility:

fn work(io: std.Io, sema: *std.Io.Semaphore, item: Item) void {
    defer sema.post(io);
    // handle item
}

pub fn main(init: std.process.Init) !void {
    var sema: std.Io.Semaphore = .{
        .permits = std.Thread.getCpuCount(),  // see comment above
    };
    while (...) {
        try sema.wait(init.io);
        ... generate item
        var f = init.io.concurrent(work, .{io, &sema, item}) catch |err| switch (err) {
            ...
        };
        defer f.cancel(init.io);
        f.await(io);
    }
}