-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathlock.go
58 lines (48 loc) · 1.42 KB
/
lock.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
package couchbasearray
import (
"errors"
"log"
"github.com/coreos/go-etcd/etcd"
)
const (
// ErrorKeyNotFound is the key not found error code from etcd
ErrorKeyNotFound = 100
// ErrorCompareFailed is the key compare failed error code from etcd
ErrorCompareFailed = 101
// ErrorNodeExist is the key exists failed error code from etcd
ErrorNodeExist = 105
)
// ErrLockInUse is returned when a lock is in use
var ErrLockInUse = errors.New("lock in use")
// AcquireLock attempts to create a new lock. If the lock already exists it returns an error
func AcquireLock(identifier string, namespace string, durationInSeconds uint64) error {
client := NewEtcdClient()
_, err := client.Create(namespace, identifier, durationInSeconds)
if err != nil {
eerr, ok := err.(*etcd.EtcdError)
if ok && eerr.ErrorCode == ErrorNodeExist {
} else {
log.Println(err)
return err
}
}
_, err = client.CompareAndSwap(namespace, identifier, durationInSeconds, identifier, 0)
if err != nil {
eerr, ok := err.(*etcd.EtcdError)
if ok && eerr.ErrorCode == ErrorCompareFailed {
return ErrLockInUse
}
log.Println(err)
return err
}
return err
}
// ReleaseLock releases an existing lock
func ReleaseLock(identifier string, namespace string) error {
if err := AcquireLock(identifier, namespace, 10); err != nil {
return err
}
client := NewEtcdClient()
_, err := client.CompareAndDelete(namespace, identifier, 0)
return err
}