diff options
author | antirez <antirez@gmail.com> | 2019-10-23 11:05:33 +0200 |
---|---|---|
committer | antirez <antirez@gmail.com> | 2019-10-23 11:05:33 +0200 |
commit | dd384ea7d59bef34b786adbf93ad918edf8c2897 (patch) | |
tree | 83c84247d32397ef751aa61b012d1d688c312f77 | |
parent | 856618570f65f6a4003a7791c89474cd884e7547 (diff) | |
download | redis-module-hooks.tar.gz |
Modules hooks: for nested calls, create new fake clients.module-hooks
We can't use the same client at the same time when re-entering the hook.
-rw-r--r-- | src/module.c | 22 |
1 files changed, 18 insertions, 4 deletions
diff --git a/src/module.c b/src/module.c index 9c51f70a8..7428801c0 100644 --- a/src/module.c +++ b/src/module.c @@ -61,7 +61,7 @@ struct RedisModule { list *using; /* List of modules we use some APIs of. */ list *filters; /* List of filters the module has registered. */ int in_call; /* RM_Call() nesting level */ - int in_hook; /* Non zero if an hook callback is active. */ + int in_hook; /* Hooks callback nesting level for this module (0 or 1). */ int options; /* Module options and capabilities. */ RedisModuleInfoFunc info_cb; /* Callback for module to add INFO fields. */ }; @@ -333,6 +333,8 @@ typedef struct RedisModuleEventListener { } RedisModuleEventListener; list *RedisModule_EventListeners; /* Global list of all the active events. */ +unsigned long long ModulesInHooks = 0; /* Total number of modules in hooks + callbacks right now. */ /* -------------------------------------------------------------------------- * Prototypes @@ -5864,7 +5866,14 @@ void moduleFireServerEvent(uint64_t eid, int subid, void *data) { if (el->event.id == eid && !el->module->in_hook) { RedisModuleCtx ctx = REDISMODULE_CTX_INIT; ctx.module = el->module; - ctx.client = moduleFreeContextReusedClient; + + if (ModulesInHooks == 0) { + ctx.client = moduleFreeContextReusedClient; + } else { + ctx.client = createClient(NULL); + ctx.client->flags |= CLIENT_MODULE; + ctx.client->user = NULL; /* Root user. */ + } void *moduledata = NULL; RedisModuleClientInfoV1 civ1; @@ -5878,9 +5887,14 @@ void moduleFireServerEvent(uint64_t eid, int subid, void *data) { if (fi->dbnum != -1) selectDb(ctx.client, fi->dbnum); } - el->module->in_hook = 1; + + ModulesInHooks++; + el->module->in_hook++; el->callback(&ctx,el->event,subid,moduledata); - el->module->in_hook = 0; + el->module->in_hook--; + ModulesInHooks--; + + if (ModulesInHooks != 0) freeClient(ctx.client); moduleFreeContext(&ctx); } } |