-
Notifications
You must be signed in to change notification settings - Fork 0
/
tx.go
112 lines (98 loc) · 2.24 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
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
package pqxd
import (
"database/sql/driver"
"github.com/aws/aws-sdk-go-v2/service/dynamodb/types"
"log/slog"
"sync"
)
// compatibility check
var _ driver.Tx = (*connection)(nil)
// Commit See: driver.Tx
func (c *connection) Commit() error {
if c.closed.Load() {
return driver.ErrBadConn
}
if !c.txOngoing.Load() {
slog.Warn("pqxd: commit was performed, but transaction is not ongoing")
return nil
}
c.txCommiter.Load().commit()
return nil
}
func (c *connection) Rollback() error {
if c.closed.Load() {
return driver.ErrBadConn
}
if !c.txOngoing.Load() {
slog.Warn("pqxd: rollback was performed, but transaction is not ongoing")
return nil
}
c.txRollbacker.Load().rollback()
return nil
}
// transactionInOut is the input/output of a transaction.
type transactionInOut struct {
input types.ParameterizedStatement
output map[string]types.AttributeValue
err error
}
// transationStatementPublisher publishes statements in a transaction.
type transactionStatementPublisher struct {
ch chan *transactionInOut
closeOnce sync.Once
}
// publish publishes a statement.
func (p *transactionStatementPublisher) publish(inout *transactionInOut) {
p.ch <- inout
}
// close closes the channel.
func (p *transactionStatementPublisher) close() {
p.closeOnce.Do(func() {
close(p.ch)
})
}
// transactionCommitter commits a transaction.
type transactionCommitter struct {
ch chan<- struct{}
done <-chan struct{}
commitOnce sync.Once
closeOnce sync.Once
}
// commit commits the transaction.
func (c *transactionCommitter) commit() {
c.commitOnce.Do(func() {
c.ch <- struct{}{}
<-c.done
})
}
// close closes the channel.
func (c *transactionCommitter) close() {
c.closeOnce.Do(func() {
close(c.ch)
})
}
// transactionRollbacker rolls back a transaction.
type transactionRollbacker struct {
ch chan<- struct{}
done <-chan struct{}
rollbackOnce sync.Once
closeOnce sync.Once
}
// rollback rolls back the transaction.
func (r *transactionRollbacker) rollback() {
r.rollbackOnce.Do(func() {
r.ch <- struct{}{}
select {
case _, ok := <-r.done:
if ok {
return
}
}
})
}
// close closes the channel.
func (r *transactionRollbacker) close() {
r.closeOnce.Do(func() {
close(r.ch)
})
}