-
-
Notifications
You must be signed in to change notification settings - Fork 21
/
gen_tx.go
137 lines (117 loc) · 3.54 KB
/
gen_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
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
// DO NOT MODIFY! AUTO GENERATED BY igo v1.0.3 (https://github.com/rocketlaunchr/igo)
// Copyright 2019-20 PJ Engineering and Business Solutions Pty. Ltd. All rights reserved.
package dbq
import (
"context"
"database/sql"
"fmt"
"time"
rlSql "github.com/rocketlaunchr/mysql-go"
// "gopkg.in/cenkalti/backoff.v4"
"github.com/cenkalti/backoff/v4"
)
type txer interface {
Commit() error
Rollback() error
}
// QFn is shorthand for Q. It will automatically use the appropriate transaction.
type QFn func(ctx context.Context, query string, options *Options, args ...interface{}) (interface{}, error)
// EFn is shorthand for E. It will automatically use the appropriate transaction.
type EFn func(ctx context.Context, query string, options *Options, args ...interface{}) (sql.Result, error)
// TxCommit will commit the transaction.
type TxCommit func() error
// Tx is used to perform an arbitrarily complex operation and not have to worry about rolling back a transaction.
// The transaction is automatically rolled back unless explicitly committed by calling txCommit.
// tx is only exposed for performance purposes. Do not use it to commit or rollback.
//
// NOTE: Until this note is removed, this function is not necessarily backward compatible.
//
// Example:
//
// ctx := context.Background()
// pool, _ := sql.Open("mysql", "user:password@tcp(localhost:3306)/db")
//
// dbq.Tx(ctx, pool, func(tx interface{}, Q dbq.QFn, E dbq.EFn, txCommit dbq.TxCommit) {
// stmt := dbq.INSERTStmt("table", []string{"name", "age", "created_at"}, 1)
// res, err := E(ctx, stmt, nil, "test name", 34, time.Now())
// if err != nil {
// return // Automatic rollback
// }
// txCommit()
// })
//
func Tx(ctx context.Context, db interface{}, fn func(tx interface{}, Q QFn, E EFn, txCommit TxCommit), retryPolicy ...backoff.BackOff) error {
if ctx == nil {
ctx = context.Background()
}
var (
alreadyTx bool
tx interface{}
err error
)
switch db := db.(type) {
case BeginTxer:
tx, err = db.BeginTx(ctx, nil)
if err != nil {
return err
}
case beginTxer2:
tx, err = db.BeginTx(ctx, nil)
if err != nil {
return err
}
case *sql.Tx, *rlSql.Tx:
tx = db
alreadyTx = true
default:
panic(fmt.Sprintf("interface conversion: %T is not dbq.BeginTxer: missing method: BeginTx", db))
}
defer func() {
if r := recover(); r != nil {
tx.(txer).Rollback()
panic(r)
}
}()
qFn := func(ctx context.Context, query string, options *Options, args ...interface{}) (interface{}, error) {
res, err := Q(ctx, tx, query, options, args...)
if err == sql.ErrTxDone && !alreadyTx {
return Q(ctx, db, query, options, args...)
}
return res, err
}
eFn := func(ctx context.Context, query string, options *Options, args ...interface{}) (sql.Result, error) {
return E(ctx, tx.(ExecContexter), query, options, args...)
}
completed := false
txCommit := func() error {
err := tx.(txer).Commit()
if err == nil || err == sql.ErrTxDone {
completed = true
return nil
}
return err
}
operation := func() error {
fn(tx, qFn, eFn, txCommit)
if completed {
return nil
}
op2 := func() error {
err = tx.(txer).Rollback()
if err == sql.ErrTxDone {
return nil
}
return err
}
exp := ExponentialRetryPolicy(120 * time.Second)
err := backoff.Retry(op2, backoff.WithContext(exp, ctx))
if err != nil {
return &backoff.PermanentError{err}
}
return nil
}
if !(len(retryPolicy) > 0 && retryPolicy[0] != nil) {
return operation()
}
return backoff.Retry(operation, backoff.WithContext(retryPolicy[0], ctx))
}