-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathtx.go
More file actions
113 lines (95 loc) · 2.44 KB
/
tx.go
File metadata and controls
113 lines (95 loc) · 2.44 KB
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
package pgsql
import (
"context"
"database/sql"
"errors"
"time"
)
// ErrAbortTx rollbacks transaction and return nil error
var ErrAbortTx = errors.New("pgsql: abort tx")
// BeginTxer type
type BeginTxer interface {
BeginTx(context.Context, *sql.TxOptions) (*sql.Tx, error)
}
// BackoffDelayFunc is a function type that defines the delay for backoff
type BackoffDelayFunc func(attempt int) time.Duration
// TxOptions is the transaction options
type TxOptions struct {
sql.TxOptions
MaxAttempts int
BackoffDelayFunc BackoffDelayFunc
}
const (
defaultMaxAttempts = 10
)
// RunInTx runs fn inside retryable transaction.
//
// see RunInTxContext for more info.
func RunInTx(db BeginTxer, opts *TxOptions, fn func(*sql.Tx) error) error {
return RunInTxContext(context.Background(), db, opts, fn)
}
// RunInTxContext runs fn inside retryable transaction with context.
// It use Serializable isolation level if tx options isolation is setted to sql.LevelDefault.
//
// RunInTxContext DO NOT handle panic.
// But when panic, it will rollback the transaction.
func RunInTxContext(ctx context.Context, db BeginTxer, opts *TxOptions, fn func(*sql.Tx) error) error {
option := TxOptions{
TxOptions: sql.TxOptions{
Isolation: sql.LevelSerializable,
},
MaxAttempts: defaultMaxAttempts,
}
if opts != nil {
if opts.MaxAttempts > 0 {
option.MaxAttempts = opts.MaxAttempts
}
option.TxOptions = opts.TxOptions
// override default isolation level to serializable
if opts.Isolation == sql.LevelDefault {
option.Isolation = sql.LevelSerializable
}
option.BackoffDelayFunc = opts.BackoffDelayFunc
}
f := func() error {
tx, err := db.BeginTx(ctx, &option.TxOptions)
if err != nil {
return err
}
// use defer to also rollback when panic
defer tx.Rollback()
err = fn(tx)
if err != nil {
return err
}
return tx.Commit()
}
var err error
for i := 0; i < option.MaxAttempts; i++ {
err = f()
if err == nil || errors.Is(err, ErrAbortTx) {
return nil
}
if !IsSerializationFailure(err) {
return err
}
if i < option.MaxAttempts-1 && option.BackoffDelayFunc != nil {
if err = wait(ctx, i, option.BackoffDelayFunc); err != nil {
return err
}
}
}
return err
}
func wait(ctx context.Context, attempt int, backOffDelayFunc BackoffDelayFunc) error {
delay := backOffDelayFunc(attempt)
if delay <= 0 {
return nil
}
select {
case <-ctx.Done():
return ctx.Err()
case <-time.After(delay):
return nil
}
}