Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add non-const connect callback #205

Merged
merged 10 commits into from
Mar 18, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -459,6 +459,21 @@ aliased to `redisConnectCallback`:
void(const redisAsyncContext *ac, int status);
```

If `hiredis` >= v1.1.0 is used you can alternatively set the following
connect callback which will be passed a non-const `redisAsyncContext*` on
invocation (e.g. allowing to set a push callback).
bjosv marked this conversation as resolved.
Show resolved Hide resolved

```c
int redisClusterAsyncSetConnectCallbackNC(redisClusterAsyncContext *acc,
redisConnectCallbackNC *fn);
```

The callback function should have the following prototype,
aliased to `redisConnectCallbackNC`:
```c
void(redisAsyncContext *ac, int status);
```

On a connection attempt, the `status` argument is set to `REDIS_OK`
when the connection was successful.
The file description of the connection socket can be retrieved
Expand Down
27 changes: 23 additions & 4 deletions hircluster.c
Original file line number Diff line number Diff line change
Expand Up @@ -3715,6 +3715,11 @@ redisAsyncContext *actx_get_by_node(redisClusterAsyncContext *acc,
if (acc->onConnect) {
redisAsyncSetConnectCallback(ac, acc->onConnect);
}
#ifndef HIRCLUSTER_NO_NONCONST_CONNECT_CB
else if (acc->onConnectNC) {
redisAsyncSetConnectCallbackNC(ac, acc->onConnectNC);
}
#endif

if (acc->onDisconnect) {
redisAsyncSetDisconnectCallback(ac, acc->onDisconnect);
Expand Down Expand Up @@ -3775,12 +3780,26 @@ int redisClusterAsyncConnect2(redisClusterAsyncContext *acc) {

int redisClusterAsyncSetConnectCallback(redisClusterAsyncContext *acc,
redisConnectCallback *fn) {
if (acc->onConnect == NULL) {
acc->onConnect = fn;
return REDIS_OK;
if (acc->onConnect != NULL)
return REDIS_ERR;
#ifndef HIRCLUSTER_NO_NONCONST_CONNECT_CB
if (acc->onConnectNC != NULL)
return REDIS_ERR;
#endif
acc->onConnect = fn;
return REDIS_OK;
}

#ifndef HIRCLUSTER_NO_NONCONST_CONNECT_CB
int redisClusterAsyncSetConnectCallbackNC(redisClusterAsyncContext *acc,
redisConnectCallbackNC *fn) {
if (acc->onConnectNC != NULL || acc->onConnect == NULL) {
return REDIS_ERR;
}
return REDIS_ERR;
acc->onConnectNC = fn;
return REDIS_OK;
}
#endif

int redisClusterAsyncSetDisconnectCallback(redisClusterAsyncContext *acc,
redisDisconnectCallback *fn) {
Expand Down
12 changes: 12 additions & 0 deletions hircluster.h
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,11 @@
#define HIRCLUSTER_EVENT_READY 2
#define HIRCLUSTER_EVENT_FREE_CONTEXT 3

/* The non-const connect callback is missing in hiredis API prior v.1.1.0 */
#if !(HIREDIS_MAJOR >= 1 && HIREDIS_MINOR >= 1)
#define HIRCLUSTER_NO_NONCONST_CONNECT_CB
#endif

#ifdef __cplusplus
extern "C" {
#endif
Expand Down Expand Up @@ -159,6 +164,9 @@ typedef struct redisClusterAsyncContext {

/* Called when the first write event was received. */
redisConnectCallback *onConnect;
#ifndef HIRCLUSTER_NO_NONCONST_CONNECT_CB
redisConnectCallbackNC *onConnectNC;
#endif

} redisClusterAsyncContext;

Expand Down Expand Up @@ -286,6 +294,10 @@ void redisClusterAsyncFree(redisClusterAsyncContext *acc);

int redisClusterAsyncSetConnectCallback(redisClusterAsyncContext *acc,
redisConnectCallback *fn);
#ifndef HIRCLUSTER_NO_NONCONST_CONNECT_CB
int redisClusterAsyncSetConnectCallbackNC(redisClusterAsyncContext *acc,
redisConnectCallbackNC *fn);
#endif
int redisClusterAsyncSetDisconnectCallback(redisClusterAsyncContext *acc,
redisDisconnectCallback *fn);

Expand Down
Loading