Skip to main content

SharedLock Plugins

withSharedLockPrefix plugin​

The SharedLock prefix plugin intercepts calls to a shared-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 reader-writer locks — Prefix lock keys with a tenant identifier to isolate shared lock state between tenants
  • Resource scoping — Organize shared locks by resource type or module
  • Environment isolation — Separate development, staging, and production shared lock state
  • Read/write path scoping — Apply consistent namespacing to both reader and writer lock operations

How it works​

The withSharedLockPrefix function returns a PluginFn that calls enhance on each adapter method that accepts a shared-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
forceReleaseFirst argument (key)prefix + key
getStateFirst argument (key)prefix + key
acquireWriterFirst argument (key)prefix + key
refreshWriterFirst argument (key)prefix + key
releaseWriterFirst argument (key)prefix + key
acquireReaderkey within settings objectprefix + key
refreshReaderFirst argument (key)prefix + key

Usage​

./samples/with-shared-lock-prefix.ts
import { withPlugin } from "eridu-tech/middleware";
import { MemorySharedLockAdapter } from "eridu-tech/shared-lock/memory-shared-lock-adapter";
import { withSharedLockPrefix } from "eridu-tech/shared-lock/plugins";

const adapter = new MemorySharedLockAdapter();

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

Before/after behavior​

Before — Shared lock keys are used as-is:

./samples/unprefixed-acquire-writer.ts
import { MemorySharedLockAdapter } from "eridu-tech/shared-lock/memory-shared-lock-adapter";

const adapter = new MemorySharedLockAdapter();

const ttl = new Date(Date.now() + 60_000);

await adapter.acquireWriter("doc:42", "writer-1", ttl);
// -> acquires writer lock on "doc:42"

After — Shared lock keys are automatically prefixed:

./samples/prefixed-acquire-writer.ts
import { withPlugin } from "eridu-tech/middleware";
import { MemorySharedLockAdapter } from "eridu-tech/shared-lock/memory-shared-lock-adapter";
import { withSharedLockPrefix } from "eridu-tech/shared-lock/plugins";

const adapter = new MemorySharedLockAdapter();

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

const ttl = new Date(Date.now() + 60_000);

await prefixedAdapter.acquireWriter("doc:42", "writer-1", ttl);
// -> acquires writer lock on "tenant-42:doc: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.