Setu-TS
    Preparing search index...

    Class DurableObjectLock

    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.

    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 } }));

    0.2.0

    Index
    • Attempts to acquire the lock.

      Parameters

      • key: string

        The lock key

      • ttlMs: number

        How long the claim lasts, in milliseconds

      Returns Promise<string | null>

      The token when acquired, or null when another holder is live

      When the Durable Object answers with a non-2xx status — a misconfigured binding or a class that is not the lock object, neither of which may be reported as "someone else holds it"

    • 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.

      Parameters

      • key: string

        The lock key

      • token: string

        The token acquire returned

      Returns Promise<void>

      When the Durable Object answers with a non-2xx status