store: Add a channel to lock that is closed if our lock is lost or an error.

Signed-off-by: Andrea Luzzardi <aluzzardi@gmail.com>
This commit is contained in:
Andrea Luzzardi 2015-05-14 17:35:57 -07:00
parent 250a17e954
commit 23481131c4
3 changed files with 10 additions and 7 deletions

View File

@ -257,10 +257,9 @@ func (s *Consul) CreateLock(key string, value []byte) (Locker, error) {
}
// Lock attempts to acquire the lock and blocks while doing so.
func (l *consulLock) Lock() error {
// FIXME: Locks may be lost and we should watch for the returned channel.
_, err := l.lock.Lock(nil)
return err
// Returns a channel that is closed if our lock is lost or an error.
func (l *consulLock) Lock() (<-chan struct{}, error) {
return l.lock.Lock(nil)
}
// Unlock released the lock. It is an error to call this

View File

@ -70,7 +70,7 @@ type KVEntry interface {
// Locker provides locking mechanism on top of the store.
// Similar to `sync.Lock` except it may return errors.
type Locker interface {
Lock() error
Lock() (<-chan struct{}, error)
Unlock() error
}

View File

@ -212,8 +212,12 @@ func (s *Zookeeper) CreateLock(key string, value []byte) (Locker, error) {
}
// Lock attempts to acquire the lock and blocks while doing so.
func (l *zookeeperLock) Lock() error {
return l.lock.Lock()
// Returns a channel that is closed if our lock is lost or an error.
func (l *zookeeperLock) Lock() (<-chan struct{}, error) {
if err := l.lock.Lock(); err != nil {
return nil, err
}
return make(<-chan struct{}), nil
}
// Unlock released the lock. It is an error to call this