diff options
author | Mikhail Galanin <mgalanin@mimecast.com> | 2018-08-17 11:16:45 +0100 |
---|---|---|
committer | Mikhail Galanin <mgalanin@mimecast.com> | 2018-08-17 11:16:45 +0100 |
commit | 15c7adc671c7d8e22febab8e64e946f71e93738c (patch) | |
tree | 13f6905213d8c1e507d2276a40c76e5ff3158c70 /src/lua/lua_redis.c | |
parent | 55afdd2905dd1d9f58982691a404c373e768d304 (diff) | |
download | rspamd-15c7adc671c7d8e22febab8e64e946f71e93738c.tar.gz rspamd-15c7adc671c7d8e22febab8e64e946f71e93738c.zip |
[Minor] use callback helpers to avoid conflicts between coroutine- and callback-based code
Diffstat (limited to 'src/lua/lua_redis.c')
-rw-r--r-- | src/lua/lua_redis.c | 41 |
1 files changed, 26 insertions, 15 deletions
diff --git a/src/lua/lua_redis.c b/src/lua/lua_redis.c index e5b97ebeb..0fc9c43b7 100644 --- a/src/lua/lua_redis.c +++ b/src/lua/lua_redis.c @@ -14,6 +14,7 @@ * limitations under the License. */ #include "lua_common.h" +#include "lua_thread_pool.h" #include "utlist.h" #include "contrib/hiredis/hiredis.h" @@ -92,7 +93,7 @@ struct lua_redis_specific_userdata; */ struct lua_redis_userdata { redisAsyncContext *ctx; - lua_State *L; + struct rspamd_task *task; struct rspamd_async_session *s; struct event_base *ev_base; struct rspamd_config *cfg; @@ -191,7 +192,7 @@ lua_redis_dtor (struct lua_redis_ctx *ctx) lua_redis_free_args (cur->args, cur->arglens, cur->nargs); if (cur->cbref != -1) { - luaL_unref (ud->L, LUA_REGISTRYINDEX, cur->cbref); + luaL_unref (ud->cfg->lua_state, LUA_REGISTRYINDEX, cur->cbref); } g_free (cur); @@ -244,21 +245,27 @@ lua_redis_push_error (const gchar *err, gboolean connected) { struct lua_redis_userdata *ud = sp_ud->c; + struct lua_callback_state cbs; if (!(sp_ud->flags & (LUA_REDIS_SPECIFIC_REPLIED|LUA_REDIS_SPECIFIC_FINISHED))) { if (sp_ud->cbref != -1) { + + lua_thread_pool_prepare_callback (ud->cfg->lua_thread_pool, &cbs); + /* Push error */ - lua_rawgeti (ud->L, LUA_REGISTRYINDEX, sp_ud->cbref); + lua_rawgeti (cbs.L, LUA_REGISTRYINDEX, sp_ud->cbref); /* String of error */ - lua_pushstring (ud->L, err); + lua_pushstring (cbs.L, err); /* Data is nil */ - lua_pushnil (ud->L); + lua_pushnil (cbs.L); - if (lua_pcall (ud->L, 2, 0, 0) != 0) { - msg_info ("call to callback failed: %s", lua_tostring (ud->L, -1)); - lua_pop (ud->L, 1); + if (lua_pcall (cbs.L, 2, 0, 0) != 0) { + msg_info ("call to callback failed: %s", lua_tostring (cbs.L, -1)); + lua_pop (cbs.L, 1); } + + lua_thread_pool_restore_callback (&cbs); } sp_ud->flags |= LUA_REDIS_SPECIFIC_REPLIED; @@ -323,21 +330,25 @@ lua_redis_push_data (const redisReply *r, struct lua_redis_ctx *ctx, struct lua_redis_specific_userdata *sp_ud) { struct lua_redis_userdata *ud = sp_ud->c; + struct lua_callback_state cbs; if (!(sp_ud->flags & (LUA_REDIS_SPECIFIC_REPLIED|LUA_REDIS_SPECIFIC_FINISHED))) { if (sp_ud->cbref != -1) { + lua_thread_pool_prepare_callback (ud->cfg->lua_thread_pool, &cbs); + /* Push error */ - lua_rawgeti (ud->L, LUA_REGISTRYINDEX, sp_ud->cbref); + lua_rawgeti (cbs.L, LUA_REGISTRYINDEX, sp_ud->cbref); /* Error is nil */ - lua_pushnil (ud->L); + lua_pushnil (cbs.L); /* Data */ - lua_redis_push_reply (ud->L, r, ctx->flags & LUA_REDIS_TEXTDATA); + lua_redis_push_reply (cbs.L, r, ctx->flags & LUA_REDIS_TEXTDATA); - if (lua_pcall (ud->L, 2, 0, 0) != 0) { - msg_info ("call to callback failed: %s", lua_tostring (ud->L, -1)); - lua_pop (ud->L, 1); + if (lua_pcall (cbs.L, 2, 0, 0) != 0) { + msg_info ("call to callback failed: %s", lua_tostring (cbs.L, -1)); + lua_pop (cbs.L, 1); } + lua_thread_pool_restore_callback (&cbs); } sp_ud->flags |= LUA_REDIS_SPECIFIC_REPLIED; @@ -689,7 +700,7 @@ rspamd_lua_redis_prepare_connection (lua_State *L, gint *pcbref) ud->cfg = cfg; ud->pool = cfg->redis_pool; ud->ev_base = ev_base; - ud->L = L; + ud->task = task; ret = TRUE; } |