code wiki / _hdl_build / nx_apistack_bulkhead.nx
nx_apistack_bulkhead.nx source
↩ module page · 16 lines · 788 B
1// nx_apistack_bulkhead.nx -- CAP-API-BULKHEAD: concurrency isolation for /api. A bulkhead caps concurrent in-flight
2// calls to a resource so a slow/expensive route can't exhaust the whole plane (isolation, the Titanic-compartment
3// pattern). Pure counter: bh[0]=active. acquire returns 1 (and increments) if under max, else 0 (reject/shed).
4// Deterministic, gateable offline. license_tier: ORIGINAL
5import "nx_syscalls.nx"
6
7// try to enter the bulkhead: 1 if under max (active incremented), 0 if full (reject).
8func bh_acquire(bh: *i64, max: i64) -> i64 {
9 if bh[0] < max { bh[0] = bh[0] + 1; return 1 }
10 return 0
11}
12// leave the bulkhead (never below 0). Returns the new active count.
13func bh_release(bh: *i64) -> i64 {
14 if bh[0] > 0 { bh[0] = bh[0] - 1 }
15 return bh[0]
16}