01 // What it’s for
Why reach for DurabilitySemaphore
Use DurabilitySemaphore when many client Durable Objects or Workers compete for a shared account concurrency budget, external API rate slot, or bounded downstream resource—and permits must survive evictions, handle crashed holders via expiry, and support live limit reductions without revoking running work.
02 // How it works
Inside your Durable Object
- 01Durable token vault: Active permits, optional per-group caps (
groupLimit), and unclaimed-grant expiries live inctx.storage.sqlinside a single coordinator DO per key. - 02Dynamic limit tightening: Lowering
limit()stops new grants immediately without killing already-running jobs; capacity drains cleanly as holders release. - 03Unclaimed grant sweeper: Grants that are issued but never confirmed or renewed are automatically reclaimed on the shared
DurabilityScheduleralarm. - 04Monotonic high-water release: Client release messages carry monotonic sequence markers so duplicate or reordered release RPCs never double-free permits.
Storage
ctx.storage.sql
Alarm
Shared DurabilityScheduler
Import
durability/experimental/semaphore