@20i/distributed-lock
This library talks to our aws distributed lock servers and provides automatic locking/unlocking on functions
import DistributedLock from "@20i/distributed-lock"
async function test() {
const dl = new DistributedLock("your-project-api-key")
const result = await dl.acquireLock({
lockName: "my-lock",
fn: async () => {
await doSomeThinkingThatTakesAWhile()
return 10
}
})
console.log(result)
}
As soon as the function is done, the lock will be removed!
Be warned that locks have an expiration time of 10 seconds by default. However, if you expect that your function will take longer,
you can provide a custom expiration time!
import DistributedLock from "@20i/distributed-lock"
async function test() {
const dl = new DistributedLock("your-project-api-key")
const result = await dl.acquireLock({
lockName: "my-lock",
expirationTimeMs: 20000,
fn: async () => {
await doSomeThinkingThatTakesAWhile()
return 10
}
})
console.log(result)
}
You can also provide a timeout if you do not wish to wait past a certain amount of time.
import DistributedLock from "@20i/distributed-lock"
async function test() {
const dl = new DistributedLock("your-project-api-key")
const result = await dl.acquireLock({
lockName: "my-lock",
timeout: 5000,
fn: async () => {
await doSomeThinkingThatTakesAWhile()
return 10
}
})
console.log(result)
}