-
Notifications
You must be signed in to change notification settings - Fork 6
/
tx.go
86 lines (73 loc) · 1.82 KB
/
tx.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
package pgsql
import (
"context"
"database/sql"
"errors"
)
// 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)
}
// TxOptions is the transaction options
type TxOptions struct {
sql.TxOptions
MaxAttempts int
}
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
}
}
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
}
}
return err
}