-
Notifications
You must be signed in to change notification settings - Fork 0
/
sqltx.go
212 lines (186 loc) · 5.18 KB
/
sqltx.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
package sqltx
import (
"context"
"database/sql"
"fmt"
"reflect"
"time"
"github.com/cenkalti/backoff/v4"
)
// TXFn is a transaction function that performs the steps of a transaction
type TXFn func(tx TXer) error
// TXBeginner begins a database transaction
type TXBeginner interface {
BeginTx(ctx context.Context, opts *sql.TxOptions) (TXer, error)
}
// RetryableErrorTester tests if an error is to be retried in the transaction
type RetryableErrorTester interface {
IsRetryable(err error) bool
}
// Sleeper interface for context aware sleep
type Sleeper interface {
Sleep(ctx context.Context, d time.Duration)
}
// DefaultSleeper is the default sleeper
type DefaultSleeper struct {
}
// Options contains transaction options
type Options struct {
Name string
Isolation sql.IsolationLevel
ReadOnly bool
Retries int
Backoff backoff.BackOff
Sleeper Sleeper
}
const (
// DefaultRetries is the default number of retries to perform
DefaultRetries = 20
// DefaultBackoffInitialInterval is the default backoff initial interval
DefaultBackoffInitialInterval = 1 * time.Millisecond
// DefaultBackoffMaxInterval is the default backoff max interval
DefaultBackoffMaxInterval = 5 * time.Second
)
var (
maxRetries = DefaultRetries
backoffObject backoff.BackOff = &backoff.ExponentialBackOff{
InitialInterval: DefaultBackoffInitialInterval,
RandomizationFactor: backoff.DefaultRandomizationFactor,
Multiplier: backoff.DefaultMultiplier,
MaxInterval: backoff.DefaultMaxInterval,
MaxElapsedTime: DefaultBackoffMaxInterval,
Stop: backoff.Stop,
Clock: backoff.SystemClock,
}
testers []RetryableErrorTester
defaultSleeper = &DefaultSleeper{}
)
type sqlDB struct {
db *sql.DB
}
// SetDefaultMaxRetries sets the default value for max retries
// Only set this once during initialization
func SetDefaultMaxRetries(retries int) {
maxRetries = retries
}
// GetDefaultMaxRetries returns the default max number of retries
func GetDefaultMaxRetries() int {
return maxRetries
}
// SetDefaultBackoff sets the default value for the backoff
// Only set this once during initialization
func SetDefaultBackoff(b backoff.BackOff) {
backoffObject = b
}
// GetDefaultBackoff returns the default backoff object
func GetDefaultBackoff() backoff.BackOff {
return backoffObject
}
// RegisterRetryableErrorTester registers a test function that is used
// to determine if a database error should be retried or not
func RegisterRetryableErrorTester(t RetryableErrorTester) {
testers = append(testers, t)
}
// Tx performs a database transaction
// If an error occurres the transaction will be automatically rolled back
// The transaction will be retried up to 20 times if the server reports a
// retryable error
func Tx(ctx context.Context, db *sql.DB, opts *Options, fn TXFn) (err error) {
return TxHandler(ctx, &sqlDB{db}, opts, fn)
}
// TxHandler is the handler that handles transaction details
func TxHandler(ctx context.Context, db TXBeginner, opts *Options, fn TXFn) (err error) {
if len(testers) == 0 {
panic("no sqltx error testers registered, please register by importing the relevant sqltx DB implementation(s)")
}
var tx TXer
defer func() {
nilTX := tx == nil || (reflect.ValueOf(tx).Kind() == reflect.Ptr && reflect.ValueOf(tx).IsNil())
if p := recover(); p != nil {
// A panic occurred, rollback and re-panic
if !nilTX {
_ = tx.Rollback()
}
panic(p)
} else if err != nil {
// Something went wrong, rollback
if !nilTX {
_ = tx.Rollback()
}
}
}()
var b backoff.BackOff
useBackoff := backoffObject
retries := maxRetries
var txOpts *sql.TxOptions
var sleeper Sleeper = defaultSleeper
var name = ""
if opts != nil {
txOpts = &sql.TxOptions{
Isolation: opts.Isolation,
ReadOnly: opts.ReadOnly,
}
if opts.Retries > 0 {
retries = opts.Retries
}
if opts.Backoff != nil {
useBackoff = opts.Backoff
}
if opts.Sleeper != nil {
sleeper = opts.Sleeper
}
if opts.Name != "" {
name = " '" + opts.Name + "'"
}
}
for i := 0; i < retries; i++ {
tx, err = db.BeginTx(ctx, txOpts)
if err != nil {
return fmt.Errorf("failed to start transaction%s: %w", name, err)
}
err = fn(tx)
if err == nil {
err = tx.Commit()
if err != nil {
err = fmt.Errorf("transaction%s commit error: %w", name, err)
}
}
if err != nil {
retryable := false
for _, tester := range testers {
if tester.IsRetryable(err) {
retryable = true
break
}
}
if !retryable {
return err
}
// Retryable error, try again (soon)
if b == nil {
b = useBackoff
b.Reset()
}
sleepTime := b.NextBackOff()
if sleepTime == backoff.Stop {
return fmt.Errorf("transaction%s backoff max time reached", name)
}
sleeper.Sleep(ctx, sleepTime)
continue
}
return err
}
return fmt.Errorf("transaction%s max retry count (%d) exceeded. Last error: %w", name, retries, err)
}
// Sleep sleeps the duration of d
func (s DefaultSleeper) Sleep(ctx context.Context, d time.Duration) {
t := time.NewTimer(d)
defer t.Stop()
select {
case <-ctx.Done():
case <-t.C:
}
}
func (d *sqlDB) BeginTx(ctx context.Context, opts *sql.TxOptions) (TXer, error) {
return d.db.BeginTx(ctx, opts)
}