Semaphore - Middle¶
Cancellation between acquire and release is the main permit-leak risk. Treat a permit as an owned resource.
sequenceDiagram
participant J as Job
participant S as Semaphore
J->>S: acquire
S-->>J: permit
J->>J: use connection
J->>S: release in finally
Set the limit from downstream capacity, then load-test it. Weighted semaphores fit jobs with different costs, but large requests may starve behind small ones. Combine a semaphore with a bounded input queue so waiting jobs do not consume unlimited memory.
Continue to senior.md.
Test yourself¶
- Where can cancellation leak a permit?
- Why pair a semaphore with a bounded queue?
- What fairness issue affects weighted permits?