Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
ruanhaishen
redis
Commits
73841e8c
Commit
73841e8c
authored
Jan 15, 2019
by
zhaozhao.zz
Browse files
scripting: a little refactor about EVAL and redis.call()
Change server.lua_client's flag in a more explicit way.
parent
097c4a65
Changes
1
Hide whitespace changes
Inline
Side-by-side
src/scripting.c
View file @
73841e8c
...
...
@@ -485,13 +485,6 @@ int luaRedisGenericCommand(lua_State *lua, int raise_error) {
static
size_t
cached_objects_len
[
LUA_CMD_OBJCACHE_SIZE
];
static
int
inuse
=
0
;
/* Recursive calls detection. */
/* Reflect MULTI state */
if
(
server
.
lua_multi_emitted
||
(
server
.
lua_caller
->
flags
&
CLIENT_MULTI
))
{
c
->
flags
|=
CLIENT_MULTI
;
}
else
{
c
->
flags
&=
~
CLIENT_MULTI
;
}
/* By using Lua debug hooks it is possible to trigger a recursive call
* to luaRedisGenericCommand(), which normally should never happen.
* To make this function reentrant is futile and makes it slower, but
...
...
@@ -1432,6 +1425,22 @@ void luaMaskCountHook(lua_State *lua, lua_Debug *ar) {
}
}
void
prepareLuaClient
(
void
)
{
/* Select the right DB in the context of the Lua client */
selectDb
(
server
.
lua_client
,
server
.
lua_caller
->
db
->
id
);
server
.
lua_client
->
resp
=
2
;
/* Default is RESP2, scripts can change it. */
/* If we are in MULTI context, flag Lua client as CLIENT_MULTI. */
if
(
server
.
lua_caller
->
flags
&
CLIENT_MULTI
)
{
server
.
lua_client
->
flags
|=
CLIENT_MULTI
;
}
}
void
resetLuaClient
(
void
)
{
/* After the script done, remove the MULTI state. */
server
.
lua_client
->
flags
&=
~
CLIENT_MULTI
;
}
void
evalGenericCommand
(
client
*
c
,
int
evalsha
)
{
lua_State
*
lua
=
server
.
lua
;
char
funcname
[
43
];
...
...
@@ -1520,10 +1529,6 @@ void evalGenericCommand(client *c, int evalsha) {
luaSetGlobalArray
(
lua
,
"KEYS"
,
c
->
argv
+
3
,
numkeys
);
luaSetGlobalArray
(
lua
,
"ARGV"
,
c
->
argv
+
3
+
numkeys
,
c
->
argc
-
3
-
numkeys
);
/* Set the Lua client database and protocol. */
selectDb
(
server
.
lua_client
,
c
->
db
->
id
);
server
.
lua_client
->
resp
=
2
;
/* Default is RESP2, scripts can change it. */
/* Set a hook in order to be able to stop the script execution if it
* is running for too much time.
* We set the hook only if the time limit is enabled as the hook will
...
...
@@ -1543,11 +1548,15 @@ void evalGenericCommand(client *c, int evalsha) {
delhook
=
1
;
}
prepareLuaClient
();
/* At this point whether this script was never seen before or if it was
* already defined, we can call it. We have zero arguments and expect
* a single return value. */
err
=
lua_pcall
(
lua
,
0
,
1
,
-
2
);
resetLuaClient
();
/* Perform some cleanup that we need to do both on error and success. */
if
(
delhook
)
lua_sethook
(
lua
,
NULL
,
0
,
0
);
/* Disable hook */
if
(
server
.
lua_timedout
)
{
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment