HELLO.BLOCK -- Block for seconds, then reply with * a random number. Timeout is the command timeout, so that you can test * what happens when the delay is greater than the timeout. */
| 95 | * a random number. Timeout is the command timeout, so that you can test |
| 96 | * what happens when the delay is greater than the timeout. */ |
| 97 | int HelloBlock_RedisCommand(RedisModuleCtx *ctx, RedisModuleString **argv, int argc) { |
| 98 | if (argc != 3) return RedisModule_WrongArity(ctx); |
| 99 | long long delay; |
| 100 | long long timeout; |
| 101 | |
| 102 | if (RedisModule_StringToLongLong(argv[1],&delay) != REDISMODULE_OK) { |
| 103 | return RedisModule_ReplyWithError(ctx,"ERR invalid count"); |
| 104 | } |
| 105 | |
| 106 | if (RedisModule_StringToLongLong(argv[2],&timeout) != REDISMODULE_OK) { |
| 107 | return RedisModule_ReplyWithError(ctx,"ERR invalid count"); |
| 108 | } |
| 109 | |
| 110 | pthread_t tid; |
| 111 | RedisModuleBlockedClient *bc = RedisModule_BlockClient(ctx,HelloBlock_Reply,HelloBlock_Timeout,HelloBlock_FreeData,timeout); |
| 112 | |
| 113 | /* Here we set a disconnection handler, however since this module will |
| 114 | * block in sleep() in a thread, there is not much we can do in the |
| 115 | * callback, so this is just to show you the API. */ |
| 116 | RedisModule_SetDisconnectCallback(bc,HelloBlock_Disconnected); |
| 117 | |
| 118 | /* Now that we setup a blocking client, we need to pass the control |
| 119 | * to the thread. However we need to pass arguments to the thread: |
| 120 | * the delay and a reference to the blocked client handle. */ |
| 121 | void **targ = RedisModule_Alloc(sizeof(void*)*2); |
| 122 | targ[0] = bc; |
| 123 | targ[1] = (void*)(unsigned long) delay; |
| 124 | |
| 125 | if (pthread_create(&tid,NULL,HelloBlock_ThreadMain,targ) != 0) { |
| 126 | RedisModule_AbortBlock(bc); |
| 127 | return RedisModule_ReplyWithError(ctx,"-ERR Can't start thread"); |
| 128 | } |
| 129 | return REDISMODULE_OK; |
| 130 | } |
| 131 | |
| 132 | /* The thread entry point that actually executes the blocking part |
| 133 | * of the command HELLO.KEYS. |
nothing calls this directly
no test coverage detected