-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathcluster-tls.c
61 lines (50 loc) · 1.58 KB
/
cluster-tls.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#include <valkey/cluster.h>
#include <valkey/tls.h>
#include <stdio.h>
#include <stdlib.h>
#define CLUSTER_NODE_TLS "127.0.0.1:7301"
int main(int argc, char **argv) {
UNUSED(argc);
UNUSED(argv);
valkeyTLSContext *tls;
valkeyTLSContextError tls_error;
valkeyInitOpenSSL();
tls = valkeyCreateTLSContext("ca.crt", NULL, "client.crt", "client.key",
NULL, &tls_error);
if (!tls) {
printf("TLS Context error: %s\n", valkeyTLSContextGetError(tls_error));
exit(1);
}
struct timeval timeout = {1, 500000}; // 1.5s
valkeyClusterOptions options = {0};
options.initial_nodes = CLUSTER_NODE_TLS;
options.connect_timeout = &timeout;
options.tls = tls;
options.tls_init_fn = &valkeyInitiateTLSWithContext;
valkeyClusterContext *cc = valkeyClusterConnectWithOptions(&options);
if (!cc) {
printf("Error: Allocation failure\n");
exit(-1);
} else if (cc->err) {
printf("Error: %s\n", cc->errstr);
// handle error
exit(-1);
}
valkeyReply *reply = valkeyClusterCommand(cc, "SET %s %s", "key", "value");
if (!reply) {
printf("Reply missing: %s\n", cc->errstr);
exit(-1);
}
printf("SET: %s\n", reply->str);
freeReplyObject(reply);
valkeyReply *reply2 = valkeyClusterCommand(cc, "GET %s", "key");
if (!reply2) {
printf("Reply missing: %s\n", cc->errstr);
exit(-1);
}
printf("GET: %s\n", reply2->str);
freeReplyObject(reply2);
valkeyClusterFree(cc);
valkeyFreeTLSContext(tls);
return 0;
}