Skip to main content

SharedLockFactoryResolver

The SharedLockFactoryResolver class provides a flexible way to configure and switch between different shared-lock adapters at runtime.

Initial configuration​

To begin using the ISharedLockFactoryResolver, you will need to register all required adapters during initialization.

./samples/shared-lock-factory-resolver-initial-config.ts
import { SharedLockFactoryResolver } from "eridu-tech/shared-lock";
import { MemorySharedLockAdapter } from "eridu-tech/shared-lock/memory-shared-lock-adapter";
import { RedisSharedLockAdapter } from "eridu-tech/shared-lock/redis-shared-lock-adapter";
import { Redis } from "ioredis";

export const sharedLockFactoryResolver = new SharedLockFactoryResolver({
adapters: {
memory: new MemorySharedLockAdapter(),
redis: new RedisSharedLockAdapter(new Redis("YOUR_REDIS_CONNECTION")),
},
// You can set an optional default adapter
defaultAdapter: "memory",
});

Usage​

1. Using the default adapter​

./samples/shared-lock-factory-resolver-default-adapter.ts
import { sharedLockFactoryResolver } from "./shared-lock-factory-resolver-initial-config.js";

await sharedLockFactoryResolver
.use()
.create("shared-resource", {
limit: 4,
})
.runWriterOrFail(async () => {
// code to run
});
danger

Note that if you dont set a default adapter, an error will be thrown.

2. Specifying an adapter explicitly​

./samples/shared-lock-factory-resolver-specific-adapter.ts
import { sharedLockFactoryResolver } from "./shared-lock-factory-resolver-initial-config.js";

await sharedLockFactoryResolver
.use("redis")
.create("shared-resource", {
limit: 4,
})
.runWriterOrFail(async () => {
// code to run
});
danger

Note that if you specify a non-existent adapter, an error will be thrown.

3. Overriding default settings​

./samples/shared-lock-factory-resolver-override-settings.ts
import { sharedLockFactoryResolver } from "./shared-lock-factory-resolver-initial-config.js";

await sharedLockFactoryResolver
.use("redis")
.create("shared-resource", {
limit: 4,
})
.runWriterOrFail(async () => {
// code to run
});
info

Note that the SharedLockFactoryResolver is immutable, meaning any configuration override returns a new instance rather than modifying the existing one.

Further information​

For further information refer to eridu-tech/shared-lock API docs.