Register a callback receiver for cluster messages of type 'type'. If there * was already a registered callback, this will replace the callback function * with the one provided, otherwise if the callback is set to NULL and there * is already a callback for this function, the callback is unregistered * (so this API call is also used in order to delete the receiver). */
| 6206 | * is already a callback for this function, the callback is unregistered |
| 6207 | * (so this API call is also used in order to delete the receiver). */ |
| 6208 | void RM_RegisterClusterMessageReceiver(RedisModuleCtx *ctx, uint8_t type, RedisModuleClusterMessageReceiver callback) { |
| 6209 | if (!g_pserver->cluster_enabled) return; |
| 6210 | |
| 6211 | uint64_t module_id = moduleTypeEncodeId(ctx->module->name,0); |
| 6212 | moduleClusterReceiver *r = clusterReceivers[type], *prev = NULL; |
| 6213 | while(r) { |
| 6214 | if (r->module_id == module_id) { |
| 6215 | /* Found! Set or delete. */ |
| 6216 | if (callback) { |
| 6217 | r->callback = callback; |
| 6218 | } else { |
| 6219 | /* Delete the receiver entry if the user is setting |
| 6220 | * it to NULL. Just unlink the receiver node from the |
| 6221 | * linked list. */ |
| 6222 | if (prev) |
| 6223 | prev->next = r->next; |
| 6224 | else |
| 6225 | clusterReceivers[type]->next = r->next; |
| 6226 | zfree(r); |
| 6227 | } |
| 6228 | return; |
| 6229 | } |
| 6230 | prev = r; |
| 6231 | r = r->next; |
| 6232 | } |
| 6233 | |
| 6234 | /* Not found, let's add it. */ |
| 6235 | if (callback) { |
| 6236 | r = (moduleClusterReceiver*)zmalloc(sizeof(*r), MALLOC_LOCAL); |
| 6237 | r->module_id = module_id; |
| 6238 | r->module = ctx->module; |
| 6239 | r->callback = callback; |
| 6240 | r->next = clusterReceivers[type]; |
| 6241 | clusterReceivers[type] = r; |
| 6242 | } |
| 6243 | } |
| 6244 | |
| 6245 | /* Send a message to all the nodes in the cluster if `target` is NULL, otherwise |
| 6246 | * at the specified target, which is a REDISMODULE_NODE_ID_LEN bytes node ID, as |
nothing calls this directly
no test coverage detected