@20i/distributed-lock 中文文档教程

发布于 3年前 浏览 23 更新于 3年前

@20i/distributed-lock

这个库与我们的 aws 分布式锁服务器对话,并提供自动锁定/解锁功能

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()
            // during this time, if any other client wants to get 'my-lock', it will wait until this long fn is done
            return 10
        }
    })

    console.log(result) // 10
}

一旦功能完成,锁就会被移除!

请注意,默认情况下锁的过期时间为 10 秒。 但是,如果您预计您的功能将花费更长的时间, 您可以提供自定义到期时间!

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,    // "my-lock" will now expire 20 seconds after it is created!
        fn: async () => {
            await doSomeThinkingThatTakesAWhile()
            // during this time, if any other client wants to get my-lock, it will wait until this long fn is done
            return 10
        }
    })

    console.log(result) // 10
}

如果您不想等待超过一定时间,您也可以提供超时。

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,  // if someone else is using "my-lock" and we end up waiting for than 5000 ms, we will stop trying!
        fn: async () => {
            await doSomeThinkingThatTakesAWhile()
            // during this time, if any other client wants to get my-lock, it will wait until this long fn is done
            return 10
        }
    })

    console.log(result) // 10
}

@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()
            // during this time, if any other client wants to get 'my-lock', it will wait until this long fn is done
            return 10
        }
    })

    console.log(result) // 10
}

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,    // "my-lock" will now expire 20 seconds after it is created!
        fn: async () => {
            await doSomeThinkingThatTakesAWhile()
            // during this time, if any other client wants to get my-lock, it will wait until this long fn is done
            return 10
        }
    })

    console.log(result) // 10
}

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,  // if someone else is using "my-lock" and we end up waiting for than 5000 ms, we will stop trying!
        fn: async () => {
            await doSomeThinkingThatTakesAWhile()
            // during this time, if any other client wants to get my-lock, it will wait until this long fn is done
            return 10
        }
    })

    console.log(result) // 10
}
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文