Pass a threadsafe ctx into fr_connection_pool create callback
[freeradius.git] / src / modules / rlm_redis / rlm_redis.c
index f752719..f07d494 100644 (file)
@@ -39,10 +39,8 @@ static const CONF_PARSER module_config[] = {
        { NULL, -1, 0, NULL, NULL} /* end the list */
 };
 
-static int mod_conn_delete(UNUSED void *instance, void *handle)
+static int _mod_conn_free(REDISSOCK *dissocket)
 {
-       REDISSOCK *dissocket = handle;
-
        redisFree(dissocket->conn);
 
        if (dissocket->reply) {
@@ -50,13 +48,12 @@ static int mod_conn_delete(UNUSED void *instance, void *handle)
                dissocket->reply = NULL;
        }
 
-       talloc_free(dissocket);
-       return 1;
+       return 0;
 }
 
-static void *mod_conn_create(void *ctx)
+static void *mod_conn_create(TALLOC_CTX *ctx, void *instance)
 {
-       REDIS_INST *inst = ctx;
+       REDIS_INST *inst = instance;
        REDISSOCK *dissocket = NULL;
        redisContext *conn;
        redisReply *reply = NULL;
@@ -70,8 +67,8 @@ static void *mod_conn_create(void *ctx)
 
                reply = redisCommand(conn, buffer);
                if (!reply) {
-                       ERROR("rlm_redis (%s): Failed to run AUTH",
-                              inst->xlat_name);
+                       ERROR("rlm_redis (%s): Failed to run AUTH", inst->xlat_name);
+
                do_close:
                        if (reply) freeReplyObject(reply);
                        redisFree(conn);
@@ -123,8 +120,9 @@ static void *mod_conn_create(void *ctx)
                }
        }
 
-       dissocket = talloc_zero(inst, REDISSOCK);
+       dissocket = talloc_zero(ctx, REDISSOCK);
        dissocket->conn = conn;
+       talloc_set_destructor(dissocket, _mod_conn_free);
 
        return dissocket;
 }
@@ -278,7 +276,7 @@ static int mod_instantiate(CONF_SECTION *conf, void *instance)
 
        xlat_register(inst->xlat_name, redis_xlat, NULL, inst); /* FIXME! */
 
-       inst->pool = fr_connection_pool_init(conf, inst, mod_conn_create, NULL, mod_conn_delete, NULL);
+       inst->pool = fr_connection_pool_init(conf, inst, mod_conn_create, NULL, NULL, NULL);
        if (!inst->pool) {
                return -1;
        }