Serializes lock acquisition for one key.
One Durable Object per lock key, and a Durable Object executes one request at a time — which is what makes the read-compare-write below atomic without a transaction, and what makes this a genuine distributed lock rather than a best-effort one.
The holder record lives in state.storage, never in a field. A Durable
Object is evicted after 70–140 seconds of inactivity when it cannot
hibernate, and a lock TTL routinely outlives that; a deadline held in memory
would evaporate on eviction and hand the same lock to a second holder, which
is precisely the failure a distributed lock exists to prevent.
Example 1
Example 1
import { DurableObject } from 'cloudflare:workers'; import { DistributedLockObjectCore } from '@setu-ts/cloudflare-plugin'; export class DistributedLockObject extends DurableObject { #core = new DistributedLockObjectCore(this.ctx); override fetch(request: Request): Promise<Response> { return this.#core.fetch(request); } }
DistributedLockObjectCore(state: IDurableObjectState,options?: DistributedLockObjectCoreOptions)
Parameters
state: IDurableObjectState
The Durable Object's ctx
optional
options: DistributedLockObjectCoreOptions
Optional seams; the defaults are the deployment path