Create a new timer that will fire after `period` milliseconds, and will call * the specified function using `data` as argument. The returned timer ID can be * used to get information from the timer or to stop it before it fires. * Note that for the common use case of a repeating timer (Re-registration * of the timer inside the RedisModuleTimerProc callback) it matters when * this API is calle
| 6331 | * (If the time it takes to execute 'callback' is negligible the two |
| 6332 | * statements above mean the same) */ |
| 6333 | RedisModuleTimerID RM_CreateTimer(RedisModuleCtx *ctx, mstime_t period, RedisModuleTimerProc callback, void *data) { |
| 6334 | RedisModuleTimer *timer = zmalloc(sizeof(*timer)); |
| 6335 | timer->module = ctx->module; |
| 6336 | timer->callback = callback; |
| 6337 | timer->data = data; |
| 6338 | timer->dbid = ctx->client ? ctx->client->db->id : 0; |
| 6339 | uint64_t expiretime = ustime()+period*1000; |
| 6340 | uint64_t key; |
| 6341 | |
| 6342 | while(1) { |
| 6343 | key = htonu64(expiretime); |
| 6344 | if (raxFind(Timers, (unsigned char*)&key,sizeof(key)) == raxNotFound) { |
| 6345 | raxInsert(Timers,(unsigned char*)&key,sizeof(key),timer,NULL); |
| 6346 | break; |
| 6347 | } else { |
| 6348 | expiretime++; |
| 6349 | } |
| 6350 | } |
| 6351 | |
| 6352 | /* We need to install the main event loop timer if it's not already |
| 6353 | * installed, or we may need to refresh its period if we just installed |
| 6354 | * a timer that will expire sooner than any other else (i.e. the timer |
| 6355 | * we just installed is the first timer in the Timers rax). */ |
| 6356 | if (aeTimer != -1) { |
| 6357 | raxIterator ri; |
| 6358 | raxStart(&ri,Timers); |
| 6359 | raxSeek(&ri,"^",NULL,0); |
| 6360 | raxNext(&ri); |
| 6361 | if (memcmp(ri.key,&key,sizeof(key)) == 0) { |
| 6362 | /* This is the first key, we need to re-install the timer according |
| 6363 | * to the just added event. */ |
| 6364 | aeDeleteTimeEvent(server.el,aeTimer); |
| 6365 | aeTimer = -1; |
| 6366 | } |
| 6367 | raxStop(&ri); |
| 6368 | } |
| 6369 | |
| 6370 | /* If we have no main timer (the old one was invalidated, or this is the |
| 6371 | * first module timer we have), install one. */ |
| 6372 | if (aeTimer == -1) |
| 6373 | aeTimer = aeCreateTimeEvent(server.el,period,moduleTimerHandler,NULL,NULL); |
| 6374 | |
| 6375 | return key; |
| 6376 | } |
| 6377 | |
| 6378 | /* Stop a timer, returns REDISMODULE_OK if the timer was found, belonged to the |
| 6379 | * calling module, and was stopped, otherwise REDISMODULE_ERR is returned. |
nothing calls this directly
no test coverage detected