1
0
mirror of https://github.com/go-micro/go-micro.git synced 2025-05-31 21:59:42 +02:00

Add Wait option support for sync/etcd plugins (#2459)

* Add Wait support for sync/etcd plugins

* Use ErrLockTimeout if context deadline exceeded
This commit is contained in:
Muhammad Iqbal Alaydrus 2022-03-25 09:28:19 +07:00 committed by GitHub
parent c6d352c832
commit 73eda3346d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -115,7 +115,15 @@ func (e *etcdSync) Lock(id string, opts ...sync.LockOption) error {
m := cc.NewMutex(s, path)
if err := m.Lock(context.TODO()); err != nil {
lockCtx := context.Background()
if options.Wait > 0 {
var cancel context.CancelFunc
lockCtx, cancel = context.WithTimeout(lockCtx, options.Wait)
defer cancel()
}
if err := m.Lock(lockCtx); err != nil && err == context.DeadlineExceeded {
return sync.ErrLockTimeout
} else if err != nil {
return err
}