为什么不是Go etcd客户端释放锁?

发布于 2025-01-24 02:40:09 字数 991 浏览 3 评论 0原文

我正在使用 golang etcd client 与ETCD交互。

我正在构建一个请求处理程序,该处理程序应该在ETCD中的键上获取锁,在键中放置一个值,然后释放锁。看台完成没有错误,但锁定没有释放。

这是我代码的简化。假设以下变量:

  • etcdclient*clientv3.client我在整个应用程序中共享。
  • keytolock字符串
  • valuetoputstring
func myHandler(w http.ResponseWriter, r *http.Request) {
    session, _ := concurrency.NewSession(etcdClient)
    defer session.Close()

    ctx, cancel := context.WithTimeout(context.Background(), time.Second*5)
    defer cancel()

    mutex := concurrency.NewMutex(session, keyToLock)
    mutex.Lock(ctx)
    defer mutex.Unlock(ctx)

    etcdClient.Put(ctx, keyToPut, valueToPut)
}

我已经尝试了以下事情:

  • 移动创建一个创建一个的代码上下文创建代码之后的会话(并辩护其关闭)。
  • 关闭会话,取消上下文并直接解锁互斥X(而不是递延)。直接调用这些功能时,这些函数均未返回任何错误。

I am using the Golang etcd client to interact with etcd. Here is an example from the official documentation for obtaining and releasing locks.

I am building a request handler that is supposed to obtain a lock on a key in etcd, put a value in the key, then release the lock. The put completes without an error but the lock is not releasing.

Here is a simplification of my code. Assume the following variables:

  • etcdClient is a *clientv3.Client that I'm sharing across the application.
  • keyToLock is a string
  • valueToPut is a string
func myHandler(w http.ResponseWriter, r *http.Request) {
    session, _ := concurrency.NewSession(etcdClient)
    defer session.Close()

    ctx, cancel := context.WithTimeout(context.Background(), time.Second*5)
    defer cancel()

    mutex := concurrency.NewMutex(session, keyToLock)
    mutex.Lock(ctx)
    defer mutex.Unlock(ctx)

    etcdClient.Put(ctx, keyToPut, valueToPut)
}

I have tried the following things already:

  • Moving the code that creates a session (and defers its closing) after the context creation code.
  • Closing the session, cancelling the context, and unlocking the mutex directly (as opposed to deferring). None of these functions return any errors when calling them directly.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文