]> git.ipfire.org Git - thirdparty/freeradius-server.git/commitdiff
Add SQL driver options for trunks
authorNick Porter <nick@portercomputing.co.uk>
Fri, 3 May 2024 11:20:41 +0000 (12:20 +0100)
committerArran Cudbard-Bell <a.cudbardb@freeradius.org>
Fri, 7 Jun 2024 02:26:58 +0000 (22:26 -0400)
Allowing for one driver at a time to be converted to trunks

src/modules/rlm_sql/rlm_sql.c
src/modules/rlm_sql/rlm_sql.h

index 5c4e0fc92851b4ba02b37484b1a68dbf2bdb807c..cbe7314d7b6f26a26baf3d714a24f44f4378c424 100644 (file)
@@ -1879,11 +1879,6 @@ static int mod_instantiate(module_inst_ctx_t const *mctx)
                return -1;
        }
 
-       /*
-        *      Initialise the connection pool for this instance
-        */
-       INFO("Attempting to connect to database \"%s\"", inst->config.sql_db);
-
        /*
         *      Driver must be instantiated before we call pool init
         *      else any configuration elements dynamically produced
@@ -1904,6 +1899,32 @@ static int mod_instantiate(module_inst_ctx_t const *mctx)
                return -1;
        }
 
+       if (inst->driver->uses_trunks) {
+               CONF_SECTION    *cs;
+
+               /*
+                *      The "pool" conf section is either used for legacy pool
+                *      connections or trunk connections depending on the
+                *      driver configuration.
+                */
+               cs = cf_section_find(conf, "pool", NULL);
+               if (!cs) cs = cf_section_alloc(conf, conf, "pool", NULL);
+               if (cf_section_rules_push(cs, fr_trunk_config) < 0) return -1;
+               if (cf_section_parse(&inst->config, &inst->config.trunk_conf, cs) < 0) return -1;
+
+               /*
+                *      SQL trunks can only have one running request per connection.
+                */
+               inst->config.trunk_conf.target_req_per_conn = 1;
+               inst->config.trunk_conf.max_req_per_conn = 1;
+               return 0;
+       }
+
+       /*
+        *      Initialise the connection pool for this instance
+        */
+       INFO("Attempting to connect to database \"%s\"", inst->config.sql_db);
+
        inst->pool = module_rlm_connection_pool_init(conf, inst, sql_mod_conn_create, NULL, NULL, NULL, NULL);
        if (!inst->pool) return -1;
 
@@ -2013,6 +2034,25 @@ static int mod_bootstrap(module_inst_ctx_t const *mctx)
        return 0;
 }
 
+/** Initialise thread specific data structure
+ *
+ */
+static int mod_thread_instantiate(module_thread_inst_ctx_t const *mctx)
+{
+       rlm_sql_thread_t        *t = talloc_get_type_abort(mctx->thread, rlm_sql_thread_t);
+       rlm_sql_t               *inst = talloc_get_type_abort(mctx->mi->data, rlm_sql_t);
+
+       t->inst = inst;
+
+       if (!inst->driver->uses_trunks) return 0;
+
+       t->trunk = fr_trunk_alloc(t, mctx->el, &inst->driver->trunk_io_funcs,
+                                 &inst->config.trunk_conf, inst->name, t, false);
+       if (!t->trunk) return -1;
+
+       return 0;
+}
+
 /* globally exported name */
 module_rlm_t rlm_sql = {
        .common = {
@@ -2026,6 +2066,7 @@ module_rlm_t rlm_sql = {
                .instantiate    = mod_instantiate,
                .detach         = mod_detach,
                .thread_inst_size       = sizeof(rlm_sql_thread_t),
+               .thread_instantiate     = mod_thread_instantiate,
        },
        .bindings = (module_method_binding_t[]){
                /*
index 3f85952760eead701686b16520c9a9238c412fb7..fb3082d65a18ec5421cca30878bc9b08668e78f3 100644 (file)
@@ -95,6 +95,8 @@ typedef struct {
 
        char const              *connect_query;                 //!< Query executed after establishing
                                                                //!< new connection.
+
+       fr_trunk_conf_t         trunk_conf;                     //!< Configuration for trunk connections.
 } rlm_sql_config_t;
 
 typedef struct sql_inst rlm_sql_t;
@@ -205,6 +207,9 @@ typedef struct {
        sql_rcode_t     (*sql_finish_select_query)(rlm_sql_handle_t *handle, rlm_sql_config_t const *config);
 
        xlat_escape_legacy_t    sql_escape_func;
+
+       bool                    uses_trunks;            //!< Transitional flag for drivers which use trunks.
+       fr_trunk_io_funcs_t     trunk_io_funcs;         //!< Trunk callback functions for this driver.
 } rlm_sql_driver_t;
 
 struct sql_inst {