From: Nick Porter Date: Fri, 3 May 2024 11:20:41 +0000 (+0100) Subject: Add SQL driver options for trunks X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=9739a9de5fbe16f04d94b50fd31058df33726048;p=thirdparty%2Ffreeradius-server.git Add SQL driver options for trunks Allowing for one driver at a time to be converted to trunks --- diff --git a/src/modules/rlm_sql/rlm_sql.c b/src/modules/rlm_sql/rlm_sql.c index 5c4e0fc9285..cbe7314d7b6 100644 --- a/src/modules/rlm_sql/rlm_sql.c +++ b/src/modules/rlm_sql/rlm_sql.c @@ -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[]){ /* diff --git a/src/modules/rlm_sql/rlm_sql.h b/src/modules/rlm_sql/rlm_sql.h index 3f85952760e..fb3082d65a1 100644 --- a/src/modules/rlm_sql/rlm_sql.h +++ b/src/modules/rlm_sql/rlm_sql.h @@ -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 {