This repository was archived by the owner on Jul 24, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathlock.go
68 lines (57 loc) · 1.58 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
59
60
61
62
63
64
65
66
67
68
package locker
import "time"
type lockState int
const (
unknown lockState = iota
acquired lockState = iota
released lockState = iota
)
// Lock will create a lock for a key and set its value. If the owned
// channel is provided a bool will be pushed whenever our ownership of
// the lock changes. Pushing true into the quit channel will stop the
// locker from refreshing the lock and let it expire if we own it.
//
// owned := make(chan bool)
//
// go client.Lock("my-service", "http://10.0.0.1:9292", owned, nil)
//
// for {
// select {
// case v := <-owned:
// fmt.Printf("Lock ownership changed: %t\n", v)
// }
// }
//
// Lock is a blocking call, so it's recommended to run it in a goroutine.
func (c Client) Lock(name, value string, owned chan<- bool, quit <-chan bool) error {
lastState := unknown
tick := time.Tick(time.Second * 3)
for {
select {
case <-quit:
return nil
case <-tick:
state, err := c.updateNode(name, value)
if err != nil {
return err
}
if owned != nil && lastState != state {
owned <- state == acquired
}
lastState = state
}
}
panic("unreachable")
}
// updateNode will update the lock node in the cluster, effectively just
// updating the TTL of the key and ensuring our value is still in it.
func (c Client) updateNode(name, value string) (lockState, error) {
if err := c.Store.AcquireOrFreshenLock(name, value); err != nil {
if _, ok := err.(LockDenied); ok {
return released, nil
}
// no idea what just happened, just return the error
return unknown, err
}
return acquired, nil
}