From 95c17c0cb24ebfff814b0c10e21d1a12b02fb478 Mon Sep 17 00:00:00 2001 From: antirez Date: Thu, 13 Oct 2016 16:57:40 +0200 Subject: [PATCH] Modules: AbortBlock() API implemented. --- src/module.c | 10 +++++++++- src/modules/INTRO.md | 1 + src/redismodule.h | 2 ++ 3 files changed, 12 insertions(+), 1 deletion(-) diff --git a/src/module.c b/src/module.c index 91cd5309..66c09a42 100644 --- a/src/module.c +++ b/src/module.c @@ -3140,6 +3140,13 @@ int RM_UnblockClient(RedisModuleBlockedClient *bc, void *privdata) { return REDISMODULE_OK; } +/* Abort a blocked client blocking operation: the client will be unblocked + * without firing the reply callback. */ +int RM_AbortBlock(RedisModuleBlockedClient *bc) { + bc->reply_callback = NULL; + return RM_UnblockClient(bc,NULL); +} + /* This function will check the moduleUnblockedClients queue in order to * call the reply callback and really unblock the client. * @@ -3163,7 +3170,7 @@ void moduleHandleBlockedClients(void) { /* Release the lock during the loop, as long as we don't * touch the shared list. */ - if (c != NULL) { + if (c != NULL && bc->reply_callback != NULL) { RedisModuleCtx ctx = REDISMODULE_CTX_INIT; ctx.flags |= REDISMODULE_CTX_BLOCKED_REPLY; ctx.blocked_privdata = bc->privdata; @@ -3545,5 +3552,6 @@ void moduleRegisterCoreAPI(void) { REGISTER_API(IsBlockedReplyRequest); REGISTER_API(IsBlockedTimeoutRequest); REGISTER_API(GetBlockedClientPrivateData); + REGISTER_API(AbortBlock); REGISTER_API(Milliseconds); } diff --git a/src/modules/INTRO.md b/src/modules/INTRO.md index e5576b7f..3ac6a467 100644 --- a/src/modules/INTRO.md +++ b/src/modules/INTRO.md @@ -6,6 +6,7 @@ The modules documentation is composed of the following files: * `INTRO.md` (this file). An overview about Redis Modules system and API. It's a good idea to start your reading here. * `API.md` is generated from module.c top comments of RedisMoule functions. It is a good reference in order to understand how each function works. * `TYPES.md` covers the implementation of native data types into modules. +* `BLOCK.md` shows how to write blocking commands that will not reply immediately, but will block the client, without blocking the Redis server, and will provide a reply whenever will be possible. Redis modules make possible to extend Redis functionality using external modules, implementing new Redis commands at a speed and with features diff --git a/src/redismodule.h b/src/redismodule.h index 104de958..a07c0948 100644 --- a/src/redismodule.h +++ b/src/redismodule.h @@ -200,6 +200,7 @@ int REDISMODULE_API_FUNC(RedisModule_UnblockClient)(RedisModuleBlockedClient *bc int REDISMODULE_API_FUNC(RedisModule_IsBlockedReplyRequest)(RedisModuleCtx *ctx); int REDISMODULE_API_FUNC(RedisModule_IsBlockedTimeoutRequest)(RedisModuleCtx *ctx); void *REDISMODULE_API_FUNC(RedisModule_GetBlockedClientPrivateData)(RedisModuleCtx *ctx); +int REDISMODULE_API_FUNC(RedisModule_AbortBlock)(RedisModuleBlockedClient *bc); long long REDISMODULE_API_FUNC(RedisModule_Milliseconds)(void); /* This is included inline inside each Redis module. */ @@ -307,6 +308,7 @@ static int RedisModule_Init(RedisModuleCtx *ctx, const char *name, int ver, int REDISMODULE_GET_API(IsBlockedReplyRequest); REDISMODULE_GET_API(IsBlockedTimeoutRequest); REDISMODULE_GET_API(GetBlockedClientPrivateData); + REDISMODULE_GET_API(AbortBlock); REDISMODULE_GET_API(Milliseconds); RedisModule_SetModuleAttribs(ctx,name,ver,apiver);