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

Introduce configuration options in the cluster API #137

Draft
wants to merge 13 commits into
base: main
Choose a base branch
from
Draft
32 changes: 15 additions & 17 deletions examples/cluster-async-tls.c
Original file line number Diff line number Diff line change
Expand Up @@ -66,26 +66,24 @@ int main(int argc, char **argv) {
exit(1);
}

valkeyClusterAsyncContext *acc = valkeyClusterAsyncContextInit();
assert(acc);
valkeyClusterAsyncSetConnectCallback(acc, connectCallback);
valkeyClusterAsyncSetDisconnectCallback(acc, disconnectCallback);
valkeyClusterSetOptionAddNodes(acc->cc, CLUSTER_NODE_TLS);
valkeyClusterSetOptionRouteUseSlots(acc->cc);
valkeyClusterSetOptionParseSlaves(acc->cc);
valkeyClusterSetOptionEnableTLS(acc->cc, tls);

if (valkeyClusterConnect2(acc->cc) != VALKEY_OK) {
printf("Error: %s\n", acc->cc->errstr);
exit(-1);
}
valkeyClusterOptions options = {0};
options.initial_nodes = CLUSTER_NODE_TLS;
options.async_connect_cb = connectCallback;
options.async_disconnect_cb = disconnectCallback;
options.options = VALKEY_OPT_USE_CLUSTER_SLOTS;
valkeyClusterSetOptionEnableTLS(&options, tls);

struct event_base *base = event_base_new();
valkeyClusterLibeventAttach(acc, base);
valkeyClusterSetOptionUseLibevent(&options, base);

valkeyClusterAsyncContext *acc = valkeyClusterAsyncConnectWithOptions(&options);
if (acc == NULL || acc->err != 0) {
printf("Error: %s\n", acc ? acc->errstr : "OOM");
exit(-1);
}

int status;
status = valkeyClusterAsyncCommand(acc, setCallback, (char *)"THE_ID",
"SET %s %s", "key", "value");
int status = valkeyClusterAsyncCommand(acc, setCallback, (char *)"THE_ID",
"SET %s %s", "key", "value");
if (status != VALKEY_OK) {
printf("error: err=%d errstr=%s\n", acc->err, acc->errstr);
}
Expand Down
40 changes: 21 additions & 19 deletions examples/cluster-async.c
Original file line number Diff line number Diff line change
Expand Up @@ -49,53 +49,55 @@ void disconnectCallback(const valkeyAsyncContext *ac, int status) {
int main(int argc, char **argv) {
(void)argc;
(void)argv;
struct event_base *base = event_base_new();

valkeyClusterOptions options = {0};
options.initial_nodes = "127.0.0.1:7000";
options.async_connect_cb = connectCallback;
options.async_disconnect_cb = disconnectCallback;
valkeyClusterSetOptionUseLibevent(&options, base);

printf("Connecting...\n");
valkeyClusterAsyncContext *cc =
valkeyClusterAsyncConnect("127.0.0.1:7000", VALKEYCLUSTER_FLAG_NULL);
if (!cc) {
valkeyClusterAsyncContext *acc = valkeyClusterAsyncConnectWithOptions(&options);
if (!acc) {
printf("Error: Allocation failure\n");
exit(-1);
} else if (cc->err) {
printf("Error: %s\n", cc->errstr);
} else if (acc->err) {
printf("Error: %s\n", acc->errstr);
// handle error
exit(-1);
}

struct event_base *base = event_base_new();
valkeyClusterLibeventAttach(cc, base);
valkeyClusterAsyncSetConnectCallback(cc, connectCallback);
valkeyClusterAsyncSetDisconnectCallback(cc, disconnectCallback);

int status;
status = valkeyClusterAsyncCommand(cc, setCallback, (char *)"THE_ID",
status = valkeyClusterAsyncCommand(acc, setCallback, (char *)"THE_ID",
"SET %s %s", "key", "value");
if (status != VALKEY_OK) {
printf("error: err=%d errstr=%s\n", cc->err, cc->errstr);
printf("error: err=%d errstr=%s\n", acc->err, acc->errstr);
}

status = valkeyClusterAsyncCommand(cc, getCallback, (char *)"THE_ID",
status = valkeyClusterAsyncCommand(acc, getCallback, (char *)"THE_ID",
"GET %s", "key");
if (status != VALKEY_OK) {
printf("error: err=%d errstr=%s\n", cc->err, cc->errstr);
printf("error: err=%d errstr=%s\n", acc->err, acc->errstr);
}

status = valkeyClusterAsyncCommand(cc, setCallback, (char *)"THE_ID",
status = valkeyClusterAsyncCommand(acc, setCallback, (char *)"THE_ID",
"SET %s %s", "key2", "value2");
if (status != VALKEY_OK) {
printf("error: err=%d errstr=%s\n", cc->err, cc->errstr);
printf("error: err=%d errstr=%s\n", acc->err, acc->errstr);
}

status = valkeyClusterAsyncCommand(cc, getCallback, (char *)"THE_ID",
status = valkeyClusterAsyncCommand(acc, getCallback, (char *)"THE_ID",
"GET %s", "key2");
if (status != VALKEY_OK) {
printf("error: err=%d errstr=%s\n", cc->err, cc->errstr);
printf("error: err=%d errstr=%s\n", acc->err, acc->errstr);
}

printf("Dispatch..\n");
event_base_dispatch(base);

printf("Done..\n");
valkeyClusterAsyncFree(cc);
valkeyClusterAsyncFree(acc);
event_base_free(base);
return 0;
}
31 changes: 13 additions & 18 deletions examples/cluster-clientside-caching-async.c
Original file line number Diff line number Diff line change
Expand Up @@ -127,11 +127,8 @@ void disconnectCallback(const valkeyAsyncContext *ac, int status) {
/* Helper to modify keys using a separate client. */
void modifyKey(const char *key, const char *value) {
printf("Modify key: '%s'\n", key);
valkeyClusterContext *cc = valkeyClusterContextInit();
int status = valkeyClusterSetOptionAddNodes(cc, CLUSTER_NODE);
assert(status == VALKEY_OK);
status = valkeyClusterConnect2(cc);
assert(status == VALKEY_OK);
valkeyClusterContext *cc = valkeyClusterConnect(CLUSTER_NODE);
assert(cc);

valkeyReply *reply = valkeyClusterCommand(cc, "SET %s %s", key, value);
assert(reply != NULL);
Expand All @@ -143,24 +140,22 @@ void modifyKey(const char *key, const char *value) {
int main(int argc, char **argv) {
(void)argc;
(void)argv;
valkeyClusterAsyncContext *acc = valkeyClusterAsyncContextInit();
struct event_base *base = event_base_new();

valkeyClusterOptions options = {0};
options.initial_nodes = CLUSTER_NODE;
options.async_connect_nc_cb = connectCallbackNC;
options.async_disconnect_cb = disconnectCallback;
valkeyClusterSetOptionUseLibevent(&options, base);

valkeyClusterAsyncContext *acc = valkeyClusterAsyncContextInit(&options);
assert(acc);

int status;
status = valkeyClusterAsyncSetConnectCallbackNC(acc, connectCallbackNC);
assert(status == VALKEY_OK);
status = valkeyClusterAsyncSetDisconnectCallback(acc, disconnectCallback);
assert(status == VALKEY_OK);
status = valkeyClusterSetEventCallback(acc->cc, eventCallback, acc);
assert(status == VALKEY_OK);
status = valkeyClusterSetOptionAddNodes(acc->cc, CLUSTER_NODE);
assert(status == VALKEY_OK);

struct event_base *base = event_base_new();
status = valkeyClusterLibeventAttach(acc, base);
status = valkeyClusterAsyncSetEventCallback(acc, eventCallback, acc);
assert(status == VALKEY_OK);

status = valkeyClusterAsyncConnect2(acc);
status = valkeyClusterAsyncConnect(acc);
assert(status == VALKEY_OK);

event_base_dispatch(base);
Expand Down
11 changes: 6 additions & 5 deletions examples/cluster-simple.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,12 @@ int main(int argc, char **argv) {
UNUSED(argv);
struct timeval timeout = {1, 500000}; // 1.5s

valkeyClusterContext *cc = valkeyClusterContextInit();
valkeyClusterSetOptionAddNodes(cc, "127.0.0.1:7000");
valkeyClusterSetOptionConnectTimeout(cc, timeout);
valkeyClusterSetOptionRouteUseSlots(cc);
valkeyClusterConnect2(cc);
valkeyClusterOptions options = {0};
options.initial_nodes = "127.0.0.1:7000";
options.connect_timeout = &timeout;
options.options = VALKEY_OPT_USE_CLUSTER_SLOTS;

valkeyClusterContext *cc = valkeyClusterConnectWithOptions(&options);
if (!cc) {
printf("Error: Allocation failure\n");
exit(-1);
Expand Down
14 changes: 7 additions & 7 deletions examples/cluster-tls.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,13 @@ int main(int argc, char **argv) {

struct timeval timeout = {1, 500000}; // 1.5s

valkeyClusterContext *cc = valkeyClusterContextInit();
valkeyClusterSetOptionAddNodes(cc, CLUSTER_NODE_TLS);
valkeyClusterSetOptionConnectTimeout(cc, timeout);
valkeyClusterSetOptionRouteUseSlots(cc);
valkeyClusterSetOptionParseSlaves(cc);
valkeyClusterSetOptionEnableTLS(cc, tls);
valkeyClusterConnect2(cc);
valkeyClusterOptions options = {0};
options.initial_nodes = CLUSTER_NODE_TLS;
options.connect_timeout = &timeout;
options.options = VALKEY_OPT_USE_CLUSTER_SLOTS;
valkeyClusterSetOptionEnableTLS(&options, tls);

valkeyClusterContext *cc = valkeyClusterConnectWithOptions(&options);
if (!cc) {
printf("Error: Allocation failure\n");
exit(-1);
Expand Down
11 changes: 6 additions & 5 deletions include/valkey/adapters/ae.h
Original file line number Diff line number Diff line change
Expand Up @@ -140,14 +140,15 @@ static int valkeyAeAttachAdapter(valkeyAsyncContext *ac, void *loop) {
}

VALKEY_UNUSED
static int valkeyClusterAeAttach(valkeyClusterAsyncContext *acc,
aeEventLoop *loop) {
if (acc == NULL || loop == NULL) {
static int valkeyClusterSetOptionUseAe(valkeyClusterOptions *options,
aeEventLoop *loop) {
if (options == NULL || loop == NULL) {
return VALKEY_ERR;
}

acc->attach_fn = valkeyAeAttachAdapter;
acc->attach_data = loop;
options->attach_fn = valkeyAeAttachAdapter;
options->attach_data = loop;
return VALKEY_OK;
}

#endif /* VALKEY_ADAPTERS_AE_H */
10 changes: 5 additions & 5 deletions include/valkey/adapters/glib.h
Original file line number Diff line number Diff line change
Expand Up @@ -153,14 +153,14 @@ static int valkeyGlibAttachAdapter(valkeyAsyncContext *ac, void *context) {
}

VALKEY_UNUSED
static int valkeyClusterGlibAttach(valkeyClusterAsyncContext *acc,
GMainContext *context) {
if (acc == NULL) { // A NULL context is accepted.
static int valkeyClusterSetOptionUseGlib(valkeyClusterOptions *options,
GMainContext *context) {
if (options == NULL) { // A NULL context is accepted.
return VALKEY_ERR;
}

acc->attach_fn = valkeyGlibAttachAdapter;
acc->attach_data = context;
options->attach_fn = valkeyGlibAttachAdapter;
options->attach_data = context;
return VALKEY_OK;
}

Expand Down
8 changes: 4 additions & 4 deletions include/valkey/adapters/ivykis.h
Original file line number Diff line number Diff line change
Expand Up @@ -85,17 +85,17 @@ static int valkeyIvykisAttach(valkeyAsyncContext *ac) {
}

/* Internal adapter function with correct function signature. */
static int valkeyClusterIvykisAttachAdapter(valkeyAsyncContext *ac, VALKEY_UNUSED void *) {
static int valkeyIvykisAttachAdapter(valkeyAsyncContext *ac, VALKEY_UNUSED void *) {
return valkeyIvykisAttach(ac);
}

VALKEY_UNUSED
static int valkeyClusterIvykisAttach(valkeyClusterAsyncContext *acc) {
if (acc == NULL) {
static int valkeyClusterSetOptionUseIvykis(valkeyClusterOptions *options) {
if (options == NULL) {
return VALKEY_ERR;
}

acc->attach_fn = valkeyClusterIvykisAttachAdapter;
options->attach_fn = valkeyIvykisAttachAdapter;
return VALKEY_OK;
}

Expand Down
10 changes: 5 additions & 5 deletions include/valkey/adapters/libev.h
Original file line number Diff line number Diff line change
Expand Up @@ -193,14 +193,14 @@ static int valkeyLibevAttachAdapter(valkeyAsyncContext *ac, void *loop) {
}

VALKEY_UNUSED
static int valkeyClusterLibevAttach(valkeyClusterAsyncContext *acc,
struct ev_loop *loop) {
if (acc == NULL || loop == NULL) {
static int valkeyClusterSetOptionUseLibev(valkeyClusterOptions *options,
struct ev_loop *loop) {
if (options == NULL || loop == NULL) {
return VALKEY_ERR;
}

acc->attach_fn = valkeyLibevAttachAdapter;
acc->attach_data = loop;
options->attach_fn = valkeyLibevAttachAdapter;
options->attach_data = loop;
return VALKEY_OK;
}

Expand Down
11 changes: 6 additions & 5 deletions include/valkey/adapters/libevent.h
Original file line number Diff line number Diff line change
Expand Up @@ -182,14 +182,15 @@ static int valkeyLibeventAttachAdapter(valkeyAsyncContext *ac, void *base) {
}

VALKEY_UNUSED
static int valkeyClusterLibeventAttach(valkeyClusterAsyncContext *acc,
struct event_base *base) {
if (acc == NULL || base == NULL) {
static int valkeyClusterSetOptionUseLibevent(valkeyClusterOptions *options,
struct event_base *base) {
if (options == NULL || base == NULL) {
return VALKEY_ERR;
}

acc->attach_fn = valkeyLibeventAttachAdapter;
acc->attach_data = base;
options->attach_fn = valkeyLibeventAttachAdapter;
options->attach_data = base;
return VALKEY_OK;
}

#endif /* VALKEY_ADAPTERS_LIBEVENT_H */
10 changes: 5 additions & 5 deletions include/valkey/adapters/libhv.h
Original file line number Diff line number Diff line change
Expand Up @@ -129,14 +129,14 @@ static int valkeyLibhvAttachAdapter(valkeyAsyncContext *ac, void *loop) {
}

VALKEY_UNUSED
static int valkeyClusterLibhvAttach(valkeyClusterAsyncContext *acc,
hloop_t *loop) {
if (acc == NULL || loop == NULL) {
static int valkeyClusterSetOptionUseLibhv(valkeyClusterOptions *options,
hloop_t *loop) {
if (options == NULL || loop == NULL) {
return VALKEY_ERR;
}

acc->attach_fn = valkeyLibhvAttachAdapter;
acc->attach_data = loop;
options->attach_fn = valkeyLibhvAttachAdapter;
options->attach_data = loop;
return VALKEY_OK;
}

Expand Down
10 changes: 5 additions & 5 deletions include/valkey/adapters/libsdevent.h
Original file line number Diff line number Diff line change
Expand Up @@ -184,14 +184,14 @@ static int valkeyLibsdeventAttachAdapter(valkeyAsyncContext *ac, void *event) {
}

VALKEY_UNUSED
static int valkeyClusterLibsdeventAttach(valkeyClusterAsyncContext *acc,
struct sd_event *event) {
if (acc == NULL || event == NULL) {
static int valkeyClusterSetOptionUseLibsdevent(valkeyClusterOptions *options,
struct sd_event *event) {
if (options == NULL || event == NULL) {
return VALKEY_ERR;
}

acc->attach_fn = valkeyLibsdeventAttachAdapter;
acc->attach_data = event;
options->attach_fn = valkeyLibsdeventAttachAdapter;
options->attach_data = event;
return VALKEY_OK;
}

Expand Down
11 changes: 6 additions & 5 deletions include/valkey/adapters/libuv.h
Original file line number Diff line number Diff line change
Expand Up @@ -203,14 +203,15 @@ static int valkeyLibuvAttachAdapter(valkeyAsyncContext *ac, void *loop) {
}

VALKEY_UNUSED
static int valkeyClusterLibuvAttach(valkeyClusterAsyncContext *acc,
uv_loop_t *loop) {
if (acc == NULL || loop == NULL) {
static int valkeyClusterSetOptionUseLibuv(valkeyClusterOptions *options,
uv_loop_t *loop) {
if (options == NULL || loop == NULL) {
return VALKEY_ERR;
}

acc->attach_fn = valkeyLibuvAttachAdapter;
acc->attach_data = loop;
options->attach_fn = valkeyLibuvAttachAdapter;
options->attach_data = loop;
return VALKEY_OK;
}

#endif /* VALKEY_ADAPTERS_LIBUV_H */
10 changes: 5 additions & 5 deletions include/valkey/adapters/macosx.h
Original file line number Diff line number Diff line change
Expand Up @@ -149,14 +149,14 @@ static int valkeyMacOSAttachAdapter(valkeyAsyncContext *ac, void *loop) {
}

VALKEY_UNUSED
static int valkeyClusterMacOSAttach(valkeyClusterAsyncContext *acc,
CFRunLoopRef loop) {
if (acc == NULL || loop == NULL) {
static int valkeyClusterSetOptionUseMacOS(valkeyClusterOptions *options,
CFRunLoopRef loop) {
if (options == NULL || loop == NULL) {
return VALKEY_ERR;
}

acc->attach_fn = valkeyMacOSAttachAdapter;
acc->attach_data = loop;
options->attach_fn = valkeyMacOSAttachAdapter;
options->attach_data = loop;
return VALKEY_OK;
}

Expand Down
Loading
Loading