Skip to main content

Lock Plugins

withLockPrefix plugin

The Lock prefix plugin intercepts calls to a lock adapter and transparently prefixes all lock keys with a configurable string. This enables logical key namespacing without modifying the adapter implementation.

Use cases

  • Multi-tenant locking — Prefix lock keys with a tenant identifier to prevent cross-tenant lock contention
  • Resource scoping — Organize locks by resource type or module to avoid key collisions
  • Environment isolation — Separate development, staging, and production lock state
  • Region isolation — Prefix lock keys with a region identifier in multi-region deployments

How it works

The withLockPrefix function returns a PluginFn that calls enhance on each adapter method that accepts a lock key. When an enhanced method is invoked, the plugin intercepts the call, prepends the configured prefix to the key argument, and forwards the modified arguments to the original method.

The plugin prefixes keys for the following methods:

MethodKey argumentPattern
acquireSecond argument (key)prefix + key
forceReleaseSecond argument (key)prefix + key
getStateSecond argument (key)prefix + key
refreshSecond argument (key)prefix + key
releaseSecond argument (key)prefix + key

Every method on the ILockAdapter that operates on a specific lock key is prefixed.

Usage

import { withPlugin } from "eridu-tech/middleware";
import { MemoryLockAdapter } from "eridu-tech/lock/memory-lock-adapter";
import { withLockPrefix } from "eridu-tech/lock/plugins";

const adapter = new MemoryLockAdapter();

// Apply the prefix plugin
const prefixedAdapter = withPlugin(adapter, withLockPrefix("tenant-42:"));

// The key "job:123" is automatically prefixed to "tenant-42:job:123"
const acquired = await prefixedAdapter.acquire(
context,
"job:123",
"lock-id",
ttl,
);

Using with LockFactory

The plugin can be applied directly to the adapter passed to the LockFactory constructor:

import { LockFactory } from "eridu-tech/lock";
import { MemoryLockAdapter } from "eridu-tech/lock/memory-lock-adapter";
import { withPlugin } from "eridu-tech/middleware";
import { withLockPrefix } from "eridu-tech/lock/plugins";

const adapter = new MemoryLockAdapter();
const prefixedAdapter = withPlugin(adapter, withLockPrefix("worker:"));

const lockFactory = new LockFactory({
adapter: prefixedAdapter,
});

// All locks acquired through lockFactory will use "worker:..." keys

Before/after behavior

Before — Lock keys are used as-is:

adapter.acquire(context, "resource:42", "lock-id", ttl)
→ acquires lock on "resource:42"

After — Lock keys are automatically prefixed:

adapter.acquire(context, "resource:42", "lock-id", ttl)
→ acquires lock on "prod:resource:42"
danger

Because withPlugin uses enhance under the hood, the same edge case applies: if one enhanced method internally calls another enhanced method via this, the middleware will apply twice. Be mindful of inter-method calls when applying plugins that enhance multiple methods on the same instance.

info

For more information about the withPlugin function and applying plugins to adapters, see the Middleware plugin documentation.