Skip to content

Commit e79bddc

Browse files
committed
redis.setresp({2,3})
1 parent 236d77d commit e79bddc

File tree

4 files changed

+44
-7
lines changed

4 files changed

+44
-7
lines changed

cmd_scripting.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -101,16 +101,20 @@ func (m *Miniredis) runLuaScript(c *server.Peer, sha, script string, args []stri
101101
l.Push(lua.LString("redis"))
102102
l.Call(1, 0)
103103

104+
// lua can call redis.setresp(...), but it's tmp state.
105+
oldresp := c.Resp3
104106
if err := doScript(l, script); err != nil {
105107
c.WriteError(err.Error())
106108
return false
107109
}
108110

109111
luaToRedis(l, c, l.Get(1))
112+
c.Resp3 = oldresp
113+
c.SwitchResp3 = nil
110114
return true
111115
}
112116

113-
// doScript pre-compiiles the given script into a Lua prototype,
117+
// doScript pre-compiles the given script into a Lua prototype,
114118
// then executes the pre-compiled function against the given lua state.
115119
//
116120
// This is thread-safe.
@@ -259,7 +263,6 @@ func (m *Miniredis) cmdScript(c *server.Peer, cmd string, args []string) {
259263
c.WriteError(msgScriptFlush)
260264
return
261265
}
262-
263266
default:
264267
setDirty(c)
265268
c.WriteError(fmt.Sprintf(msgFScriptUsageSimple, strings.ToUpper(opts.subcmd)))
@@ -276,7 +279,6 @@ func (m *Miniredis) cmdScript(c *server.Peer, cmd string, args []string) {
276279
sha := sha1Hex(opts.script)
277280
m.scripts[sha] = opts.script
278281
c.WriteBulk(sha)
279-
280282
case "exists":
281283
c.WriteLen(len(args))
282284
for _, arg := range args {
@@ -286,11 +288,9 @@ func (m *Miniredis) cmdScript(c *server.Peer, cmd string, args []string) {
286288
c.WriteInt(0)
287289
}
288290
}
289-
290291
case "flush":
291292
m.scripts = map[string]string{}
292293
c.WriteOK()
293-
294294
}
295295
})
296296
}

integration/script_test.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,22 @@ func TestScript(t *testing.T) {
115115
c.Error("not allowed with BLOCK option", "EVAL", `redis.call("XREADGROUP", "GROUP", "group", "consumer", "BLOCK", 1000, "STREAMS", "pl", ">")`, "0")
116116
})
117117
})
118+
119+
t.Run("setresp", func(t *testing.T) {
120+
testRaw(t, func(c *client) {
121+
c.Do("EVAL", `redis.setresp(3); redis.call("SET", "foo", 12); return redis.call("GET", "foo")`, "0")
122+
c.Do("SCRIPT", "LOAD", `redis.setresp(3)`)
123+
c.Do("EVALSHA", "d204691e560b5b17f19626b50f84c2dcadff7ed5", "0")
124+
c.Do("EVAL", `return redis.setresp(3)`, "0")
125+
c.Do("EVAL", `return redis.setresp(2)`, "0")
126+
c.Error("RESP version must be 2 or 3", "EVAL", `return redis.setresp(4)`, "0")
127+
})
128+
testRESP3(t, func(c *client) {
129+
c.Do("SCRIPT", "LOAD", `redis.setresp(3)`)
130+
c.Do("EVALSHA", "d204691e560b5b17f19626b50f84c2dcadff7ed5", "0")
131+
c.Do("EVAL", `redis.setresp(3); redis.call("SET", "foo", 12); return redis.call("GET", "foo")`, "0")
132+
})
133+
})
118134
}
119135

120136
func TestLua(t *testing.T) {

lua.go

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,8 @@ func mkLua(srv *server.Server, c *server.Peer, sha string) (map[string]lua.LGFun
158158
return 1
159159
},
160160
"replicate_commands": func(l *lua.LState) int {
161-
// ignored
161+
// always succeeds since 7.0.0
162+
l.Push(lua.LTrue)
162163
return 1
163164
},
164165
"set_repl": func(l *lua.LState) int {
@@ -170,6 +171,21 @@ func mkLua(srv *server.Server, c *server.Peer, sha string) (map[string]lua.LGFun
170171
// ignored
171172
return 1
172173
},
174+
"setresp": func(l *lua.LState) int {
175+
level := l.CheckInt(1)
176+
toresp3 := false
177+
switch level {
178+
case 2:
179+
toresp3 = false
180+
case 3:
181+
toresp3 = true
182+
default:
183+
l.Error(lua.LString("RESP version must be 2 or 3"), 1)
184+
return 0
185+
}
186+
c.SwitchResp3 = &toresp3
187+
return 0
188+
},
173189
}, luaRedisConstants
174190
}
175191

@@ -226,7 +242,7 @@ func luaToRedis(l *lua.LState, c *server.Peer, value lua.LValue) {
226242
luaToRedis(l, c, r)
227243
}
228244
default:
229-
panic("....")
245+
panic(fmt.Sprintf("wat: %T", t))
230246
}
231247
}
232248

server/server.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,10 @@ func (s *Server) Dispatch(c *Peer, args []string) {
216216
s.infoCmds++
217217
s.mu.Unlock()
218218
cb(c, cmdUp, args)
219+
if c.SwitchResp3 != nil {
220+
c.Resp3 = *c.SwitchResp3
221+
c.SwitchResp3 = nil
222+
}
219223
}
220224

221225
// TotalCommands is total (known) commands since this the server started
@@ -245,6 +249,7 @@ type Peer struct {
245249
w *bufio.Writer
246250
closed bool
247251
Resp3 bool
252+
SwitchResp3 *bool // we'll switch to this version _after_ the command
248253
Ctx interface{} // anything goes, server won't touch this
249254
onDisconnect []func() // list of callbacks
250255
mu sync.Mutex // for Block()

0 commit comments

Comments
 (0)