summaryrefslogtreecommitdiff
path: root/examples
diff options
context:
space:
mode:
authorMichael Grunder <michael.grunder@gmail.com>2020-07-29 11:53:03 -0700
committerGitHub <noreply@github.com>2020-07-29 11:53:03 -0700
commitd8ff72387d5474e56228a3403dc91354d9916189 (patch)
tree1a6fa645967132b78d92d52de9c9175b429236ba /examples
parentbe32bcdc8e84ae7dc091ceeffca2c5d4126f415c (diff)
Move SSL management to a distinct private pointer. (#855)
We need to allow our users to use redisContext->privdata as context for any RESP3 PUSH messages, which means we can't use it for managing SSL connections. Bulletpoints: * Create a secondary redisContext member for internal use only called privctx and rename the redisContextFuncs->free_privdata accordingly. * Adds a `free_privdata` function pointer so the user can tie allocated memory to the lifetime of a redisContext (like they can already do with redisAsyncContext) * Enables SSL tests in .travis.yml
Diffstat (limited to 'examples')
-rw-r--r--examples/example-push.c21
1 files changed, 17 insertions, 4 deletions
diff --git a/examples/example-push.c b/examples/example-push.c
index 23dbd17..2d4ab4d 100644
--- a/examples/example-push.c
+++ b/examples/example-push.c
@@ -97,6 +97,13 @@ void pushReplyHandler(void *privdata, void *r) {
freeReplyObject(reply);
}
+/* We aren't actually freeing anything here, but it is included to show that we can
+ * have hiredis call our data destructor when freeing the context */
+void privdata_dtor(void *privdata) {
+ unsigned int *icount = privdata;
+ printf("privdata_dtor(): In context privdata dtor (invalidations: %u)\n", *icount);
+}
+
int main(int argc, char **argv) {
unsigned int j, invalidations = 0;
redisContext *c;
@@ -108,6 +115,16 @@ int main(int argc, char **argv) {
redisOptions o = {0};
REDIS_OPTIONS_SET_TCP(&o, hostname, port);
+ /* Set our context privdata to the address of our invalidation counter. Each
+ * time our PUSH handler is called, hiredis will pass the privdata for context.
+ *
+ * This could also be done after we create the context like so:
+ *
+ * c->privdata = &invalidations;
+ * c->free_privdata = privdata_dtor;
+ */
+ REDIS_OPTIONS_SET_PRIVDATA(&o, &invalidations, privdata_dtor);
+
/* Set our custom PUSH message handler */
o.push_cb = pushReplyHandler;
@@ -118,10 +135,6 @@ int main(int argc, char **argv) {
/* Enable RESP3 and turn on client tracking */
enableClientTracking(c);
- /* Set our context privdata to the address of our invalidation counter. Each
- * time our PUSH handler is called, hiredis will pass the privdata for context */
- c->privdata = &invalidations;
-
/* Set some keys and then read them back. Once we do that, Redis will deliver
* invalidation push messages whenever the key is modified */
for (j = 0; j < KEY_COUNT; j++) {