import { MongodbSemaphoreAdapter } from "@daiso-tech/core/semaphore/mongodb-semaphore-adapter";
import { MongoClient } from "mongodb";
const client = await MongoClient.connect("YOUR_MONGODB_CONNECTION_STRING");
const database = client.db("database");
const semaphoreAdapter = new MongodbSemaphoreAdapter({
database
});
// You need initialize the adapter once before using it.
await semaphoreAdapter.init()
Attempts to acquire a slot in the semaphore. Succeeds only if the current number of acquired slots has not reached the limit.
Settings containing the context, key, slotId, limit, and ttl for the acquisition
Promise resolving to true if the slot was successfully acquired, false if the slot limit has been reached
Forcibly releases all slots for the specified semaphore regardless of ownership. Used for emergency cleanup or administrative operations. Bypasses ownership verification for situations where individual slot holders are unavailable.
Unique identifier for the semaphore
Promise resolving to true if the semaphore existed and slots were released, false if the semaphore doesn't exist or has no acquired slots
Retrieves the current state of a semaphore.
Unique identifier for the semaphore
Promise resolving to the non-expired semaphore state if it exists; otherwise null for missing or expired semaphores
Refreshes (extends) the time-to-live of an existing slot. Only succeeds if all conditions are met: the slot exists, hasn't expired, and is expirable.
Unique identifier for the semaphore
Unique identifier of the slot to refresh
New time-to-live duration to set
Promise resolving to true if refresh succeeded, false if the slot is unexpirable, expired, or doesn't exist
Releases a specific slot if it is currently acquired. Only the holder of the slot (identified by slotId) can release it.
Unique identifier for the semaphore
Unique identifier of the slot to release
Promise resolving to true if the slot was successfully released, false if the slot doesn't exist or is already released
To utilize the
MongodbSemaphoreAdapter, you must install the"mongodb"package.Note in order to use
MongodbSemaphoreAdaptercorrectly, ensure you use a single, consistent database across all server instances.IMPORT_PATH:
"@daiso-tech/core/semaphore/mongodb-semaphore-adapter"