This repository has been archived by the owner on Jul 27, 2023. It is now read-only.
/
kv_lock.go
178 lines (149 loc) · 4.06 KB
/
kv_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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
package commands
import (
"fmt"
"time"
consulapi "github.com/hashicorp/consul/api"
"github.com/spf13/cobra"
)
type KvLockOptions struct {
Behavior string
Ttl string
LockDelay time.Duration
Session string
cleanSession bool
}
func (k *Kv) AddLockSub(cmd *cobra.Command) {
klo := &KvLockOptions{cleanSession: false}
lockCmd := &cobra.Command{
Use: "lock <path>",
Short: "Acquire a lock on a given path",
Long: "Acquire a lock on a given path",
RunE: func(cmd *cobra.Command, args []string) error {
return k.Lock(args, klo)
},
}
oldLockCmd := &cobra.Command{
Use: "kv-lock <path>",
Short: "Acquire a lock on a given path",
Long: "Acquire a lock on a given path",
Deprecated: "Use kv lock",
Hidden: true,
RunE: func(cmd *cobra.Command, args []string) error {
return k.Lock(args, klo)
},
}
lockCmd.Flags().StringVar(&klo.Behavior, "behavior", "release", "Lock behavior. One of 'release' or 'delete'")
lockCmd.Flags().StringVar(&klo.Ttl, "ttl", "", "Lock time to live")
lockCmd.Flags().DurationVar(&klo.LockDelay, "lock-delay", 15*time.Second, "Lock delay")
lockCmd.Flags().StringVar(&klo.Session, "session", "", "Previously created session to use for lock")
k.AddDatacenterOption(lockCmd)
oldLockCmd.Flags().StringVar(&klo.Behavior, "behavior", "release", "Lock behavior. One of 'release' or 'delete'")
oldLockCmd.Flags().StringVar(&klo.Ttl, "ttl", "", "Lock time to live")
oldLockCmd.Flags().DurationVar(&klo.LockDelay, "lock-delay", 15*time.Second, "Lock delay")
oldLockCmd.Flags().StringVar(&klo.Session, "session", "", "Previously created session to use for lock")
k.AddDatacenterOption(oldLockCmd)
cmd.AddCommand(lockCmd)
k.AddCommand(oldLockCmd)
}
func (k *Kv) Lock(args []string, klo *KvLockOptions) error {
var lockOpts *consulapi.KVPair
// Work around a Consul API bug that ignores LockDelay == 0
if klo.LockDelay == 0 {
klo.LockDelay = time.Nanosecond
}
switch {
case len(args) == 0:
return fmt.Errorf("Key path must be specified")
case len(args) > 1:
return fmt.Errorf("Only one path allowed")
}
path := args[0]
client, err := k.KV()
if err != nil {
return err
}
writeOpts := k.WriteOptions()
queryOpts := k.QueryOptions()
queryOpts.WaitTime = 15 * time.Second
sessionClient, err := k.Session()
if err != nil {
return err
}
if klo.Session == "" {
// Create the Consul session
se, _, err := sessionClient.CreateNoChecks(&consulapi.SessionEntry{
Name: "Session for consul-cli",
LockDelay: klo.LockDelay,
Behavior: klo.Behavior,
TTL: klo.Ttl,
}, writeOpts)
if err != nil {
return err
}
klo.Session = se
klo.cleanSession = true
}
// Set the session to renew periodically
sessionRenew := make(chan struct{})
go sessionClient.RenewPeriodic(klo.Ttl, klo.Session, nil, sessionRenew)
defer func() {
close(sessionRenew)
sessionRenew = nil
}()
WAIT:
kv, meta, err := client.Get(path, queryOpts)
if err != nil {
k.destroySession(sessionClient, klo)
return err
}
locked := false
if kv != nil && kv.Session == klo.Session {
goto HELD
}
if kv != nil && kv.Session != "" {
queryOpts.WaitIndex = meta.LastIndex
goto WAIT
}
// Node doesn't already exist
if kv == nil {
lockOpts = &consulapi.KVPair{
Key: path,
Session: klo.Session,
}
} else {
lockOpts = &consulapi.KVPair{
Key: kv.Key,
Flags: kv.Flags,
Value: kv.Value,
Session: klo.Session,
}
}
// Try to acquire the lock
locked, _, err = client.Acquire(lockOpts, nil)
if err != nil {
k.destroySession(sessionClient, klo)
return err
}
if !locked {
select {
case <-time.After(5 * time.Second):
goto WAIT
}
}
HELD:
fmt.Fprintln(k.Out, klo.Session)
return nil
}
// Destroy the session on error. Only performed when
// klo.cleanSession == true
//
func (k *Kv) destroySession(s *consulapi.Session, klo *KvLockOptions) error {
if klo.cleanSession {
writeOpts := k.WriteOptions()
_, err := s.Destroy(klo.Session, writeOpts)
if err != nil {
return fmt.Errorf("Session not destroyed: %s", klo.Session)
}
}
return nil
}