CommandFilter API: More cleanup.

This commit is contained in:
Yossi Gottlieb 2019-03-18 23:05:52 +02:00
parent 9095e4dc9b
commit 2a5aeef79f
2 changed files with 10 additions and 29 deletions

View File

@ -270,32 +270,23 @@ typedef struct RedisModuleDictIter {
raxIterator ri; raxIterator ri;
} RedisModuleDictIter; } RedisModuleDictIter;
/* Information about the command to be executed, as passed to and from a typedef struct RedisModuleCommandFilterCtx {
* filter. */
typedef struct RedisModuleFilteredCommand {
RedisModuleString **argv; RedisModuleString **argv;
int argc; int argc;
} RedisModuleFilteredCommand; } RedisModuleCommandFilterCtx;
typedef void (*RedisModuleCommandFilterFunc) (RedisModuleCtx *ctx, RedisModuleFilteredCommand *cmd); typedef void (*RedisModuleCommandFilterFunc) (RedisModuleCommandFilterCtx *filter);
typedef struct RedisModuleCommandFilter { typedef struct RedisModuleCommandFilter {
/* The module that registered the filter */ /* The module that registered the filter */
RedisModule *module; RedisModule *module;
/* Filter callback function */ /* Filter callback function */
RedisModuleCommandFilterFunc callback; RedisModuleCommandFilterFunc callback;
/* Indicates a filter is active, avoid reentrancy */
int active;
} RedisModuleCommandFilter; } RedisModuleCommandFilter;
/* Registered filters */ /* Registered filters */
static list *moduleCommandFilters; static list *moduleCommandFilters;
typedef struct RedisModuleCommandFilterCtx {
RedisModuleString **argv;
int argc;
} RedisModuleCommandFilterCtx;
/* -------------------------------------------------------------------------- /* --------------------------------------------------------------------------
* Prototypes * Prototypes
* -------------------------------------------------------------------------- */ * -------------------------------------------------------------------------- */
@ -4802,16 +4793,13 @@ int moduleUnregisterUsedAPI(RedisModule *module) {
/* Register a new command filter function. Filters get executed by Redis /* Register a new command filter function. Filters get executed by Redis
* before processing an inbound command and can be used to manipulate the * before processing an inbound command and can be used to manipulate the
* behavior of standard Redis commands. Filters must not attempt to * behavior of standard Redis commands.
* perform Redis commands or operate on the dataset, and must restrict
* themselves to manipulation of the arguments.
*/ */
int RM_RegisterCommandFilter(RedisModuleCtx *ctx, RedisModuleCommandFilterFunc callback) { int RM_RegisterCommandFilter(RedisModuleCtx *ctx, RedisModuleCommandFilterFunc callback) {
RedisModuleCommandFilter *filter = zmalloc(sizeof(*filter)); RedisModuleCommandFilter *filter = zmalloc(sizeof(*filter));
filter->module = ctx->module; filter->module = ctx->module;
filter->callback = callback; filter->callback = callback;
filter->active = 0;
listAddNodeTail(moduleCommandFilters, filter); listAddNodeTail(moduleCommandFilters, filter);
return REDISMODULE_OK; return REDISMODULE_OK;
@ -4824,26 +4812,19 @@ void moduleCallCommandFilters(client *c) {
listNode *ln; listNode *ln;
listRewind(moduleCommandFilters,&li); listRewind(moduleCommandFilters,&li);
RedisModuleFilteredCommand cmd = { RedisModuleCommandFilterCtx filter = {
.argv = c->argv, .argv = c->argv,
.argc = c->argc .argc = c->argc
}; };
while((ln = listNext(&li))) { while((ln = listNext(&li))) {
RedisModuleCommandFilter *filter = ln->value; RedisModuleCommandFilter *f = ln->value;
if (filter->active) continue;
RedisModuleCtx ctx = REDISMODULE_CTX_INIT; f->callback(&filter);
ctx.module = filter->module;
filter->active = 1;
filter->callback(&ctx, &cmd);
filter->active = 0;
moduleFreeContext(&ctx);
} }
c->argv = cmd.argv; c->argv = filter.argv;
c->argc = cmd.argc; c->argc = filter.argc;
} }
/* Return the number of arguments a filtered command has. The number of /* Return the number of arguments a filtered command has. The number of

View File

@ -163,7 +163,7 @@ typedef void (*RedisModuleTypeDigestFunc)(RedisModuleDigest *digest, void *value
typedef void (*RedisModuleTypeFreeFunc)(void *value); typedef void (*RedisModuleTypeFreeFunc)(void *value);
typedef void (*RedisModuleClusterMessageReceiver)(RedisModuleCtx *ctx, const char *sender_id, uint8_t type, const unsigned char *payload, uint32_t len); typedef void (*RedisModuleClusterMessageReceiver)(RedisModuleCtx *ctx, const char *sender_id, uint8_t type, const unsigned char *payload, uint32_t len);
typedef void (*RedisModuleTimerProc)(RedisModuleCtx *ctx, void *data); typedef void (*RedisModuleTimerProc)(RedisModuleCtx *ctx, void *data);
typedef void (*RedisModuleCommandFilterFunc) (RedisModuleCtx *ctx, RedisModuleCommandFilterCtx *filter); typedef void (*RedisModuleCommandFilterFunc) (RedisModuleCommandFilterCtx *filter);
#define REDISMODULE_TYPE_METHOD_VERSION 1 #define REDISMODULE_TYPE_METHOD_VERSION 1
typedef struct RedisModuleTypeMethods { typedef struct RedisModuleTypeMethods {