-
Notifications
You must be signed in to change notification settings - Fork 56
/
baskets_bolt.go
526 lines (427 loc) · 11.4 KB
/
baskets_bolt.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
package main
import (
"encoding/binary"
"encoding/json"
"fmt"
"log"
"net/http"
"strings"
"time"
bolt "go.etcd.io/bbolt"
)
// DbTypeBolt defines name of Bolt database storage
const DbTypeBolt = "bolt"
const (
boltOptExpandPath = 1 << iota
boltOptInsecureTLS
boltOptProxyResponse
)
var (
boltKeyToken = []byte("token")
boltKeyForwardURL = []byte("url")
boltKeyOptions = []byte("opts")
boltKeyCapacity = []byte("capacity")
boltKeyTotalCount = []byte("total")
boltKeyCount = []byte("count")
boltKeyRequests = []byte("requests")
boltKeyResponses = []byte("responses")
)
func itob(i int) []byte {
b := make([]byte, 4)
binary.BigEndian.PutUint32(b, uint32(i))
return b
}
func btoi(b []byte) int {
return int(binary.BigEndian.Uint32(b))
}
func toOpts(config BasketConfig) []byte {
opts := byte(0)
if config.ExpandPath {
opts |= boltOptExpandPath
}
if config.InsecureTLS {
opts |= boltOptInsecureTLS
}
if config.ProxyResponse {
opts |= boltOptProxyResponse
}
return []byte{opts}
}
func fromOpts(opts []byte, config *BasketConfig) {
if len(opts) > 0 {
config.ExpandPath = opts[0]&boltOptExpandPath != 0
config.InsecureTLS = opts[0]&boltOptInsecureTLS != 0
config.ProxyResponse = opts[0]&boltOptProxyResponse != 0
} else {
config.ExpandPath = false
config.InsecureTLS = false
config.ProxyResponse = false
}
}
/// Basket interface ///
type boltBasket struct {
db *bolt.DB
name string
}
func (basket *boltBasket) update(fn func(*bolt.Bucket) error) error {
err := basket.db.Update(func(tx *bolt.Tx) error {
if b := tx.Bucket([]byte(basket.name)); b != nil {
return fn(b)
}
return fmt.Errorf("failed to locate bucket by name")
})
if err != nil {
log.Printf("[error] %s; basket: %s", err, basket.name)
}
return err
}
func (basket *boltBasket) view(fn func(*bolt.Bucket) error) error {
err := basket.db.View(func(tx *bolt.Tx) error {
if b := tx.Bucket([]byte(basket.name)); b != nil {
return fn(b)
}
return fmt.Errorf("failed to locate bucket by name")
})
if err != nil {
log.Printf("[error] %s; basket: %s", err, basket.name)
}
return err
}
func (basket *boltBasket) Config() BasketConfig {
config := BasketConfig{}
basket.view(func(b *bolt.Bucket) error {
config.ForwardURL = string(b.Get(boltKeyForwardURL))
config.Capacity = btoi(b.Get(boltKeyCapacity))
fromOpts(b.Get(boltKeyOptions), &config)
return nil
})
return config
}
func (basket *boltBasket) Update(config BasketConfig) {
basket.update(func(b *bolt.Bucket) error {
oldCap := btoi(b.Get(boltKeyCapacity))
curCount := btoi(b.Get(boltKeyCount))
b.Put(boltKeyForwardURL, []byte(config.ForwardURL))
b.Put(boltKeyOptions, toOpts(config))
b.Put(boltKeyCapacity, itob(config.Capacity))
if oldCap != config.Capacity && curCount > config.Capacity {
// remove overflow requests
remCount := curCount - config.Capacity
reqsCur := b.Bucket(boltKeyRequests).Cursor()
reqsCur.First()
for i := 0; i < remCount; i++ {
reqsCur.Delete()
reqsCur.Next()
}
// update count
b.Put(boltKeyCount, itob(config.Capacity))
}
return nil
})
}
func (basket *boltBasket) Authorize(token string) bool {
result := false
basket.view(func(b *bolt.Bucket) error {
result = string(b.Get(boltKeyToken)) == token
return nil
})
return result
}
func (basket *boltBasket) GetResponse(method string) *ResponseConfig {
var response *ResponseConfig
basket.view(func(b *bolt.Bucket) error {
if resps := b.Bucket(boltKeyResponses); resps != nil {
if resp := resps.Get([]byte(method)); resp != nil {
// parse response configuration
response = new(ResponseConfig)
if err := json.Unmarshal(resp, response); err != nil {
return err
}
}
}
return nil
})
return response
}
func (basket *boltBasket) SetResponse(method string, response ResponseConfig) {
basket.update(func(b *bolt.Bucket) error {
respj, err := json.Marshal(response)
if err != nil {
return err
}
resps := b.Bucket(boltKeyResponses)
if resps == nil {
// first time declaring response
if resps, err = b.CreateBucket(boltKeyResponses); err != nil {
return err
}
}
// save configuration
return resps.Put([]byte(method), respj)
})
}
func (basket *boltBasket) Add(req *http.Request) *RequestData {
data := ToRequestData(req)
basket.update(func(b *bolt.Bucket) error {
reqs := b.Bucket(boltKeyRequests)
dataj, err := json.Marshal(data)
if err != nil {
return err
}
key, _ := reqs.NextSequence()
err = reqs.Put(itob(int(key)), dataj)
if err != nil {
return err
}
// update counters
cap := btoi(b.Get(boltKeyCapacity))
count := btoi(b.Get(boltKeyCount))
total := btoi(b.Get(boltKeyTotalCount))
// total count
total++
b.Put(boltKeyTotalCount, itob(total))
// current count (may not exceed capacity)
if count < cap {
count++
b.Put(boltKeyCount, itob(count))
} else {
// do not increase counter, just remove 1 entry
cur := reqs.Cursor()
cur.First()
cur.Delete()
if count > cap {
// should not happen
log.Printf("[warn] number of requests: %d exceeds capacity: %d; basket: %s", count, cap, basket.name)
}
}
return nil
})
return data
}
func (basket *boltBasket) Clear() {
basket.update(func(b *bolt.Bucket) error {
err := b.DeleteBucket(boltKeyRequests)
if err != nil {
return err
}
// b.Put(boltKeyTotalCount, itob(0)) // reset total stats
b.Put(boltKeyCount, itob(0))
b.CreateBucket(boltKeyRequests)
return nil
})
}
func (basket *boltBasket) Size() int {
result := -1
basket.view(func(b *bolt.Bucket) error {
result = btoi(b.Get(boltKeyCount))
return nil
})
return result
}
func (basket *boltBasket) GetRequests(max int, skip int) RequestsPage {
last := skip + max
page := RequestsPage{make([]*RequestData, 0, max), 0, 0, false}
basket.view(func(b *bolt.Bucket) error {
page.TotalCount = btoi(b.Get(boltKeyTotalCount))
page.Count = btoi(b.Get(boltKeyCount))
cur := b.Bucket(boltKeyRequests).Cursor()
index := 0
for key, val := cur.Last(); key != nil; key, val = cur.Prev() {
if index >= skip && index < last {
request := new(RequestData)
if err := json.Unmarshal(val, request); err != nil {
return err
}
page.Requests = append(page.Requests, request)
} else if index >= last {
page.HasMore = true
break
}
index++
}
return nil
})
return page
}
func (basket *boltBasket) FindRequests(query string, in string, max int, skip int) RequestsQueryPage {
page := RequestsQueryPage{make([]*RequestData, 0, max), false}
basket.view(func(b *bolt.Bucket) error {
cur := b.Bucket(boltKeyRequests).Cursor()
skipped := 0
for key, val := cur.Last(); key != nil; key, val = cur.Prev() {
request := new(RequestData)
if err := json.Unmarshal(val, request); err != nil {
return err
}
// filter
if request.Matches(query, in) {
if skipped < skip {
skipped++
} else {
page.Requests = append(page.Requests, request)
}
}
// early exit
if len(page.Requests) == max {
// check if there are more keys (basket names)
key, _ = cur.Next()
page.HasMore = key != nil
break
}
}
return nil
})
return page
}
/// BasketsDatabase interface ///
type boltDatabase struct {
db *bolt.DB
}
func (bdb *boltDatabase) Create(name string, config BasketConfig) (BasketAuth, error) {
auth := BasketAuth{}
token, err := GenerateToken()
if err != nil {
return auth, fmt.Errorf("failed to generate token: %s", err)
}
err = bdb.db.Update(func(tx *bolt.Tx) error {
b, cerr := tx.CreateBucket([]byte(name))
if cerr != nil {
return fmt.Errorf("failed to create basket: %s - %s", name, cerr)
}
// initialize basket bucket (assuming that no issues arose)
b.Put(boltKeyToken, []byte(token))
b.Put(boltKeyForwardURL, []byte(config.ForwardURL))
b.Put(boltKeyOptions, toOpts(config))
b.Put(boltKeyCapacity, itob(config.Capacity))
b.Put(boltKeyTotalCount, itob(0))
b.Put(boltKeyCount, itob(0))
b.CreateBucket(boltKeyRequests)
return nil
})
if err != nil {
return auth, err
}
auth.Token = token
return auth, nil
}
func (bdb *boltDatabase) Get(name string) Basket {
err := bdb.db.View(func(tx *bolt.Tx) error {
if tx.Bucket([]byte(name)) != nil {
return nil
}
return fmt.Errorf("[warn] no basket found: %s", name)
})
if err != nil {
log.Print(err)
return nil
}
return &boltBasket{bdb.db, name}
}
func (bdb *boltDatabase) Delete(name string) {
err := bdb.db.Update(func(tx *bolt.Tx) error {
return tx.DeleteBucket([]byte(name))
})
if err != nil {
log.Print(err)
}
}
func (bdb *boltDatabase) Size() int {
// TODO : introduce bucket with statistics (e.g. "/stats", or ".stats"), see https://github.com/boltdb/bolt/issues/276
size := 0
bdb.db.View(func(tx *bolt.Tx) error {
cur := tx.Cursor()
for key, _ := cur.First(); key != nil; key, _ = cur.Next() {
size++
}
return nil
})
return size
}
func (bdb *boltDatabase) GetNames(max int, skip int) BasketNamesPage {
last := skip + max
page := BasketNamesPage{make([]string, 0, max), 0, false}
bdb.db.View(func(tx *bolt.Tx) error {
cur := tx.Cursor()
for key, _ := cur.First(); key != nil; key, _ = cur.Next() {
if page.Count >= skip && page.Count < last {
page.Names = append(page.Names, string(key))
} else if page.Count >= last {
page.HasMore = true
// cannot break here, we need to countinue counting
}
page.Count++
}
return nil
})
return page
}
func (bdb *boltDatabase) FindNames(query string, max int, skip int) BasketNamesQueryPage {
page := BasketNamesQueryPage{make([]string, 0, max), false}
bdb.db.View(func(tx *bolt.Tx) error {
skipped := 0
cur := tx.Cursor()
for key, _ := cur.First(); key != nil; key, _ = cur.Next() {
// filter
name := string(key)
if strings.Contains(name, query) {
if skipped < skip {
skipped++
} else {
page.Names = append(page.Names, name)
}
}
// early exit
if len(page.Names) == max {
// check if there are more keys (basket names)
key, _ = cur.Next()
page.HasMore = key != nil
break
}
}
return nil
})
return page
}
func (bdb *boltDatabase) GetStats(max int) DatabaseStats {
stats := DatabaseStats{}
bdb.db.View(func(tx *bolt.Tx) error {
cur := tx.Cursor()
for key, _ := cur.First(); key != nil; key, _ = cur.Next() {
if b := tx.Bucket(key); b != nil {
var lastRequestDate int64
if _, val := b.Bucket(boltKeyRequests).Cursor().Last(); val != nil {
request := new(RequestData)
if err := json.Unmarshal(val, request); err == nil {
lastRequestDate = request.Date
}
}
stats.Collect(&BasketInfo{
Name: string(key),
RequestsCount: btoi(b.Get(boltKeyCount)),
RequestsTotalCount: btoi(b.Get(boltKeyTotalCount)),
LastRequestDate: lastRequestDate}, max)
}
}
return nil
})
stats.UpdateAvarage()
return stats
}
func (bdb *boltDatabase) Release() {
log.Print("[info] closing Bolt database")
err := bdb.db.Close()
if err != nil {
log.Printf("[error] failed to release database file: %s", err)
}
}
// NewBoltDatabase creates an instance of Baskets Database backed with Bolt DB
func NewBoltDatabase(file string) BasketsDatabase {
log.Print("[info] using Bolt database to store baskets")
log.Printf("[info] Bolt database location: %s", file)
db, err := bolt.Open(file, 0600, &bolt.Options{Timeout: 5 * time.Second})
if err != nil {
log.Printf("[error] failed to open Bolt database: %s - %s", file, err)
return nil
}
return &boltDatabase{db}
}