A distributed lock backed by one Durable Object per key.
Structurally satisfies scheduler-plugin's IDistributedLock without
importing it — that interface is internal to its own package, and
AI_GUIDELINES §2.2/§3.3 forbid a plugin importing another plugin. The
application hands an instance to SchedulerPlugin, the same wiring
KvSessionStore uses for SessionPlugin, because that option is read at
plugin construction before any application exists.
Correctness comes from the platform rather than from an algorithm: a Durable
Object executes one request at a time, so the read-compare-write inside
DistributedLockObjectCore is atomic with no transaction and no
Redlock-style quorum.
Example 1
Example 1
import { env } from 'cloudflare:workers'; import { DurableObjectLock } from '@setu-ts/cloudflare-plugin'; import { SchedulerPlugin } from '@setu-ts/scheduler-plugin'; const lock = new DurableObjectLock(env.LOCKS as IDurableObjectNamespace, { runtime, keyPrefix: 'reports:', }); // `enabled: true` is NOT needed: resolveLock consults `lock` before it // consults `enabled`, so an injected lock always wins. app.register(SchedulerPlugin({ distributedLock: { lock } }));
DurableObjectLock(namespace: IDurableObjectNamespace,options: DurableObjectLockOptions)
namespace: IDurableObjectNamespace
The Durable Object namespace binding
options: DurableObjectLockOptions
The runtime services, and optional key namespacing
acquire(key: string,ttlMs: number): Promise<string | null>
Attempts to acquire the lock.
release(key: string,token: string): Promise<void>
Releases a previously acquired lock.
A token that does not match the current holder is ignored by the object, so a caller whose claim already expired cannot release its successor's.