-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmanager.go
151 lines (130 loc) · 2.65 KB
/
manager.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
package tx
import (
"context"
"sync/atomic"
)
type txManager struct {
db DB
}
func newTxManager(db DB) *txManager {
t := &txManager{db: db}
return t
}
func existTx(ctx context.Context) bool {
if ctx == nil {
ctx = context.Background()
}
_, ok := ctx.Value(txCtxKey).(*TxNode)
return ok
}
func (t *txManager) NewTxNode(ctx context.Context, fc TxFunc, txOpts *TxOptions) *TxNode {
var node *TxNode
if ctx == nil {
ctx = context.Background()
}
pNode, ok := ctx.Value(txCtxKey).(*TxNode)
if ok {
node = &TxNode{
parent: pNode,
tx: pNode.tx,
db: pNode.db,
fc: fc,
txOpts: txOpts,
}
} else {
node = &TxNode{
db: t.db,
fc: fc,
txOpts: txOpts,
}
}
node.withCtx(ctx)
return node
}
func (t *txManager) WithTx(ctx context.Context, fc TxFunc, opts ...TxOption) (err error) {
txOpts := &TxOptions{Propagation: Required}
for _, opt := range opts {
opt(txOpts)
}
txNode := t.NewTxNode(ctx, fc, txOpts)
switch txOpts.Propagation {
case Required:
if !existTx(ctx) { // 不存在就创建
return txNode.newTx()
} else {
return txNode.useTx()
}
case Supported:
if existTx(ctx) {
return txNode.useTx()
} else {
return txNode.nonTx(ctx)
}
case Nested:
if !existTx(ctx) { // 不存在就创建
return txNode.newTx()
} else { // 存在就嵌套
return txNode.subTx()
}
case New_:
return txNode.newTx()
}
return
}
func WithTx(ctx context.Context, fc TxFunc, opts ...TxOption) (err error) {
name := defaultKey.Load().(string)
options := &TxOptions{Name: name}
for _, opt := range opts {
opt(options)
}
return GetTxManager(options.Name).WithTx(ctx, fc, opts...)
}
func (t *txManager) TxFromContext(ctx context.Context) (tx Tx) {
if ctx == nil {
return nil
}
node, ok := ctx.Value(txCtxKey).(*TxNode)
if !ok {
return nil
}
return node.tx
}
func (t *txManager) DBFromContext(ctx context.Context) (db DB) {
if ctx == nil {
return t.db
}
node, ok := ctx.Value(txCtxKey).(*TxNode)
if !ok {
return t.db
}
return node.db
}
type TxFunc func(ctx context.Context) error
var txManagers = map[string]*txManager{}
func GetTxManager(name string) *txManager {
return txManagers[name]
}
var defaultKey atomic.Value
func init() {
defaultKey.Store("")
}
func Register(db DB, opts ...RegisterDBOption) {
o := &RegisterDBOptions{}
for _, opt := range opts {
opt(o)
}
name := db.Name()
if o.Default {
defaultKey.Store(name)
}
txManagers[name] = newTxManager(db)
}
type RegisterDBOptions struct {
Default bool
}
type RegisterDBOption func(opt *RegisterDBOptions)
func RegisterDefaultDB(def bool) RegisterDBOption {
return func(opt *RegisterDBOptions) {
opt.Default = def
}
}