为什么不是Go etcd客户端释放锁?
我正在使用 golang etcd client 与ETCD交互。
我正在构建一个请求处理程序,该处理程序应该在ETCD中的键上获取锁,在键中放置一个值,然后释放锁。看台完成没有错误,但锁定没有释放。
这是我代码的简化。假设以下变量:
etcdclient
是*clientv3.client
我在整个应用程序中共享。keytolock
是字符串
valuetoput
是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)
}
我已经尝试了以下事情:
- 移动创建一个创建一个的代码上下文创建代码之后的会话(并辩护其关闭)。
- 关闭会话,取消上下文并直接解锁互斥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 astring
valueToPut
is astring
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论