mirror of
https://codeberg.org/redict/redict.git
synced 2025-01-23 00:28:26 -05:00
CommandFilter API: More cleanup.
This commit is contained in:
parent
9095e4dc9b
commit
2a5aeef79f
37
src/module.c
37
src/module.c
@ -270,32 +270,23 @@ typedef struct RedisModuleDictIter {
|
||||
raxIterator ri;
|
||||
} RedisModuleDictIter;
|
||||
|
||||
/* Information about the command to be executed, as passed to and from a
|
||||
* filter. */
|
||||
typedef struct RedisModuleFilteredCommand {
|
||||
typedef struct RedisModuleCommandFilterCtx {
|
||||
RedisModuleString **argv;
|
||||
int argc;
|
||||
} RedisModuleFilteredCommand;
|
||||
} RedisModuleCommandFilterCtx;
|
||||
|
||||
typedef void (*RedisModuleCommandFilterFunc) (RedisModuleCtx *ctx, RedisModuleFilteredCommand *cmd);
|
||||
typedef void (*RedisModuleCommandFilterFunc) (RedisModuleCommandFilterCtx *filter);
|
||||
|
||||
typedef struct RedisModuleCommandFilter {
|
||||
/* The module that registered the filter */
|
||||
RedisModule *module;
|
||||
/* Filter callback function */
|
||||
RedisModuleCommandFilterFunc callback;
|
||||
/* Indicates a filter is active, avoid reentrancy */
|
||||
int active;
|
||||
} RedisModuleCommandFilter;
|
||||
|
||||
/* Registered filters */
|
||||
static list *moduleCommandFilters;
|
||||
|
||||
typedef struct RedisModuleCommandFilterCtx {
|
||||
RedisModuleString **argv;
|
||||
int argc;
|
||||
} RedisModuleCommandFilterCtx;
|
||||
|
||||
/* --------------------------------------------------------------------------
|
||||
* Prototypes
|
||||
* -------------------------------------------------------------------------- */
|
||||
@ -4802,16 +4793,13 @@ int moduleUnregisterUsedAPI(RedisModule *module) {
|
||||
|
||||
/* Register a new command filter function. Filters get executed by Redis
|
||||
* before processing an inbound command and can be used to manipulate the
|
||||
* behavior of standard Redis commands. Filters must not attempt to
|
||||
* perform Redis commands or operate on the dataset, and must restrict
|
||||
* themselves to manipulation of the arguments.
|
||||
* behavior of standard Redis commands.
|
||||
*/
|
||||
|
||||
int RM_RegisterCommandFilter(RedisModuleCtx *ctx, RedisModuleCommandFilterFunc callback) {
|
||||
RedisModuleCommandFilter *filter = zmalloc(sizeof(*filter));
|
||||
filter->module = ctx->module;
|
||||
filter->callback = callback;
|
||||
filter->active = 0;
|
||||
|
||||
listAddNodeTail(moduleCommandFilters, filter);
|
||||
return REDISMODULE_OK;
|
||||
@ -4824,26 +4812,19 @@ void moduleCallCommandFilters(client *c) {
|
||||
listNode *ln;
|
||||
listRewind(moduleCommandFilters,&li);
|
||||
|
||||
RedisModuleFilteredCommand cmd = {
|
||||
RedisModuleCommandFilterCtx filter = {
|
||||
.argv = c->argv,
|
||||
.argc = c->argc
|
||||
};
|
||||
|
||||
while((ln = listNext(&li))) {
|
||||
RedisModuleCommandFilter *filter = ln->value;
|
||||
if (filter->active) continue;
|
||||
RedisModuleCommandFilter *f = ln->value;
|
||||
|
||||
RedisModuleCtx ctx = REDISMODULE_CTX_INIT;
|
||||
ctx.module = filter->module;
|
||||
|
||||
filter->active = 1;
|
||||
filter->callback(&ctx, &cmd);
|
||||
filter->active = 0;
|
||||
moduleFreeContext(&ctx);
|
||||
f->callback(&filter);
|
||||
}
|
||||
|
||||
c->argv = cmd.argv;
|
||||
c->argc = cmd.argc;
|
||||
c->argv = filter.argv;
|
||||
c->argc = filter.argc;
|
||||
}
|
||||
|
||||
/* Return the number of arguments a filtered command has. The number of
|
||||
|
@ -163,7 +163,7 @@ typedef void (*RedisModuleTypeDigestFunc)(RedisModuleDigest *digest, 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 (*RedisModuleTimerProc)(RedisModuleCtx *ctx, void *data);
|
||||
typedef void (*RedisModuleCommandFilterFunc) (RedisModuleCtx *ctx, RedisModuleCommandFilterCtx *filter);
|
||||
typedef void (*RedisModuleCommandFilterFunc) (RedisModuleCommandFilterCtx *filter);
|
||||
|
||||
#define REDISMODULE_TYPE_METHOD_VERSION 1
|
||||
typedef struct RedisModuleTypeMethods {
|
||||
|
Loading…
Reference in New Issue
Block a user