登录
首页 >  Golang >  Go问答

带 ttl 的 etcd 互斥锁

来源:stackoverflow

时间:2024-04-20 16:54:34 170浏览 收藏

大家好,今天本人给大家带来文章《带 ttl 的 etcd 互斥锁》,文中内容主要涉及到,如果你对Golang方面的知识点感兴趣,那就请各位朋友继续看下去吧~希望能真正帮到你们,谢谢!

问题内容

我正在尝试创建一个简单的演示golang etcd客户端程序,它使用etcd互斥体创建一个共享锁,并设置超时。目标是让互斥锁在一段时间后过期。

package main

import (
    "context"
    "log"
    "time"

    "go.etcd.io/etcd/clientv3"
    "go.etcd.io/etcd/clientv3/concurrency"
)

var c chan int

func init() {
    c = make(chan int)
}

func main() {
    client, err := clientv3.new(clientv3.config{
        endpoints: []string{"http://localhost:2379"},
    })
    if err != nil {
        panic(err)
    }

    watcher := clientv3.newwatcher(client)
    channel := watcher.watch(context.background(), "/foobar", clientv3.withprefix())
    go func() {
        for {
            select {
            case change := <-channel:
                for _, ev := range change.events {
                    log.printf("etcd change on key; %s, type = %v", string(ev.kv.key), ev.type)
                }
            }
        }
    }()

    go lockfoobar(client, 1)
    go lockfoobar(client, 2)
    <-c
    <-c
}

func lockfoobar(client *clientv3.client, id int) {
    res, err := client.grant(context.background(), 1)
    if err != nil {
        panic(err)
    }

    session, err := concurrency.newsession(client, concurrency.withlease(res.id))
    if err != nil {
        panic(err)
    }

    mux := concurrency.newmutex(session, "/foobar")

    log.printf("trying to lock by #%d\n", id)
    ctx, _ := context.withtimeout(context.background(), 15*time.second)
    if err := mux.lock(ctx); err != nil {
        log.printf("failed to lock #%d: %v\n", id, err)
        c <- id
        return
    }

    log.printf("post-lock #%d (lease id = %x) bullshit\n", id, res.id)
    time.sleep(10 * time.second)
    ttl, _ := client.timetolive(context.todo(), res.id)
    log.printf("post-post-lock-#%d-sleep. lease ttl = %v", id, ttl.ttl)
    // mux.unlock(ctx)
    // log.printf("post-unlock #%d bullshit\n", id)

    time.sleep(200 * time.millisecond)
    c <- id
}

租约的 ttl 为 1 秒,而上下文的超时时间为 5 秒,因此,在上下文到期时锁应该已被删除。但是,无论上下文超时如何,“已锁定”锁始终仅在锁定失败后才会被删除。

这是当前的输出:

2018-10-04 18:39:59.413274 i | trying to lock by #2
2018-10-04 18:39:59.414530 i | trying to lock by #1
2018-10-04 18:39:59.414656 i | etcd change on key; /foobar/2a0966398d0677a2, type = put
2018-10-04 18:39:59.414684 i | post-lock #2 (lease id = 2a0966398d0677a2) bullshit
2018-10-04 18:39:59.415617 i | etcd change on key; /foobar/2a0966398d0677a4, type = put
2018-10-04 18:40:10.239045 i | post-post-lock-#2-sleep. lease ttl = 1                       <-- lock for #2 has ttl = 1 even after 10s
2018-10-04 18:40:15.238871 i | failed to lock #1: context deadline exceeded                 <-- lock for #1 fails after 15s

如您所见,#2 的锁即使在 15 秒后仍然有效。

在另一个终端中运行 etcdctl_api=3 etcdctl watch --prefix=true /foobar 来观察按键的更改,显示以下输出

PUT
/foobar/2a0966398d0677a2

PUT
/foobar/2a0966398d0677a4

DELETE
/foobar/2a0966398d0677a4

DELETE
/foobar/2a0966398d0677a2

这是预期的行为吗?有没有办法实现我想要的目标?

p.s.:现实世界的用例是创建一个程序,该程序在多个实例中运行,并且不会在崩溃和/或终止(sigkill)时在 etcd 中留下锁。


解决方案


经过一番搜索,我找到了这种行为的原因。会话使租约保持活动状态,直到出现错误或取消为止。

来自 session.go

...
// keep the lease alive until client error or cancelled context
go func() {
    defer close(donec)
    for range keepAlive {
        // eat messages until keep alive channel closes
    }
}()
...

创建互斥体后调用 session.orphan() 将阻止会话保持活动状态并达到我的目的。

终于介绍完啦!小伙伴们,这篇关于《带 ttl 的 etcd 互斥锁》的介绍应该让你收获多多了吧!欢迎大家收藏或分享给更多需要学习的朋友吧~golang学习网公众号也会发布Golang相关知识,快来关注吧!

声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>