Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
ruanhaishen
redis
Commits
bebb2e19
Commit
bebb2e19
authored
Oct 21, 2019
by
antirez
Browse files
Modules hooks: a first version of events and some API.
parent
3170f633
Changes
2
Hide whitespace changes
Inline
Side-by-side
src/module.c
View file @
bebb2e19
...
@@ -345,13 +345,6 @@ static struct RedisModuleForkInfo {
...
@@ -345,13 +345,6 @@ static struct RedisModuleForkInfo {
#define REDISMODULE_EVENT_ID_MASTER_LINK_UP 14
#define REDISMODULE_EVENT_ID_MASTER_LINK_UP 14
#define REDISMODULE_EVENT_ID_MASTER_LINK_DOWN 15
#define REDISMODULE_EVENT_ID_MASTER_LINK_DOWN 15
typedef struct RedisModuleEvent {
uint64_t id; /* REDISMODULE_EVENT_ID_... defines. */
uint64_t dataver; /* Version of the structure we pass as 'data'. */
} RedisModuleEvent;
typedef int (*RedisModuleEventCallback)(RedisModuleEvent eid, void *data);
typedef struct RedisModuleEventListener {
typedef struct RedisModuleEventListener {
RedisModule *module;
RedisModule *module;
RedisModuleEvent event;
RedisModuleEvent event;
...
@@ -5587,6 +5580,57 @@ void ModuleForkDoneHandler(int exitcode, int bysignal) {
...
@@ -5587,6 +5580,57 @@ void ModuleForkDoneHandler(int exitcode, int bysignal) {
moduleForkInfo.done_handler_user_data = NULL;
moduleForkInfo.done_handler_user_data = NULL;
}
}
/* --------------------------------------------------------------------------
* Server hooks implementation
* -------------------------------------------------------------------------- */
/* Register to be notified, via a callback, when the specified server event
* happens. The callback is called with the event as argument, and an additional
* argument which is a void pointer and should be cased to a specific type
* that is event-specific (but many events will just use NULL since they do not
* have additional information to pass to the callback).
*
* If the callback is NULL and there was a previous subscription, the module
* will be unsubscribed. If there was a previous subscription and the callback
* is not null, the old callback will be replaced with the new one.
*
* The function returns REDISMODULE_OK if the module was successfully subscrived
* for the specified event. If the API is called from a wrong context then
* REDISMODULE_ERR is returned. */
int RedisModule_SubscribeToServerEvent(RedisModuleCtx *ctx, RedisModuleEvent event, RedisModuleEventCallback callback) {
RedisModuleEventListener *el;
/* Protect in case of calls from contexts without a module reference. */
if (ctx->module == NULL) return REDISMODULE_ERR;
/* Search an event matching this module and event ID. */
listIter li;
listNode *ln;
listRewind(RedisModule_EventListeners,&li);
while((ln = listNext(&li))) {
el = ln->value;
if (el->module == ctx->module && el->event.id == event.id)
break; /* Matching event found. */
}
/* Modify or remove the event listener if we already had one. */
if (ln) {
if (callback == NULL)
listDelNode(RedisModule_EventListeners,ln);
else
el->callback = callback; /* Update the callback with the new one. */
return REDISMODULE_OK;
}
/* No event found, we need to add a new one. */
el = zmalloc(sizeof(*el));
el->module = ctx->module;
el->event = event;
el->callback = callback;
listAddNodeTail(RedisModule_EventListeners,el);
return REDISMODULE_OK;
}
/* --------------------------------------------------------------------------
/* --------------------------------------------------------------------------
* Modules API internals
* Modules API internals
* -------------------------------------------------------------------------- */
* -------------------------------------------------------------------------- */
...
...
src/redismodule.h
View file @
bebb2e19
...
@@ -162,6 +162,75 @@ typedef uint64_t RedisModuleTimerID;
...
@@ -162,6 +162,75 @@ typedef uint64_t RedisModuleTimerID;
/* Declare that the module can handle errors with RedisModule_SetModuleOptions. */
/* Declare that the module can handle errors with RedisModule_SetModuleOptions. */
#define REDISMODULE_OPTIONS_HANDLE_IO_ERRORS (1<<0)
#define REDISMODULE_OPTIONS_HANDLE_IO_ERRORS (1<<0)
/* Server events definitions. */
#define REDISMODULE_EVENT_ID_REPLICATION_ROLE_CHANGED 0
#define REDISMODULE_EVENT_ID_PERSISTENCE 1
#define REDISMODULE_EVENT_ID_FLUSHDB 2
#define REDISMODULE_EVENT_ID_LOADING 3
#define REDISMODULE_EVENT_ID_CLIENT_CHANGE 4
#define REDISMODULE_EVENT_ID_SHUTDOWN 5
#define REDISMODULE_EVENT_ID_REPLICA_CHANGE 6
#define REDISMODULE_EVENT_ID_MASTER_LINK_CHANGE 7
typedef
struct
RedisModuleEvent
{
uint64_t
id
;
/* REDISMODULE_EVENT_ID_... defines. */
uint64_t
dataver
;
/* Version of the structure we pass as 'data'. */
}
RedisModuleEvent
;
RedisModuleEvent
RedisModuleEvent_ReplicationRoleChanged
=
{
REDISMODULE_EVENT_ID_REPLICATION_ROLE_CHANGED
,
0
},
RedisModuleEvent_Persistence
=
{
REDISMODULE_EVENT_ID_PERSISTENCE
,
0
},
RedisModuleEvent_FlushDB
=
{
REDISMODULE_EVENT_ID_FLUSHDB
,
0
},
RedisModuleEvent_Loading
=
{
REDISMODULE_EVENT_ID_LOADING
,
0
},
RedisModuleEvent_ClientChange
=
{
REDISMODULE_EVENT_ID_CLIENT_CHANGE
,
0
},
RedisModuleEvent_Shutdown
=
{
REDISMODULE_EVENT_ID_SHUTDOWN
,
0
},
RedisModuleEvent_ReplicaChange
=
{
REDISMODULE_EVENT_ID_REPLICA_CHANGE
,
0
},
RedisModuleEvent_MasterLinkChange
=
{
REDISMODULE_EVENT_ID_MASTER_LINK_CHANGE
,
0
};
typedef
int
(
*
RedisModuleEventCallback
)(
RedisModuleEvent
eid
,
uint64_t
subevent
,
void
*
data
);
/* Those are values that are used for the 'subevent' callback argument. */
#define REDISMODULE_EVENT_PERSISTENCE_RDB_START 0
#define REDISMODULE_EVENT_PERSISTENCE_RDB_END 1
#define REDISMODULE_EVENT_PERSISTENCE_AOF_START 2
#define REDISMODULE_EVENT_PERSISTENCE_AOF_END 3
#define REDISMODULE_EVENT_LOADING_START 0
#define REDISMODULE_EVENT_LOADING_END 1
#define REDISMODULE_EVENT_CLIENT_CHANGE_CONNECTED 0
#define REDISMODULE_EVENT_CLIENT_CHANGE_DISCONNECTED 1
#define REDISMODULE_EVENT_MASTER_LINK_UP 0
#define REDISMODULE_EVENT_MASTER_LINK_DOWN 1
#define REDISMODULE_EVENT_REPLICA_CHANGE_CONNECTED 0
#define REDISMODULE_EVENT_REPLICA_CHANGE_DISCONNECTED 1
/* ------------------------- End of common defines ------------------------ */
/* ------------------------- End of common defines ------------------------ */
#ifndef REDISMODULE_CORE
#ifndef REDISMODULE_CORE
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment