forked from OneOfOne/slowbolt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdb.go
61 lines (53 loc) · 1.08 KB
/
db.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
package slowbolt
import (
"os"
"runtime"
"time"
"go.etcd.io/bbolt"
)
type (
Tx = bbolt.Tx
TxStats = bbolt.TxStats
Bucket = bbolt.Bucket
BucketStats = bbolt.BucketStats
Options = bbolt.Options
)
func Open(path string, mode os.FileMode, options *Options) (*DB, error) {
db, err := bbolt.Open(path, mode, options)
if err != nil {
return nil, err
}
return &DB{DB: db, SlowDuration: time.Minute}, nil
}
type DB struct {
*bbolt.DB
OnSlow func(op, fn, file string, line int)
SlowDuration time.Duration
}
func (b *DB) Update(fn func(*Tx) error) error {
if b.SlowDuration == -1 {
return b.DB.Update(fn)
}
var pcs [2]uintptr
frames := runtime.CallersFrames(pcs[:runtime.Callers(2, pcs[:])])
start := time.Now()
err := b.DB.Update(fn)
if took := time.Since(start); took > b.SlowDuration {
var (
fn string
file string
line int
)
for {
frame, more := frames.Next()
if !more {
break
}
fn, file, line = frame.Function, frame.File, frame.Line
}
if fn != "" {
b.OnSlow("update", fn, file, line)
}
}
return err
}