From 6bb8cdaebe74f9c79bb754ccd7a7f05fe8385f81 Mon Sep 17 00:00:00 2001 From: antirez Date: Thu, 20 Dec 2018 17:16:39 +0100 Subject: [PATCH] Modules shared API: unregister APIs function. --- src/module.c | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/src/module.c b/src/module.c index 2914b5903..eafdd81f6 100644 --- a/src/module.c +++ b/src/module.c @@ -4700,6 +4700,29 @@ void *RM_GetSharedAPI(RedisModuleCtx *ctx, const char *apiname) { return sapi->func; } +/* Remove all the APIs registered by the specified module. Usually you + * want this when the module is going to be unloaded. This function + * assumes that's caller responsibility to make sure the APIs are not + * used by other modules. + * + * The number of unregistered APIs is returned. */ +int moduleUnregisterSharedAPI(RedisModule *module) { + int count = 0; + dictIterator *di = dictGetSafeIterator(server.sharedapi); + dictEntry *de; + while ((de = dictNext(di)) != NULL) { + const char *apiname = dictGetKey(de); + RedisModuleSharedAPI *sapi = dictGetVal(de); + if (sapi->module == module) { + dictDelete(server.sharedapi,apiname); + zfree(sapi); + count++; + } + } + dictReleaseIterator(di); + return count; +} + /* -------------------------------------------------------------------------- * Modules API internals * -------------------------------------------------------------------------- */ @@ -4843,6 +4866,7 @@ int moduleLoad(const char *path, void **module_argv, int module_argc) { if (onload((void*)&ctx,module_argv,module_argc) == REDISMODULE_ERR) { if (ctx.module) { moduleUnregisterCommands(ctx.module); + moduleUnregisterSharedAPI(ctx.module); moduleFreeModuleStructure(ctx.module); } dlclose(handle); @@ -4880,6 +4904,7 @@ int moduleUnload(sds name) { } moduleUnregisterCommands(module); + moduleUnregisterSharedAPI(module); /* Remove any notification subscribers this module might have */ moduleUnsubscribeNotifications(module);