-
Notifications
You must be signed in to change notification settings - Fork 217
/
cmd_transactions.go
179 lines (155 loc) · 3.08 KB
/
cmd_transactions.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
// Commands from https://redis.io/commands#transactions
package miniredis
import (
"github.com/alicebob/miniredis/v2/server"
)
// commandsTransaction handles MULTI &c.
func commandsTransaction(m *Miniredis) {
m.srv.Register("DISCARD", m.cmdDiscard)
m.srv.Register("EXEC", m.cmdExec)
m.srv.Register("MULTI", m.cmdMulti)
m.srv.Register("UNWATCH", m.cmdUnwatch)
m.srv.Register("WATCH", m.cmdWatch)
}
// MULTI
func (m *Miniredis) cmdMulti(c *server.Peer, cmd string, args []string) {
if len(args) != 0 {
c.WriteError(errWrongNumber(cmd))
return
}
if !m.handleAuth(c) {
return
}
if m.checkPubsub(c, cmd) {
return
}
ctx := getCtx(c)
if ctx.nested {
c.WriteError(msgNotFromScripts(ctx.nestedSHA))
return
}
if inTx(ctx) {
c.WriteError("ERR MULTI calls can not be nested")
return
}
startTx(ctx)
c.WriteOK()
}
// EXEC
func (m *Miniredis) cmdExec(c *server.Peer, cmd string, args []string) {
if len(args) != 0 {
setDirty(c)
c.WriteError(errWrongNumber(cmd))
return
}
if !m.handleAuth(c) {
return
}
if m.checkPubsub(c, cmd) {
return
}
ctx := getCtx(c)
if ctx.nested {
c.WriteError(msgNotFromScripts(ctx.nestedSHA))
return
}
if !inTx(ctx) {
c.WriteError("ERR EXEC without MULTI")
return
}
if ctx.dirtyTransaction {
c.WriteError("EXECABORT Transaction discarded because of previous errors.")
// a failed EXEC finishes the tx
stopTx(ctx)
return
}
m.Lock()
defer m.Unlock()
// Check WATCHed keys.
for t, version := range ctx.watch {
if m.db(t.db).keyVersion[t.key] > version {
// Abort! Abort!
stopTx(ctx)
c.WriteLen(-1)
return
}
}
c.WriteLen(len(ctx.transaction))
for _, cb := range ctx.transaction {
cb(c, ctx)
}
// wake up anyone who waits on anything.
m.signal.Broadcast()
stopTx(ctx)
}
// DISCARD
func (m *Miniredis) cmdDiscard(c *server.Peer, cmd string, args []string) {
if len(args) != 0 {
setDirty(c)
c.WriteError(errWrongNumber(cmd))
return
}
if !m.handleAuth(c) {
return
}
if m.checkPubsub(c, cmd) {
return
}
ctx := getCtx(c)
if !inTx(ctx) {
c.WriteError("ERR DISCARD without MULTI")
return
}
stopTx(ctx)
c.WriteOK()
}
// WATCH
func (m *Miniredis) cmdWatch(c *server.Peer, cmd string, args []string) {
if len(args) == 0 {
setDirty(c)
c.WriteError(errWrongNumber(cmd))
return
}
if !m.handleAuth(c) {
return
}
if m.checkPubsub(c, cmd) {
return
}
ctx := getCtx(c)
if ctx.nested {
c.WriteError(msgNotFromScripts(ctx.nestedSHA))
return
}
if inTx(ctx) {
c.WriteError("ERR WATCH in MULTI")
return
}
m.Lock()
defer m.Unlock()
db := m.db(ctx.selectedDB)
for _, key := range args {
watch(db, ctx, key)
}
c.WriteOK()
}
// UNWATCH
func (m *Miniredis) cmdUnwatch(c *server.Peer, cmd string, args []string) {
if len(args) != 0 {
setDirty(c)
c.WriteError(errWrongNumber(cmd))
return
}
if !m.handleAuth(c) {
return
}
if m.checkPubsub(c, cmd) {
return
}
// Doesn't matter if UNWATCH is in a TX or not. Looks like a Redis bug to me.
unwatch(getCtx(c))
withTx(m, c, func(c *server.Peer, ctx *connCtx) {
// Do nothing if it's called in a transaction.
c.WriteOK()
})
}