Skip to content

Commit

Permalink
Merge remote-tracking branch 'upstream/master' into api-fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
bjosv committed Aug 24, 2023
2 parents 0a3f1a6 + 9d306c4 commit 157f57f
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 17 deletions.
13 changes: 10 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -215,11 +215,18 @@ It is that the slotmap has been updated.
```c
int redisClusterSetEventCallback(redisClusterContext *cc,
void(fn)(const redisClusterContext *cc, int event));
void(fn)(const redisClusterContext *cc, int event,
void *privdata),
void *privdata);
```

The callback is called with `event` set to `HIRCLUSTER_EVENT_SLOTMAP_UPDATED`
when the slotmap has been updated.
The callback is called with `event` set to one of the following values:

* `HIRCLUSTER_EVENT_SLOTMAP_UPDATED` when the slot mapping has been updated;
* `HIRCLUSTER_EVENT_READY` when the slot mapping has been fetched for the first
time and the client is ready to accept commands;
* `HIRCLUSTER_EVENT_FREE_CONTEXT` when the cluster context is being freed, so
that the user can free the event privdata.

#### Events per connection

Expand Down
16 changes: 14 additions & 2 deletions hircluster.c
Original file line number Diff line number Diff line change
Expand Up @@ -1418,7 +1418,12 @@ static int updateNodesAndSlotmap(redisClusterContext *cc, dict *nodes) {
dictRelease(oldnodes);
}
if (cc->event_callback != NULL) {
cc->event_callback(cc, HIRCLUSTER_EVENT_SLOTMAP_UPDATED);
cc->event_callback(cc, HIRCLUSTER_EVENT_SLOTMAP_UPDATED,
cc->event_privdata);
if (cc->route_version == 1) {
/* Special event the first time the slotmap was updated. */
cc->event_callback(cc, HIRCLUSTER_EVENT_READY, cc->event_privdata);
}
}
return REDIS_OK;

Expand Down Expand Up @@ -1490,6 +1495,11 @@ void redisClusterFree(redisClusterContext *cc) {
if (cc == NULL)
return;

if (cc->event_callback) {
cc->event_callback(cc, HIRCLUSTER_EVENT_FREE_CONTEXT,
cc->event_privdata);
}

if (cc->connect_timeout) {
hi_free(cc->connect_timeout);
cc->connect_timeout = NULL;
Expand Down Expand Up @@ -2900,9 +2910,11 @@ int redisClusterSetConnectCallback(redisClusterContext *cc,

int redisClusterSetEventCallback(redisClusterContext *cc,
void(fn)(const redisClusterContext *cc,
int event)) {
int event, void *privdata),
void *privdata) {
if (cc->event_callback == NULL) {
cc->event_callback = fn;
cc->event_privdata = privdata;
return REDIS_OK;
}
return REDIS_ERR;
Expand Down
11 changes: 8 additions & 3 deletions hircluster.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@

/* Events, for redisClusterSetEventCallback() */
#define HIRCLUSTER_EVENT_SLOTMAP_UPDATED 1
#define HIRCLUSTER_EVENT_READY 2
#define HIRCLUSTER_EVENT_FREE_CONTEXT 3

#ifdef __cplusplus
extern "C" {
Expand Down Expand Up @@ -133,7 +135,9 @@ typedef struct redisClusterContext {
sslInitFn *ssl_init_fn; /* Func ptr for SSL context initiation */

void (*on_connect)(const struct redisContext *c, int status);
void (*event_callback)(const struct redisClusterContext *cc, int event);
void (*event_callback)(const struct redisClusterContext *cc, int event,
void *privdata);
void *event_privdata;

} redisClusterContext;

Expand Down Expand Up @@ -213,10 +217,11 @@ void redisClusterSetMaxRedirect(redisClusterContext *cc,
int redisClusterSetConnectCallback(redisClusterContext *cc,
void(fn)(const redisContext *c, int status));

/* A hook for events, currently only the slot map updated notification. */
/* A hook for events. */
int redisClusterSetEventCallback(redisClusterContext *cc,
void(fn)(const redisClusterContext *cc,
int event));
int event, void *privdata),
void *privdata);

/* Blocking
* The following functions will block for a reply, or return NULL if there was
Expand Down
21 changes: 17 additions & 4 deletions tests/clusterclient.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,23 @@ void printReply(const redisReply *reply) {
}
}

void eventCallback(const redisClusterContext *cc, int event) {
void eventCallback(const redisClusterContext *cc, int event, void *privdata) {
(void)cc;
char *e = event == HIRCLUSTER_EVENT_SLOTMAP_UPDATED ? "slotmap-updated" :
"unknown";
(void)privdata;
char *e = NULL;
switch (event) {
case HIRCLUSTER_EVENT_SLOTMAP_UPDATED:
e = "slotmap-updated";
break;
case HIRCLUSTER_EVENT_READY:
e = "ready";
break;
case HIRCLUSTER_EVENT_FREE_CONTEXT:
e = "free-context";
break;
default:
e = "unknown";
}
printf("Event: %s\n", e);
}

Expand Down Expand Up @@ -67,7 +80,7 @@ int main(int argc, char **argv) {
redisClusterSetOptionConnectTimeout(cc, timeout);
redisClusterSetOptionRouteUseSlots(cc);
if (show_events) {
redisClusterSetEventCallback(cc, eventCallback);
redisClusterSetEventCallback(cc, eventCallback, NULL);
}

if (redisClusterConnect2(cc) != REDIS_OK) {
Expand Down
21 changes: 17 additions & 4 deletions tests/clusterclient_async.c
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,23 @@ void disconnectCallback(const redisAsyncContext *ac, int status) {
// printf("Disconnected from %s:%d\n", ac->c.tcp.host, ac->c.tcp.port);
}

void eventCallback(const redisClusterContext *cc, int event) {
void eventCallback(const redisClusterContext *cc, int event, void *privdata) {
(void)cc;
char *e = event == HIRCLUSTER_EVENT_SLOTMAP_UPDATED ? "slotmap-updated" :
"unknown";
(void)privdata;
char *e = NULL;
switch (event) {
case HIRCLUSTER_EVENT_SLOTMAP_UPDATED:
e = "slotmap-updated";
break;
case HIRCLUSTER_EVENT_READY:
e = "ready";
break;
case HIRCLUSTER_EVENT_FREE_CONTEXT:
e = "free-context";
break;
default:
e = "unknown";
}
printf("Event: %s\n", e);
}

Expand Down Expand Up @@ -102,7 +115,7 @@ int main(int argc, char **argv) {
redisClusterSetOptionRouteUseSlots(acc->cc);
}
if (show_events) {
redisClusterSetEventCallback(acc->cc, eventCallback);
redisClusterSetEventCallback(acc->cc, eventCallback, NULL);
}

if (redisClusterConnect2(acc->cc) != REDIS_OK) {
Expand Down
4 changes: 3 additions & 1 deletion tests/scripts/moved-redirect-test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,10 @@ fi

# Check the output from clusterclient
expected="Event: slotmap-updated
Event: ready
Event: slotmap-updated
bar"
bar
Event: free-context"

echo "$expected" | diff -u - "$testname.out" || exit 99

Expand Down

0 comments on commit 157f57f

Please sign in to comment.