-
Notifications
You must be signed in to change notification settings - Fork 1
/
transaction.go
154 lines (130 loc) · 2.78 KB
/
transaction.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
package saga
import (
"context"
"log"
)
//type MySQLTxContext struct {
// Tx *sql.Tx
//}
//
//type MySQLTxHandler struct {
// DB *sql.DB
//}
//
//func (ctor *MySQLTxHandler) BeginTx(context context.Context) (ctx MySQLTxContext, error error) {
// tx, err := ctor.DB.BeginTx(context, nil)
// if err != nil {
// return MySQLTxContext{}, err
// }
//
// return MySQLTxContext{Tx: tx}, nil
//}
//
//func (ctor *MySQLTxHandler) Commit(ctx MySQLTxContext) error {
// if err := ctx.Tx.Commit(); err != nil {
// return err
// }
//
// return nil
//}
//
//func (ctor *MySQLTxHandler) Rollback(ctx MySQLTxContext) error {
// if err := ctx.Tx.Rollback(); err != nil {
// return err
// }
//
// return nil
//}
type TxContext interface{}
type TxHandler[Tx TxContext] interface {
BeginTx(ctx context.Context) (tx Tx, error error)
Commit(ctx Tx) error
Rollback(ctx Tx) error
}
type Executable[Tx TxContext] func(ctx Tx) error
func CombineExecutables[Tx TxContext](executables ...Executable[Tx]) Executable[Tx] {
return func(ctx Tx) error {
for _, executable := range executables {
err := executable(ctx)
if err != nil {
return err
}
}
return nil
}
}
type UnitOfWorkFactory[Tx TxContext] func(ctx context.Context) (*UnitOfWork[Tx], error)
func NewUnitOfWorkFactory[Tx TxContext](handler TxHandler[Tx]) UnitOfWorkFactory[Tx] {
return func(ctx context.Context) (*UnitOfWork[Tx], error) {
return NewUnitOfWork[Tx](ctx, handler), nil
}
}
type UnitOfWork[Tx TxContext] struct {
handler TxHandler[Tx]
unitChan chan Executable[Tx]
ctx context.Context
commited bool
}
func NewUnitOfWork[Tx TxContext](ctx context.Context, handler TxHandler[Tx]) *UnitOfWork[Tx] {
return &UnitOfWork[Tx]{
handler: handler,
unitChan: make(chan Executable[Tx], 100),
ctx: ctx,
}
}
func (u *UnitOfWork[Tx]) AddWorkUnit(workUnit Executable[Tx]) error {
if u.commited {
return ErrUnitOfWorkImmutable
}
u.unitChan <- workUnit
return nil
}
func (u *UnitOfWork[Tx]) commitExecutables(ctx Tx) error {
if u.commited {
return ErrUnitOfWorkImmutable
}
u.commited = true
backupChan := make(chan Executable[Tx], 100)
errors := make(chan error, 100)
defer close(errors)
commitLoop:
for {
select {
case executable := <-u.unitChan:
backupChan <- executable
err := executable(ctx)
if err != nil {
errors <- err
}
default:
close(u.unitChan)
break commitLoop
}
}
select {
case err := <-errors:
u.unitChan = backupChan
return err
default:
break
}
close(backupChan)
log.Print()
return nil
}
func (u *UnitOfWork[Tx]) Commit() error {
tx, err := u.handler.BeginTx(u.ctx)
defer u.handler.Rollback(tx)
if err != nil {
return err
}
err = u.commitExecutables(tx)
if err != nil {
return err
}
err = u.handler.Commit(tx)
if err != nil {
return err
}
return nil
}