Using Semaphores to Control In-Flight Work
ยท 7 min read
An asynchronous program can start work faster than the resources behind it can handle. Downloads compete for connections and bandwidth. Background jobs compete for CPU time. Buffered data occupies memory until its consumer finishes with it. A semaphore gives these operations a shared budget: work acquires permits before consuming capacity and returns them when that capacity becomes available again.
The useful questions are what one permit represents, which operations share the budget, and how long each operation holds its permits. This post works through those choices with Asyncband's Semaphore, including how Apache OpenDAL and prek use it.