-
Notifications
You must be signed in to change notification settings - Fork 1
/
kvcmd.go
212 lines (165 loc) · 3.73 KB
/
kvcmd.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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
package goconsul
import (
"fmt"
"time"
"github.com/gogf/gf/encoding/gjson"
"github.com/gogf/gf/encoding/gtoml"
"github.com/hashicorp/consul/api"
)
type KVCmd struct {
client *Client
kv *api.KV
DataRoot string `json:"dataRoot"`
}
func NewKVCmd(client *Client, dataRoot string) *KVCmd {
return &KVCmd{
client: client,
kv: client.KV(),
DataRoot: dataRoot,
}
}
func (cmd *KVCmd) realKey(key string) string {
if cmd.DataRoot != "" {
return fmt.Sprintf("%s/%s", cmd.DataRoot, key)
}
return key
}
func (cmd *KVCmd) GetStr(key string) (string, error) {
if cmd.kv == nil {
return "", fmt.Errorf("not found, center not connected")
}
key = cmd.realKey(key)
keyPair, _, err := cmd.kv.Get(key, nil)
if err != nil {
return "", err
}
if keyPair == nil || keyPair.Value == nil {
return "", fmt.Errorf("value of key[%s] is nil", key)
}
return string(keyPair.Value), nil
}
func (cmd *KVCmd) GetJson(key string, value interface{}) error {
if cmd.kv == nil {
return fmt.Errorf("not found, center not connected")
}
data, err := cmd.GetStr(key)
if err != nil {
return err
}
j := gjson.New(data)
if j == nil {
return fmt.Errorf("get config json[%s] failed, invalid json string", key)
}
err = j.Struct(value)
if err != nil {
return err
}
return nil
}
func (cmd *KVCmd) GetToml(key string, value interface{}) error {
if cmd.kv == nil {
return fmt.Errorf("not found, center not connected")
}
data, err := cmd.GetStr(key)
if err != nil {
return err
}
err = gtoml.DecodeTo([]byte(data), value)
if err != nil {
return err
}
return nil
}
func (cmd *KVCmd) SetStr(key string, value string) error {
if cmd.kv == nil {
return fmt.Errorf("not found, center not connected")
}
key = cmd.realKey(key)
kv := &api.KVPair{
Key: key,
Value: []byte(value),
}
_, err := cmd.kv.Put(kv, nil)
if err != nil {
return err
}
return nil
}
func (cmd *KVCmd) SetJson(key string, value interface{}) error {
if cmd.kv == nil {
return fmt.Errorf("not found, center not connected")
}
data, err := gjson.Encode(value)
if err != nil {
return err
}
return cmd.SetStr(key, string(data))
}
func (cmd *KVCmd) SetToml(key string, value interface{}) error {
if cmd.kv == nil {
return fmt.Errorf("not found, center not connected")
}
data, err := gtoml.Encode(value)
if err != nil {
return err
}
return cmd.SetStr(key, string(data))
}
func (cmd *KVCmd) Delete(key string) error {
realKey := fmt.Sprintf("%s%s", cmd.DataRoot, key)
_, err := cmd.kv.Delete(realKey, nil)
if err != nil {
return err
}
return nil
}
func (cmd *KVCmd) Acquire(key string, value string, ttl string) (bool, error) {
session := cmd.client.Session()
sessionId, _, err := session.Create(&api.SessionEntry{
Name: key,
Behavior: api.SessionBehaviorDelete,
TTL: ttl,
LockDelay: 1 * time.Microsecond,
}, nil)
if err != nil {
return false, err
}
key = cmd.realKey(key)
pair := &api.KVPair{
Key: key,
Session: sessionId,
Value: ([]byte)(value),
}
blockCounter := 0
for blockCounter < 2 {
ok, _, err := cmd.kv.Acquire(pair, nil)
if err != nil {
return false, err
}
if ok {
return true, nil
}
time.Sleep(time.Microsecond * 10)
blockCounter++
}
return false, nil
}
func (cmd *KVCmd) UpdateExpire(key string) (bool, error) {
if cmd.kv == nil {
return false, fmt.Errorf("not found, center not connected")
}
realKey := cmd.realKey(key)
keyPair, _, err := cmd.kv.Get(realKey, nil)
if err != nil {
return false, err
}
if keyPair == nil || keyPair.Value == nil {
return false, fmt.Errorf("value of key[%s] is nil", realKey)
}
sessionId := keyPair.Session
_, _, err = cmd.client.Session().Renew(sessionId, nil)
if err != nil {
return true, err
}
return true, nil
}