Skip to content

Praise: the BranchSlot RAII guard is a really clean pattern #178

@henliveira

Description

@henliveira

I was reading through crates/forkd-controller/src/http.rs to understand how the BRANCH endpoint enforces its concurrency limits, and the BranchSlot design (lines 80-140) is genuinely well thought out.

What I like:

  1. Both invariants are bundled into one type. The per-tag exclusion set (branch_in_flight: HashSet<String>) and the global semaphore are two pieces of state that have to be acquired and released together — splitting them across the handler body would make it really easy to add an early-return that forgets one half. Wrapping both in BranchSlot and giving it Drop collapses the cleanup into "let it go out of scope".

  2. Acquisition order is documented. The comment at lines 122-123 ("Acquire the global permit first") explains exactly why the ordering matters — if you did it the other way, you'd have a HashSet rollback to write on the semaphore-failure path. The comment also tells future-you why you can't just "fix" the order.

  3. The error type is just two variantsAlreadyInFlight (→ 409) and CapacityExceeded (→ 503) — which exactly mirror the two failure modes a client cares about. No Box<dyn Error>, no string matching.

  4. It's tested in isolation. The three branch_slot_* tests (lines 1600-1670) cover the happy path, the same-tag conflict, the cap, and (the easy-to-forget one) recovery after Drop. The recovery test is exactly the regression I'd worry about if someone refactored the order of operations.

This is the kind of small primitive that pays for itself the first time someone adds a new early-return to branch_sandbox and it just works. Nice work.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions