-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpail.go
586 lines (522 loc) · 17.4 KB
/
pail.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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
package pail
import (
"time"
"github.com/couchbase/gocb/v2"
cbsearch "github.com/couchbase/gocb/v2/search"
)
const (
defaultThingName = "_default"
)
type commonRetryable struct {
retries uint32
delay time.Duration
}
func Connect(connStr string, opts gocb.ClusterOptions, retries int, delay time.Duration) (*Cluster, error) {
cluster, err := gocb.Connect(connStr, opts)
if err != nil {
return nil, err
}
return NewCluster(cluster, retries, delay), nil
}
type Cluster struct {
*gocb.Cluster
commonRetryable
}
func NewCluster(cluster *gocb.Cluster, retries int, delay time.Duration) *Cluster {
c := new(Cluster)
c.Cluster = cluster
c.retries = uint32(retries)
c.delay = delay
return c
}
func (c *Cluster) Bucket(bucketName string) *Pail {
return NewPail(c.Cluster.Bucket(bucketName), int(c.retries), c.delay)
}
func (c *Cluster) TryQueryIndexes() *QueryIndexManager {
return NewQueryIndexManager(c.Cluster.QueryIndexes(), int(c.retries), c.delay)
}
func (c *Cluster) QueryOptions(in *gocb.QueryOptions, fn ClusterRetryFunc) (ClusterRetryContext, *gocb.QueryOptions) {
out := new(gocb.QueryOptions)
if in != nil {
*out = *in
}
ctx := NewSimpleClusterRetryContext(c.retries, c.delay, out.RetryStrategy, fn)
out.RetryStrategy = ctx
return ctx, out
}
func (c *Cluster) SearchOptions(in *gocb.SearchOptions, fn ClusterRetryFunc) (ClusterRetryContext, *gocb.SearchOptions) {
out := new(gocb.SearchOptions)
if in != nil {
*out = *in
}
ctx := NewSimpleClusterRetryContext(c.retries, c.delay, out.RetryStrategy, fn)
out.RetryStrategy = ctx
return ctx, out
}
func (c *Cluster) Try(ctx ClusterRetryContext) error {
return ctx.Try(c.Cluster)
}
func (c *Cluster) TryQuery(statement string, opts *gocb.QueryOptions) (*gocb.QueryResult, error) {
var (
res *gocb.QueryResult
ctx ClusterRetryContext
err error
)
ctx, opts = c.QueryOptions(opts, func(cluster *gocb.Cluster) error { res, err = cluster.Query(statement, opts); return err })
if tryErr := c.Try(ctx); tryErr != nil {
return nil, tryErr
}
return res, err
}
func (c *Cluster) TrySearchQuery(indexName string, query cbsearch.Query, opts *gocb.SearchOptions) (*gocb.SearchResult, error) {
var (
res *gocb.SearchResult
ctx ClusterRetryContext
err error
)
ctx, opts = c.SearchOptions(opts, func(c *gocb.Cluster) error { res, err = c.SearchQuery(indexName, query, opts); return err })
if tryErr := c.Try(ctx); tryErr != nil {
return nil, tryErr
}
return res, err
}
type QueryIndexManager struct {
*gocb.QueryIndexManager
commonRetryable
}
func NewQueryIndexManager(queryIndexManager *gocb.QueryIndexManager, retries int, delay time.Duration) *QueryIndexManager {
qm := new(QueryIndexManager)
qm.QueryIndexManager = queryIndexManager
qm.retries = uint32(retries)
qm.delay = delay
return qm
}
func (qm *QueryIndexManager) Try(ctx QueryIndexManagerRetryContext) error {
return ctx.Try(qm.QueryIndexManager)
}
func (qm *QueryIndexManager) CreateQueryIndexOptions(in *gocb.CreateQueryIndexOptions, fn QueryIndexManagerRetryFunc) (QueryIndexManagerRetryContext, *gocb.CreateQueryIndexOptions) {
out := new(gocb.CreateQueryIndexOptions)
if in != nil {
*out = *in
}
ctx := NewSimpleQueryIndexManagerRetryContext(qm.retries, qm.delay, out.RetryStrategy, fn)
out.RetryStrategy = ctx
return ctx, out
}
func (qm *QueryIndexManager) CreatePrimaryQueryIndexOptions(in *gocb.CreatePrimaryQueryIndexOptions, fn QueryIndexManagerRetryFunc) (QueryIndexManagerRetryContext, *gocb.CreatePrimaryQueryIndexOptions) {
out := new(gocb.CreatePrimaryQueryIndexOptions)
if in != nil {
*out = *in
}
ctx := NewSimpleQueryIndexManagerRetryContext(qm.retries, qm.delay, out.RetryStrategy, fn)
out.RetryStrategy = ctx
return ctx, out
}
func (qm *QueryIndexManager) DropQueryIndexOptions(in *gocb.DropQueryIndexOptions, fn QueryIndexManagerRetryFunc) (QueryIndexManagerRetryContext, *gocb.DropQueryIndexOptions) {
out := new(gocb.DropQueryIndexOptions)
if in != nil {
*out = *in
}
ctx := NewSimpleQueryIndexManagerRetryContext(qm.retries, qm.delay, out.RetryStrategy, fn)
out.RetryStrategy = ctx
return ctx, out
}
func (qm *QueryIndexManager) DropPrimaryQueryIndexOptions(in *gocb.DropPrimaryQueryIndexOptions, fn QueryIndexManagerRetryFunc) (QueryIndexManagerRetryContext, *gocb.DropPrimaryQueryIndexOptions) {
out := new(gocb.DropPrimaryQueryIndexOptions)
if in != nil {
*out = *in
}
ctx := NewSimpleQueryIndexManagerRetryContext(qm.retries, qm.delay, out.RetryStrategy, fn)
out.RetryStrategy = ctx
return ctx, out
}
func (qm *QueryIndexManager) GetAllQueryIndexesOptions(in *gocb.GetAllQueryIndexesOptions, fn QueryIndexManagerRetryFunc) (QueryIndexManagerRetryContext, *gocb.GetAllQueryIndexesOptions) {
out := new(gocb.GetAllQueryIndexesOptions)
if in != nil {
*out = *in
}
ctx := NewSimpleQueryIndexManagerRetryContext(qm.retries, qm.delay, out.RetryStrategy, fn)
out.RetryStrategy = ctx
return ctx, out
}
func (qm *QueryIndexManager) BuildDeferredQueryIndexOptions(in *gocb.BuildDeferredQueryIndexOptions, fn QueryIndexManagerRetryFunc) (QueryIndexManagerRetryContext, *gocb.BuildDeferredQueryIndexOptions) {
out := new(gocb.BuildDeferredQueryIndexOptions)
if in != nil {
*out = *in
}
ctx := NewSimpleQueryIndexManagerRetryContext(qm.retries, qm.delay, out.RetryStrategy, fn)
out.RetryStrategy = ctx
return ctx, out
}
func (qm *QueryIndexManager) WatchQueryIndexOptions(in *gocb.WatchQueryIndexOptions, fn QueryIndexManagerRetryFunc) (QueryIndexManagerRetryContext, *gocb.WatchQueryIndexOptions) {
out := new(gocb.WatchQueryIndexOptions)
if in != nil {
*out = *in
}
ctx := NewSimpleQueryIndexManagerRetryContext(qm.retries, qm.delay, out.RetryStrategy, fn)
out.RetryStrategy = ctx
return ctx, out
}
func (qm *QueryIndexManager) TryCreateIndex(bucketName, indexName string, fields []string, opts *gocb.CreateQueryIndexOptions) error {
var (
ctx QueryIndexManagerRetryContext
err error
)
ctx, opts = qm.CreateQueryIndexOptions(opts, func(qm *gocb.QueryIndexManager) error { return qm.CreateIndex(bucketName, indexName, fields, opts) })
if tryErr := qm.Try(ctx); tryErr != nil {
return tryErr
}
return err
}
func (qm *QueryIndexManager) TryCreatePrimaryIndex(bucketName string, opts *gocb.CreatePrimaryQueryIndexOptions) error {
var (
ctx QueryIndexManagerRetryContext
err error
)
ctx, opts = qm.CreatePrimaryQueryIndexOptions(opts, func(qm *gocb.QueryIndexManager) error { return qm.CreatePrimaryIndex(bucketName, opts) })
if tryErr := qm.Try(ctx); tryErr != nil {
return tryErr
}
return err
}
func (qm *QueryIndexManager) TryDropIndex(bucketName, indexName string, opts *gocb.DropQueryIndexOptions) error {
var (
ctx QueryIndexManagerRetryContext
err error
)
ctx, opts = qm.DropQueryIndexOptions(opts, func(qm *gocb.QueryIndexManager) error { return qm.DropIndex(bucketName, indexName, opts) })
if tryErr := qm.Try(ctx); tryErr != nil {
return tryErr
}
return err
}
func (qm *QueryIndexManager) TryDropPrimaryIndex(bucketName string, opts *gocb.DropPrimaryQueryIndexOptions) error {
var (
ctx QueryIndexManagerRetryContext
err error
)
ctx, opts = qm.DropPrimaryQueryIndexOptions(opts, func(qm *gocb.QueryIndexManager) error { return qm.DropPrimaryIndex(bucketName, opts) })
if tryErr := qm.Try(ctx); tryErr != nil {
return tryErr
}
return err
}
func (qm *QueryIndexManager) TryGetAllIndexes(bucketName string, opts *gocb.GetAllQueryIndexesOptions) ([]gocb.QueryIndex, error) {
var (
res []gocb.QueryIndex
ctx QueryIndexManagerRetryContext
err error
)
ctx, opts = qm.GetAllQueryIndexesOptions(opts, func(qm *gocb.QueryIndexManager) error { res, err = qm.GetAllIndexes(bucketName, opts); return err })
if tryErr := qm.Try(ctx); tryErr != nil {
return nil, tryErr
}
return res, err
}
func (qm *QueryIndexManager) TryBuildDeferredIndexes(bucketName string, opts *gocb.BuildDeferredQueryIndexOptions) ([]string, error) {
var (
res []string
ctx QueryIndexManagerRetryContext
err error
)
ctx, opts = qm.BuildDeferredQueryIndexOptions(opts, func(qm *gocb.QueryIndexManager) error {
res, err = qm.BuildDeferredIndexes(bucketName, opts)
return err
})
if tryErr := qm.Try(ctx); tryErr != nil {
return nil, tryErr
}
return res, err
}
// Pail is our gocb.Bucket wrapper, providing retry goodness.
type Pail struct {
*gocb.Bucket
commonRetryable
}
func NewPail(bucket *gocb.Bucket, retries int, delay time.Duration) *Pail {
p := new(Pail)
p.Bucket = bucket
p.retries = uint32(retries)
p.delay = delay
return p
}
func (p *Pail) Scope(scopeName string) *Scope {
scope := new(Scope)
scope.Scope = p.Bucket.Scope(scopeName)
scope.commonRetryable = p.commonRetryable
return scope
}
func (p *Pail) DefaultScope() *Scope {
return p.Scope(defaultThingName)
}
func (p *Pail) Collection(collectionName string) *Collection {
return p.DefaultScope().Collection(collectionName)
}
func (p *Pail) DefaultCollection() *Collection {
return p.Collection(defaultThingName)
}
func (p *Pail) ScopeCollection(scopeName, collectionName string) *Collection {
return p.Scope(scopeName).Collection(collectionName)
}
type Scope struct {
*gocb.Scope
commonRetryable
}
func (s *Scope) Collection(collectionName string) *Collection {
c := new(Collection)
c.Collection = s.Scope.Collection(collectionName)
c.commonRetryable = s.commonRetryable
return c
}
func (s *Scope) DefaultCollection() *Collection {
return s.Collection(defaultThingName)
}
type Collection struct {
*gocb.Collection
commonRetryable
}
// Try will attempt to execute retryFunc up to retries+1 times or until a
// non-connection-related error is seen.
func (c *Collection) Try(ctx CollectionRetryContext) error {
return ctx.Try(c.Collection)
}
func (c *Collection) GetOptions(in *gocb.GetOptions, fn CollectionRetryFunc) (CollectionRetryContext, *gocb.GetOptions) {
out := new(gocb.GetOptions)
if in != nil {
*out = *in
}
ctx := NewSimpleCollectionRetryContext(c.retries, c.delay, out.RetryStrategy, fn)
out.RetryStrategy = ctx
return ctx, out
}
func (c *Collection) TouchOptions(in *gocb.TouchOptions, fn CollectionRetryFunc) (CollectionRetryContext, *gocb.TouchOptions) {
out := new(gocb.TouchOptions)
if in != nil {
*out = *in
}
ctx := NewSimpleCollectionRetryContext(c.retries, c.delay, out.RetryStrategy, fn)
out.RetryStrategy = ctx
return ctx, out
}
func (c *Collection) UpsertOptions(in *gocb.UpsertOptions, fn CollectionRetryFunc) (CollectionRetryContext, *gocb.UpsertOptions) {
out := new(gocb.UpsertOptions)
if in != nil {
*out = *in
}
ctx := NewSimpleCollectionRetryContext(c.retries, c.delay, out.RetryStrategy, fn)
out.RetryStrategy = ctx
return ctx, out
}
func (c *Collection) InsertOptions(in *gocb.InsertOptions, fn CollectionRetryFunc) (CollectionRetryContext, *gocb.InsertOptions) {
out := new(gocb.InsertOptions)
if in != nil {
*out = *in
}
ctx := NewSimpleCollectionRetryContext(c.retries, c.delay, out.RetryStrategy, fn)
out.RetryStrategy = ctx
return ctx, out
}
func (c *Collection) ReplaceOptions(in *gocb.ReplaceOptions, fn CollectionRetryFunc) (CollectionRetryContext, *gocb.ReplaceOptions) {
out := new(gocb.ReplaceOptions)
if in != nil {
*out = *in
}
ctx := NewSimpleCollectionRetryContext(c.retries, c.delay, out.RetryStrategy, fn)
out.RetryStrategy = ctx
return ctx, out
}
func (c *Collection) RemoveOptions(in *gocb.RemoveOptions, fn CollectionRetryFunc) (CollectionRetryContext, *gocb.RemoveOptions) {
out := new(gocb.RemoveOptions)
if in != nil {
*out = *in
}
ctx := NewSimpleCollectionRetryContext(c.retries, c.delay, out.RetryStrategy, fn)
out.RetryStrategy = ctx
return ctx, out
}
func (c *Collection) IncrementOptions(in *gocb.IncrementOptions, fn CollectionRetryFunc) (CollectionRetryContext, *gocb.IncrementOptions) {
out := new(gocb.IncrementOptions)
if in != nil {
*out = *in
}
ctx := NewSimpleCollectionRetryContext(c.retries, c.delay, out.RetryStrategy, fn)
out.RetryStrategy = ctx
return ctx, out
}
func (c *Collection) DecrementOptions(in *gocb.DecrementOptions, fn CollectionRetryFunc) (CollectionRetryContext, *gocb.DecrementOptions) {
out := new(gocb.DecrementOptions)
if in != nil {
*out = *in
}
ctx := NewSimpleCollectionRetryContext(c.retries, c.delay, out.RetryStrategy, fn)
out.RetryStrategy = ctx
return ctx, out
}
func (c *Collection) AppendOptions(in *gocb.AppendOptions, fn CollectionRetryFunc) (CollectionRetryContext, *gocb.AppendOptions) {
out := new(gocb.AppendOptions)
if in != nil {
*out = *in
}
ctx := NewSimpleCollectionRetryContext(c.retries, c.delay, out.RetryStrategy, fn)
out.RetryStrategy = ctx
return ctx, out
}
func (c *Collection) PrependOptions(in *gocb.PrependOptions, fn CollectionRetryFunc) (CollectionRetryContext, *gocb.PrependOptions) {
out := new(gocb.PrependOptions)
if in != nil {
*out = *in
}
ctx := NewSimpleCollectionRetryContext(c.retries, c.delay, out.RetryStrategy, fn)
out.RetryStrategy = ctx
return ctx, out
}
func (c *Collection) BulkOpOptions(in *gocb.BulkOpOptions, fn CollectionRetryFunc) (CollectionRetryContext, *gocb.BulkOpOptions) {
out := new(gocb.BulkOpOptions)
if in != nil {
*out = *in
}
ctx := NewSimpleCollectionRetryContext(c.retries, c.delay, out.RetryStrategy, fn)
out.RetryStrategy = ctx
return ctx, out
}
func (c *Collection) TryDo(ops []gocb.BulkOp, opts *gocb.BulkOpOptions) error {
var (
ctx CollectionRetryContext
err error
)
ctx, opts = c.BulkOpOptions(opts, func(c *gocb.Collection) error { err = c.Do(ops, opts); return err })
if tryErr := c.Try(ctx); tryErr != nil {
return tryErr
}
return err
}
func (c *Collection) TryGet(id string, opts *gocb.GetOptions) (*gocb.GetResult, error) {
var (
res *gocb.GetResult
ctx CollectionRetryContext
err error
)
ctx, opts = c.GetOptions(opts, func(c *gocb.Collection) error { res, err = c.Get(id, opts); return err })
if tryErr := c.Try(ctx); tryErr != nil {
return nil, tryErr
}
return res, err
}
func (c *Collection) TryGetContent(id string, ptr interface{}, opts *gocb.GetOptions) (*gocb.GetResult, error) {
var (
res *gocb.GetResult
err error
)
if res, err = c.TryGet(id, opts); err != nil {
return nil, err
}
return res, res.Content(ptr)
}
func (c *Collection) TryTouch(id string, expiry time.Duration, opts *gocb.TouchOptions) (*gocb.MutationResult, error) {
var (
res *gocb.MutationResult
ctx CollectionRetryContext
err error
)
ctx, opts = c.TouchOptions(opts, func(c *gocb.Collection) error { res, err = c.Touch(id, expiry, opts); return err })
if tryErr := c.Try(ctx); tryErr != nil {
return nil, tryErr
}
return res, err
}
func (c *Collection) TryUpsert(id string, value interface{}, opts *gocb.UpsertOptions) (*gocb.MutationResult, error) {
var (
res *gocb.MutationResult
ctx CollectionRetryContext
err error
)
ctx, opts = c.UpsertOptions(opts, func(c *gocb.Collection) error { res, err = c.Upsert(id, value, opts); return err })
if tryErr := c.Try(ctx); tryErr != nil {
return nil, tryErr
}
return res, err
}
func (c *Collection) TryInsert(id string, value interface{}, opts *gocb.InsertOptions) (*gocb.MutationResult, error) {
var (
res *gocb.MutationResult
ctx CollectionRetryContext
err error
)
ctx, opts = c.InsertOptions(opts, func(c *gocb.Collection) error { res, err = c.Insert(id, value, opts); return err })
if tryErr := c.Try(ctx); tryErr != nil {
return nil, tryErr
}
return res, err
}
func (c *Collection) TryReplace(id string, value interface{}, opts *gocb.ReplaceOptions) (*gocb.MutationResult, error) {
var (
res *gocb.MutationResult
ctx CollectionRetryContext
err error
)
ctx, opts = c.ReplaceOptions(opts, func(c *gocb.Collection) error { res, err = c.Replace(id, value, opts); return err })
if tryErr := c.Try(ctx); tryErr != nil {
return nil, tryErr
}
return res, err
}
func (c *Collection) TryRemove(id string, opts *gocb.RemoveOptions) (*gocb.MutationResult, error) {
var (
res *gocb.MutationResult
ctx CollectionRetryContext
err error
)
ctx, opts = c.RemoveOptions(opts, func(c *gocb.Collection) error { res, err = c.Remove(id, opts); return err })
if tryErr := c.Try(ctx); tryErr != nil {
return nil, tryErr
}
return res, err
}
func (c *Collection) TryIncrement(id string, opts *gocb.IncrementOptions) (*gocb.CounterResult, error) {
var (
res *gocb.CounterResult
ctx CollectionRetryContext
err error
)
ctx, opts = c.IncrementOptions(opts, func(c *gocb.Collection) error { res, err = c.Binary().Increment(id, opts); return err })
if tryErr := c.Try(ctx); tryErr != nil {
return nil, tryErr
}
return res, err
}
func (c *Collection) TryDecrement(id string, opts *gocb.DecrementOptions) (*gocb.CounterResult, error) {
var (
res *gocb.CounterResult
ctx CollectionRetryContext
err error
)
ctx, opts = c.DecrementOptions(opts, func(c *gocb.Collection) error { res, err = c.Binary().Decrement(id, opts); return err })
if tryErr := c.Try(ctx); tryErr != nil {
return nil, tryErr
}
return res, err
}
func (c *Collection) TryAppend(id string, value []byte, opts *gocb.AppendOptions) (*gocb.MutationResult, error) {
var (
res *gocb.MutationResult
ctx CollectionRetryContext
err error
)
ctx, opts = c.AppendOptions(opts, func(c *gocb.Collection) error { res, err = c.Binary().Append(id, value, opts); return err })
if tryErr := c.Try(ctx); tryErr != nil {
return nil, tryErr
}
return res, err
}
func (c *Collection) TryPrepend(id string, value []byte, opts *gocb.PrependOptions) (*gocb.MutationResult, error) {
var (
res *gocb.MutationResult
ctx CollectionRetryContext
err error
)
ctx, opts = c.PrependOptions(opts, func(c *gocb.Collection) error { res, err = c.Binary().Prepend(id, value, opts); return err })
if tryErr := c.Try(ctx); tryErr != nil {
return nil, tryErr
}
return res, err
}