-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathnats.go
298 lines (250 loc) · 7.16 KB
/
nats.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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
package certmagic_nats
import (
"context"
"encoding/binary"
"errors"
"fmt"
"io/fs"
"math/rand"
"path"
"strings"
"sync"
"time"
"github.com/caddyserver/caddy/v2"
"github.com/caddyserver/certmagic"
"github.com/nats-io/nats.go"
"go.uber.org/zap"
)
type Nats struct {
logger *zap.Logger
Client nats.KeyValue
Hosts string `json:"hosts"`
Bucket string `json:"bucket"`
Creds string `json:"creds"`
InboxPrefix string `json:"inbox_prefix"`
ConnectionName string `json:"connection_name"`
revMap map[string]uint64
maplock sync.Mutex
}
var (
_ caddy.Provisioner = (*Nats)(nil)
_ certmagic.Storage = (*Nats)(nil)
_ certmagic.Locker = (*Nats)(nil)
)
// should be save to use as it is not allowed to be used in urls
const replaceChar = "#"
func normalizeNatsKey(key string) string {
if len(key) == 0 {
return key
}
key = strings.ReplaceAll(key, ".", replaceChar)
key = strings.ReplaceAll(key, "/", ".")
key = strings.ReplaceAll(key, replaceChar, "/")
return key
}
func denormalizeNatsKey(key string) string {
key = strings.ReplaceAll(key, "/", replaceChar)
key = strings.ReplaceAll(key, ".", "/")
key = strings.ReplaceAll(key, replaceChar, ".")
return key
}
func connectNats(host, creds, bucket, connectionName, inboxPrefix string) (nats.KeyValue, error) {
options := []nats.Option{nats.Name(connectionName), nats.CustomInboxPrefix(inboxPrefix)}
if creds != "" {
options = append(options, nats.UserCredentials(creds))
}
nc, err := nats.Connect(host, options...)
if err != nil {
return nil, err
}
js, err := nc.JetStream(nats.PublishAsyncMaxPending(256))
if err != nil {
return nil, err
}
return js.KeyValue(bucket)
}
func (n *Nats) setRev(key string, value uint64) {
n.maplock.Lock()
defer n.maplock.Unlock()
n.revMap[key] = value
}
func (n *Nats) getRev(key string) uint64 {
n.maplock.Lock()
defer n.maplock.Unlock()
return n.revMap[key]
}
func isWrongSequence(err error) bool {
return strings.Contains(err.Error(), "wrong last sequence")
}
// Lock acquires the lock for key, blocking until the lock
// can be obtained or an error is returned. Note that, even
// after acquiring a lock, an idempotent operation may have
// already been performed by another process that acquired
// the lock before - so always check to make sure idempotent
// operations still need to be performed after acquiring the
// lock.
//
// The actual implementation of obtaining of a lock must be
// an atomic operation so that multiple Lock calls at the
// same time always results in only one caller receiving the
// lock at any given time.
//
// To prevent deadlocks, all implementations (where this concern
// is relevant) should put a reasonable expiration on the lock in
// case Unlock is unable to be called due to some sort of network
// failure or system crash.
func (n *Nats) Lock(ctx context.Context, key string) error {
n.logger.Info(fmt.Sprintf("Lock: %v", key))
lockKey := fmt.Sprintf("LOCK.%s", key)
loop:
for {
// Check for existing lock
revision, err := n.Client.Get(lockKey)
if err != nil && !errors.Is(err, nats.ErrKeyNotFound) {
return err
}
if revision == nil {
break
}
expires := time.Unix(0, int64(binary.LittleEndian.Uint64(revision.Value())))
// Lock exists, check if expired
if time.Now().After(expires) {
// the lock expired and can be deleted
// break and try to create a new one
n.setRev(lockKey, revision.Revision())
if err := n.Unlock(ctx, key); err != nil {
if isWrongSequence(err) {
goto loop
}
return err
}
break
}
select {
// retry after a short period of time
case <-time.After(time.Duration(50+rand.Float64()*(200-50+1)) * time.Millisecond):
case <-ctx.Done():
return ctx.Err()
}
}
// lock doesn't exist, create it
contents := make([]byte, 8)
binary.LittleEndian.PutUint64(contents, uint64(time.Now().Add(time.Duration(5*time.Minute)).UnixNano()))
nrev, err := n.Client.Create(lockKey, contents)
if err != nil && isWrongSequence(err) {
// another process created the lock in the meantime
// try again
goto loop
}
if err != nil {
return err
}
n.setRev(lockKey, nrev)
return nil
}
// Unlock releases the lock for key. This method must ONLY be
// called after a successful call to Lock, and only after the
// critical section is finished, even if it errored or timed
// out. Unlock cleans up any resources allocated during Lock.
func (n *Nats) Unlock(ctx context.Context, key string) error {
n.logger.Info(fmt.Sprintf("Unlock: %v", key))
lockKey := fmt.Sprintf("LOCK.%s", key)
return n.Client.Delete(lockKey, nats.LastRevision(n.getRev(lockKey)))
}
func (n *Nats) Store(ctx context.Context, key string, value []byte) error {
n.logger.Info(fmt.Sprintf("Store: %v, %v bytes", key, len(value)))
_, err := n.Client.Put(normalizeNatsKey(key), value)
return err
}
func (n *Nats) Load(ctx context.Context, key string) ([]byte, error) {
n.logger.Info(fmt.Sprintf("Load: %v", key))
k, err := n.Client.Get(normalizeNatsKey(key))
if err != nil {
if err == nats.ErrKeyNotFound {
return nil, fs.ErrNotExist
}
return nil, err
}
return k.Value(), nil
}
func (n *Nats) Delete(ctx context.Context, key string) error {
n.logger.Info(fmt.Sprintf("Delete: %v", key))
return n.Client.Delete(normalizeNatsKey(key))
}
func (n *Nats) Exists(ctx context.Context, key string) bool {
n.logger.Info(fmt.Sprintf("Exists: %v", key))
_, err := n.Client.Get(normalizeNatsKey(key))
return err == nil
}
func (n *Nats) List(ctx context.Context, prefix string, recursive bool) ([]string, error) {
n.logger.Info(fmt.Sprintf("List: %v, %v", prefix, recursive))
oprefix := strings.TrimSuffix(prefix, "/")
prefix = normalizeNatsKey(prefix)
if len(prefix) > 1 && prefix[len(prefix)-1] != '.' {
prefix += "."
}
prefix += ">"
watcher, err := n.Client.Watch(prefix, nats.IgnoreDeletes(), nats.MetaOnly(), nats.Context(ctx))
if err != nil {
return nil, err
}
defer watcher.Stop()
var keys []string
for entry := range watcher.Updates() {
if entry == nil {
break
}
keys = append(keys, entry.Key())
}
for k := range keys {
keys[k] = denormalizeNatsKey(keys[k])
}
if recursive {
return keys, nil
}
dirs := make(map[string]struct{})
for k := range keys {
paths := strings.Split(keys[k], "/")
var dir string
for i := range paths {
dir += paths[i]
if path.Dir(dir) == oprefix {
dirs[dir] = struct{}{}
}
dir += "/"
}
}
dkeys := make([]string, 0, len(dirs))
for k := range dirs {
dkeys = append(dkeys, k)
}
return dkeys, nil
}
func (n *Nats) Stat(ctx context.Context, key string) (certmagic.KeyInfo, error) {
n.logger.Info(fmt.Sprintf("Stat: %v", key))
var ki certmagic.KeyInfo
key = strings.TrimSuffix(key, "/")
nkey := normalizeNatsKey(key)
k, err := n.Client.Get(nkey)
if err == nats.ErrKeyNotFound {
entries, err := n.List(ctx, nkey, false)
if err != nil {
return ki, fs.ErrNotExist
}
// the key doesn't exists but has children
// so it's a directory
if len(entries) > 0 {
ki.IsTerminal = false
ki.Key = key
return ki, nil
}
return ki, fs.ErrNotExist
} else if err != nil {
return ki, fs.ErrNotExist
}
ki.Key = key
ki.Size = int64(len(k.Value()))
ki.Modified = k.Created()
ki.IsTerminal = true
return ki, nil
}