2 * This program is free software; you can redistribute it and/or modify
3 * it under the terms of the GNU General Public License as published by
4 * the Free Software Foundation; either version 2 of the License, or
5 * (at your option) any later version.
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
12 * You should have received a copy of the GNU General Public License
13 * along with this program; if not, write to the Free Software
14 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19 * @brief Handle pools of connections (threads, sockets, etc.)
20 * @note This API must be used by all modules in the public distribution that
21 * maintain pools of connections.
23 * @copyright 2012 The FreeRADIUS server project
24 * @copyright 2012 Alan DeKok <aland@deployingradius.com>
28 #include <freeradius-devel/radiusd.h>
29 #include <freeradius-devel/rad_assert.h>
31 typedef struct fr_connection fr_connection_t;
33 static int fr_connection_pool_check(fr_connection_pool_t *pool);
35 /** An individual connection within the connection pool
37 * Defines connection counters, timestamps, and holds a pointer to the
38 * connection handle itself.
40 * @see fr_connection_pool_t
42 struct fr_connection {
43 fr_connection_t *prev; //!< Previous connection in list.
44 fr_connection_t *next; //!< Next connection in list.
46 time_t created; //!< Time connection was created.
47 time_t last_used; //!< Last time the connection was
50 uint64_t num_uses; //!< Number of times the connection
51 //!< has been reserved.
52 int in_use; //!< Whether the connection is currently
54 uint64_t number; //!< Unique ID assigned when the
55 //!< connection is created, these will
56 //!< monotonically increase over the
57 //!< lifetime of the connection pool.
58 void *connection; //!< Pointer to whatever the module
59 //!< uses for a connection handle.
64 * Defines the configuration of the connection pool, all the counters and
65 * timestamps related to the connection pool, the mutex that stops multiple
66 * threads leaving the pool in an inconsistent state, and the callbacks
67 * required to open, close and check the status of connections within the pool.
71 struct fr_connection_pool_t {
72 int start; //!< Number of initial connections
73 int min; //!< Minimum number of concurrent
74 //!< connections to keep open.
75 int max; //!< Maximum number of concurrent
76 //!< connections to allow.
77 int spare; //!< Number of spare connections to try
79 int cleanup_delay; //!< How long a connection can go unused
80 //!< for before it's closed
82 uint64_t max_uses; //!< Maximum number of times a
83 //!< connection can be used before being
85 int lifetime; //!< How long a connection can be open
86 //!< before being closed (irrespective
87 //!< of whether it's idle or not).
88 int idle_timeout; //!< How long a connection can be idle
89 //!< before being closed.
91 bool trigger; //!< If true execute connection triggers
92 //!< associated with the connection
95 bool spread; //!< If true requests will be spread
96 //!< across all connections, instead of
97 //!< re-using the most recently used
98 //!< connections first.
100 time_t last_checked; //!< Last time we pruned the connection
102 time_t last_spawned; //!< Last time we spawned a connection.
103 time_t last_failed; //!< Last time we tried to spawn a
104 //!< a connection but failed.
105 time_t last_complained;//!< Last time we complained about
106 //!< configuration parameters.
107 time_t last_throttled; //!< Last time we refused to spawn a
108 //!< connection because the last
109 //!< connection failed, or we were
110 //!< already spawning a connection.
111 time_t last_at_max; //!< Last time we hit the maximum number
112 //!< of allowed connections.
114 uint64_t count; //!< Number of connections spawned over
115 //!< the lifetime of the pool.
116 int num; //!< Number of connections in the pool.
117 int active; //!< Number of currently reserved
120 fr_connection_t *head; //!< Start of the connection list.
121 fr_connection_t *tail; //!< End of the connection list.
123 bool spawning; //!< Whether we are currently attempting
124 //!< to spawn a new connection.
126 #ifdef HAVE_PTHREAD_H
127 pthread_mutex_t mutex; //!< Mutex used to keep consistent state
128 //!< when making modifications in
132 CONF_SECTION *cs; //!< Configuration section holding
133 //!< the section of parsed config file
134 //!< that relates to this pool.
135 void *ctx; //!< Pointer to context data that will
136 //!< be passed to callbacks.
138 char *log_prefix; //!< Log prefix to prepend to all log
139 //!< messages created by the connection
142 fr_connection_create_t create; //!< Function used to create new
144 fr_connection_alive_t alive; //!< Function used to check status
146 fr_connection_delete_t delete; //!< Function used to close existing
150 #define LOG_PREFIX "rlm_%s (%s)"
151 #ifndef HAVE_PTHREAD_H
152 #define pthread_mutex_lock(_x)
153 #define pthread_mutex_unlock(_x)
156 static const CONF_PARSER connection_config[] = {
157 { "start", PW_TYPE_INTEGER, offsetof(fr_connection_pool_t, start),
159 { "min", PW_TYPE_INTEGER, offsetof(fr_connection_pool_t, min),
161 { "max", PW_TYPE_INTEGER, offsetof(fr_connection_pool_t, max),
163 { "spare", PW_TYPE_INTEGER, offsetof(fr_connection_pool_t, spare),
165 { "uses", PW_TYPE_INTEGER, offsetof(fr_connection_pool_t, max_uses),
167 { "lifetime", PW_TYPE_INTEGER, offsetof(fr_connection_pool_t, lifetime),
169 { "cleanup_delay", PW_TYPE_INTEGER, offsetof(fr_connection_pool_t, cleanup_delay),
171 { "idle_timeout", PW_TYPE_INTEGER, offsetof(fr_connection_pool_t, idle_timeout),
173 { "spread", PW_TYPE_BOOLEAN, offsetof(fr_connection_pool_t, spread),
175 { NULL, -1, 0, NULL, NULL }
178 /** Removes a connection from the connection list
180 * @note Must be called with the mutex held.
182 * @param[in,out] pool to modify.
183 * @param[in] this Connection to delete.
185 static void fr_connection_unlink(fr_connection_pool_t *pool,
186 fr_connection_t *this)
189 rad_assert(pool->head != this);
190 this->prev->next = this->next;
192 rad_assert(pool->head == this);
193 pool->head = this->next;
196 rad_assert(pool->tail != this);
197 this->next->prev = this->prev;
199 rad_assert(pool->tail == this);
200 pool->tail = this->prev;
203 this->prev = this->next = NULL;
206 /** Adds a connection to the head of the connection list
208 * @note Must be called with the mutex held.
210 * @param[in,out] pool to modify.
211 * @param[in] this Connection to add.
213 static void fr_connection_link_head(fr_connection_pool_t *pool,
214 fr_connection_t *this)
216 rad_assert(pool != NULL);
217 rad_assert(this != NULL);
218 rad_assert(pool->head != this);
219 rad_assert(pool->tail != this);
222 pool->head->prev = this;
225 this->next = pool->head;
229 rad_assert(this->next == NULL);
232 rad_assert(this->next != NULL);
236 /** Adds a connection to the tail of the connection list
238 * @note Must be called with the mutex held.
240 * @param[in,out] pool to modify.
241 * @param[in] this Connection to add.
243 static void fr_connection_link_tail(fr_connection_pool_t *pool,
244 fr_connection_t *this)
246 rad_assert(pool != NULL);
247 rad_assert(this != NULL);
248 rad_assert(pool->head != this);
249 rad_assert(pool->tail != this);
252 pool->tail->next = this;
254 this->prev = pool->tail;
258 rad_assert(this->prev == NULL);
261 rad_assert(this->prev != NULL);
266 /** Spawns a new connection
268 * Spawns a new connection using the create callback, and returns it for
269 * adding to the connection list.
271 * @note Will call the 'open' trigger.
272 * @note Must be called with the mutex free.
274 * @param[in] pool to modify.
275 * @param[in] now Current time.
276 * @return the new connection struct or NULL on error.
278 static fr_connection_t *fr_connection_spawn(fr_connection_pool_t *pool,
281 fr_connection_t *this;
284 rad_assert(pool != NULL);
287 * Prevent all threads from blocking if the resource
288 * were managing connections for appears to be unavailable.
290 if ((pool->num == 0) && pool->spawning) {
294 pthread_mutex_lock(&pool->mutex);
295 rad_assert(pool->num <= pool->max);
297 if ((pool->last_failed == now) || pool->spawning) {
298 bool complain = false;
300 if (pool->last_throttled != now) {
303 pool->last_throttled = now;
306 pthread_mutex_unlock(&pool->mutex);
309 if (pool->spawning) {
310 ERROR("%s: Cannot open new connection, "
311 "connection spawning already in "
312 "progress", pool->log_prefix);
314 ERROR("%s: Last connection failed, "
315 "throttling connection spawn",
323 pool->spawning = true;
326 * Unlock the mutex while we try to open a new
327 * connection. If there are issues with the back-end,
328 * opening a new connection may take a LONG time. In
329 * that case, we want the other connections to continue
332 pthread_mutex_unlock(&pool->mutex);
334 INFO("%s: Opening additional connection (%" PRIu64 ")", pool->log_prefix, pool->count);
336 this = rad_malloc(sizeof(*this));
337 memset(this, 0, sizeof(*this));
340 * This may take a long time, which prevents other
341 * threads from releasing connections. We don't care
342 * about other threads opening new connections, as we
343 * already have no free connections.
345 conn = pool->create(pool->ctx);
347 ERROR("%s: Opening connection failed (%" PRIu64 ")", pool->log_prefix, pool->count);
349 pool->last_failed = now;
351 pool->spawning = false; /* atomic, so no lock is needed */
356 this->connection = conn;
359 * And lock the mutex again while we link the new
360 * connection back into the pool.
362 pthread_mutex_lock(&pool->mutex);
364 this->number = pool->count++;
365 this->last_used = now;
366 fr_connection_link_head(pool, this);
368 pool->spawning = false;
369 pool->last_spawned = time(NULL);
371 pthread_mutex_unlock(&pool->mutex);
373 if (pool->trigger) exec_trigger(NULL, pool->cs, "open", true);
378 /** Add a new connection to the pool
380 * If conn is not NULL will attempt to add that connection handle to the pool.
381 * If conn is NULL will attempt to spawn a new connection using the create
384 * @note Will call the 'open' trigger.
386 * @param[in,out] pool to add connection to.
387 * @param[in] conn to add.
388 * @return 0 if the connection wasn't added else 1.
390 int fr_connection_add(fr_connection_pool_t *pool, void *conn)
392 fr_connection_t *this;
396 pthread_mutex_lock(&pool->mutex);
399 conn = pool->create(pool->ctx);
401 pthread_mutex_unlock(&pool->mutex);
405 INFO("%s: Opening connection successful (%" PRIu64 ")", pool->log_prefix, pool->count);
409 * Too many connections: can't add it.
411 if (pool->num >= pool->max) {
412 pthread_mutex_unlock(&pool->mutex);
416 this = rad_malloc(sizeof(*this));
417 memset(this, 0, sizeof(*this));
419 this->created = time(NULL);
420 this->connection = conn;
422 this->number = pool->count++;
423 this->last_used = time(NULL);
424 fr_connection_link_head(pool, this);
427 pthread_mutex_unlock(&pool->mutex);
429 if (pool->trigger) exec_trigger(NULL, pool->cs, "open", true);
434 /** Close an existing connection.
436 * Removes the connection from the list, calls the delete callback to close
437 * the connection, then frees memory allocated to the connection.
439 * @note Will call the 'close' trigger.
440 * @note Must be called with the mutex held.
442 * @param[in,out] pool to modify.
443 * @param[in,out] this Connection to delete.
446 static void fr_connection_close(fr_connection_pool_t *pool,
447 fr_connection_t *this)
449 if (pool->trigger) exec_trigger(NULL, pool->cs, "close", true);
451 rad_assert(this->in_use == false);
453 fr_connection_unlink(pool, this);
454 pool->delete(pool->ctx, this->connection);
455 rad_assert(pool->num > 0);
460 /** Find a connection handle in the connection list
462 * Walks over the list of connections searching for a specified connection
463 * handle and returns the first connection that contains that pointer.
465 * @note Will lock mutex and only release mutex if connection handle
466 * is not found, so will usually return will mutex held.
467 * @note Must be called with the mutex free.
469 * @param[in] pool to search in.
470 * @param[in] conn handle to search for.
471 * @return the connection containing the specified handle, or NULL if non is
474 static fr_connection_t *fr_connection_find(fr_connection_pool_t *pool, void *conn)
476 fr_connection_t *this;
478 if (!pool || !conn) return NULL;
480 pthread_mutex_lock(&pool->mutex);
483 * FIXME: This loop could be avoided if we passed a 'void
484 * **connection' instead. We could use "offsetof" in
485 * order to find top of the parent structure.
487 for (this = pool->head; this != NULL; this = this->next) {
488 if (this->connection == conn) return this;
491 pthread_mutex_unlock(&pool->mutex);
495 /** Delete a connection from the connection pool.
497 * Resolves the connection handle to a connection, then (if found)
498 * closes, unlinks and frees that connection.
500 * @note Must be called with the mutex free.
502 * @param[in,out] pool Connection pool to modify.
503 * @param[in] conn to delete.
504 * @return 0 if the connection could not be found, else 1.
506 int fr_connection_del(fr_connection_pool_t *pool, void *conn)
508 fr_connection_t *this;
510 this = fr_connection_find(pool, conn);
514 * If it's in use, release it.
517 rad_assert(this->in_use == true);
518 this->in_use = false;
520 rad_assert(pool->active > 0);
524 INFO("%s: Deleting connection (%" PRIu64 ")", pool->log_prefix, this->number);
526 fr_connection_close(pool, this);
527 fr_connection_pool_check(pool);
531 /** Delete a connection pool
533 * Closes, unlinks and frees all connections in the connection pool, then frees
534 * all memory used by the connection pool.
536 * @note Will call the 'stop' trigger.
537 * @note Must be called with the mutex free.
539 * @param[in,out] pool to delete.
541 void fr_connection_pool_delete(fr_connection_pool_t *pool)
543 fr_connection_t *this, *next;
547 DEBUG("%s: Removing connection pool", pool->log_prefix);
549 pthread_mutex_lock(&pool->mutex);
551 for (this = pool->head; this != NULL; this = next) {
554 INFO("%s: Closing connection (%" PRIu64 ")", pool->log_prefix, this->number);
556 fr_connection_close(pool, this);
559 if (pool->trigger) exec_trigger(NULL, pool->cs, "stop", true);
561 rad_assert(pool->head == NULL);
562 rad_assert(pool->tail == NULL);
563 rad_assert(pool->num == 0);
565 free(pool->log_prefix);
569 /** Create a new connection pool
571 * Allocates structures used by the connection pool, initialises the various
572 * configuration options and counters, and sets the callback functions.
574 * Will also spawn the number of connections specified by the 'start'
575 * configuration options.
577 * @note Will call the 'start' trigger.
579 * @param[in] parent configuration section containing a 'pool' subsection.
580 * @param[in] ctx pointer to pass to callbacks.
581 * @param[in] c Callback to create new connections.
582 * @param[in] a Callback to check the status of connections.
583 * @param[in] d Callback to delete connections.
584 * @param[in] prefix to prepend to all log message, if NULL will create prefix
585 * from parent conf section names.
586 * @return A new connection pool or NULL on error.
588 fr_connection_pool_t *fr_connection_pool_init(CONF_SECTION *parent,
590 fr_connection_create_t c,
591 fr_connection_alive_t a,
592 fr_connection_delete_t d,
596 fr_connection_pool_t *pool;
597 fr_connection_t *this;
598 CONF_SECTION *modules;
600 char const *cs_name1, *cs_name2;
601 time_t now = time(NULL);
603 if (!parent || !ctx || !c || !d) return NULL;
605 cs = cf_section_sub_find(parent, "pool");
606 if (!cs) cs = cf_section_sub_find(parent, "limit");
608 pool = rad_malloc(sizeof(*pool));
609 memset(pool, 0, sizeof(*pool));
617 pool->head = pool->tail = NULL;
619 #ifdef HAVE_PTHREAD_H
620 pthread_mutex_init(&pool->mutex, NULL);
624 modules = cf_item_parent(cf_sectiontoitem(parent));
626 cs_name1 = cf_section_name1(modules);
627 if (cs_name1 && (strcmp(cs_name1, "modules") == 0)) {
628 cs_name1 = cf_section_name1(parent);
629 cs_name2 = cf_section_name2(parent);
634 lp_len = (sizeof(LOG_PREFIX) - 4) + strlen(cs_name1) + strlen(cs_name2);
635 pool->log_prefix = rad_malloc(lp_len);
636 snprintf(pool->log_prefix, lp_len, LOG_PREFIX, cs_name1,
639 } else { /* not a module configuration */
640 cs_name1 = cf_section_name1(parent);
642 pool->log_prefix = strdup(cs_name1);
645 pool->log_prefix = strdup(prefix);
648 DEBUG("%s: Initialising connection pool", pool->log_prefix);
651 if (cf_section_parse(cs, pool, connection_config) < 0) {
655 if (cf_section_sub_find(cs, "trigger")) pool->trigger = true;
661 pool->cleanup_delay = 5;
662 pool->idle_timeout = 60;
668 if (pool->max == 0) {
669 cf_log_err_cs(cs, "Cannot set 'max' to zero");
672 if (pool->min > pool->max) {
673 cf_log_err_cs(cs, "Cannot set 'min' to more than 'max'");
677 if (pool->max > 1024) pool->max = 1024;
678 if (pool->start > pool->max) pool->start = pool->max;
679 if (pool->spare > (pool->max - pool->min)) {
680 pool->spare = pool->max - pool->min;
682 if ((pool->lifetime > 0) && (pool->idle_timeout > pool->lifetime)) {
683 pool->idle_timeout = 0;
687 * Create all of the connections, unless the admin says
690 for (i = 0; i < pool->start; i++) {
691 this = fr_connection_spawn(pool, now);
694 fr_connection_pool_delete(pool);
699 if (pool->trigger) exec_trigger(NULL, pool->cs, "start", true);
705 /** Check whether a connection needs to be removed from the pool
707 * Will verify that the connection is within idle_timeout, max_uses, and
708 * lifetime values. If it is not, the connection will be closed.
710 * @note Will only close connections not in use.
711 * @note Must be called with the mutex held.
713 * @param[in,out] pool to modify.
714 * @param[in,out] this Connection to manage.
715 * @param[in] now Current time.
716 * @return 0 if the connection was closed, otherwise 1.
718 static int fr_connection_manage(fr_connection_pool_t *pool,
719 fr_connection_t *this,
722 rad_assert(pool != NULL);
723 rad_assert(this != NULL);
726 * Don't terminated in-use connections
728 if (this->in_use) return 1;
730 if ((pool->max_uses > 0) &&
731 (this->num_uses >= pool->max_uses)) {
732 DEBUG("%s: Closing expired connection (%" PRIu64 "): Hit max_uses limit", pool->log_prefix,
735 if ((pool->num <= pool->min) &&
736 (pool->last_complained < now)) {
737 WARN("%s: You probably need to lower \"min\"", pool->log_prefix);
739 pool->last_complained = now;
741 fr_connection_close(pool, this);
745 if ((pool->lifetime > 0) &&
746 ((this->created + pool->lifetime) < now)) {
747 DEBUG("%s: Closing expired connection (%" PRIu64 ")", pool->log_prefix, this->number);
751 if ((pool->idle_timeout > 0) &&
752 ((this->last_used + pool->idle_timeout) < now)) {
753 INFO("%s: Closing connection (%" PRIu64 "): Hit idle_timeout, was idle for %u seconds",
754 pool->log_prefix, this->number, (int) (now - this->last_used));
762 /** Check whether any connections needs to be removed from the pool
764 * Maintains the number of connections in the pool as per the configuration
765 * parameters for the connection pool.
767 * @note Will only run checks the first time it's called in a given second,
768 * to throttle connection spawning/closing.
769 * @note Will only close connections not in use.
770 * @note Must be called with the mutex held, will release mutex before
773 * @param[in,out] pool to manage.
776 static int fr_connection_pool_check(fr_connection_pool_t *pool)
779 time_t now = time(NULL);
780 fr_connection_t *this, *next;
782 if (pool->last_checked == now) {
783 pthread_mutex_unlock(&pool->mutex);
787 spare = pool->num - pool->active;
789 if ((pool->num < pool->max) && (spare < pool->spare)) {
790 spawn = pool->spare - spare;
791 if ((spawn + pool->num) > pool->max) {
792 spawn = pool->max - pool->num;
794 if (pool->spawning) spawn = 0;
797 pthread_mutex_unlock(&pool->mutex);
798 fr_connection_spawn(pool, now); /* ignore return code */
799 pthread_mutex_lock(&pool->mutex);
804 * We haven't spawned connections in a while, and there
805 * are too many spare ones. Close the one which has been
806 * idle for the longest.
808 if ((now >= (pool->last_spawned + pool->cleanup_delay)) &&
809 (spare > pool->spare)) {
810 fr_connection_t *idle;
813 for (this = pool->tail; this != NULL; this = this->prev) {
814 if (this->in_use) continue;
817 (this->last_used < idle->last_used)) {
822 rad_assert(idle != NULL);
824 INFO("%s: Closing connection (%" PRIu64 "): Too many free connections (%d > %d)", pool->log_prefix,
825 idle->number, spare, pool->spare);
826 fr_connection_close(pool, idle);
830 * Pass over all of the connections in the pool, limiting
831 * lifetime, idle time, max requests, etc.
833 for (this = pool->head; this != NULL; this = next) {
835 fr_connection_manage(pool, this, now);
838 pool->last_checked = now;
839 pthread_mutex_unlock(&pool->mutex);
844 /** Trigger connection check for a given connection or all connections
846 * If conn is not NULL then we call fr_connection_manage on the connection.
847 * If conn is NULL we call fr_connection_pool_check on the pool.
849 * @note Only connections that are not in use will be closed.
851 * @see fr_connection_manage
852 * @see fr_connection_pool_check
853 * @param[in,out] pool to manage.
854 * @param[in,out] conn to check.
855 * @return 0 if the connection was closed, else 1.
857 int fr_connection_check(fr_connection_pool_t *pool, void *conn)
859 fr_connection_t *this;
866 pthread_mutex_lock(&pool->mutex);
868 if (!conn) return fr_connection_pool_check(pool);
870 for (this = pool->head; this != NULL; this = this->next) {
871 if (this->connection == conn) {
872 ret = fr_connection_manage(pool, conn, now);
877 pthread_mutex_unlock(&pool->mutex);
882 /** Reserve a connection in the connection pool
884 * Will attempt to find an unused connection in the connection pool, if one is
885 * found, will mark it as in in use increment the number of active connections
886 * and return the connection handle.
888 * If no free connections are found will attempt to spawn a new one, conditional
889 * on a connection spawning not already being in progress, and not being at the
890 * 'max' connection limit.
892 * @note fr_connection_release must be called once the caller has finished
893 * using the connection.
895 * @see fr_connection_release
896 * @param[in,out] pool to reserve the connection from.
897 * @return a pointer to the connection handle, or NULL on error.
899 void *fr_connection_get(fr_connection_pool_t *pool)
902 fr_connection_t *this, *next;
904 if (!pool) return NULL;
906 pthread_mutex_lock(&pool->mutex);
909 for (this = pool->head; this != NULL; this = next) {
912 if (!this->in_use) goto do_return;
915 if (pool->num == pool->max) {
916 bool complain = false;
919 * Rate-limit complaints.
921 if (pool->last_at_max != now) {
923 pool->last_at_max = now;
926 pthread_mutex_unlock(&pool->mutex);
929 ERROR("%s: No connections available and at max connection limit", pool->log_prefix);
935 pthread_mutex_unlock(&pool->mutex);
936 this = fr_connection_spawn(pool, now);
937 if (!this) return NULL;
938 pthread_mutex_lock(&pool->mutex);
943 this->last_used = now;
946 pthread_mutex_unlock(&pool->mutex);
948 DEBUG("%s: Reserved connection (%" PRIu64 ")", pool->log_prefix, this->number);
950 return this->connection;
953 /** Release a connection
955 * Will mark a connection as unused and decrement the number of active
958 * @see fr_connection_get
959 * @param[in,out] pool to release the connection in.
960 * @param[in,out] conn to release.
962 void fr_connection_release(fr_connection_pool_t *pool, void *conn)
964 fr_connection_t *this;
966 this = fr_connection_find(pool, conn);
969 rad_assert(this->in_use == true);
970 this->in_use = false;
973 * Determines whether the last used connection gets
978 * Put it at the tail of the list, so
979 * that it will get re-used last.
981 if (this != pool->tail) {
982 fr_connection_unlink(pool, this);
983 fr_connection_link_tail(pool, this);
987 * Put it at the head of the list, so
988 * that it will get re-used quickly.
990 if (this != pool->head) {
991 fr_connection_unlink(pool, this);
992 fr_connection_link_head(pool, this);
996 rad_assert(pool->active > 0);
999 DEBUG("%s: Released connection (%" PRIu64 ")", pool->log_prefix, this->number);
1002 * We mirror the "spawn on get" functionality by having
1003 * "delete on release". If there are too many spare
1004 * connections, go manage the pool && clean some up.
1006 fr_connection_pool_check(pool);
1009 /** Reconnect a suspected inviable connection
1011 * This should be called by the module if it suspects that a connection is
1012 * not viable (e.g. the server has closed it).
1014 * Will attempt to create a new connection handle using the create callback,
1015 * and if this is successful the new handle will be assigned to the existing
1018 * If this is not successful, the connection will be removed from the pool.
1020 * When implementing a module that uses the connection pool API, it is advisable
1021 * to pass a pointer to the pointer to the handle (void **conn)
1022 * to all functions which may call reconnect. This is so that if a new handle
1023 * is created and returned, the handle pointer can be updated up the callstack,
1024 * and a function higher up the stack doesn't attempt to use a now invalid
1025 * connection handle.
1027 * @warning After calling reconnect the caller *MUST NOT* attempt to use
1028 * the old handle in any other operations, as its memory will have been freed.
1030 * @see fr_connection_get
1031 * @param[in,out] pool to reconnect the connection in.
1032 * @param[in,out] conn to reconnect.
1033 * @return ew connection handle if successful else NULL.
1035 void *fr_connection_reconnect(fr_connection_pool_t *pool, void *conn)
1038 fr_connection_t *this;
1039 uint64_t conn_number;
1041 if (!pool || !conn) return NULL;
1043 this = fr_connection_find(pool, conn);
1044 if (!this) return NULL;
1047 * The pool is now locked.
1049 conn_number = this->number;
1051 DEBUG("%s: Reconnecting (%" PRIu64 ")", pool->log_prefix, conn_number);
1053 new_conn = pool->create(pool->ctx);
1055 time_t now = time(NULL);
1057 if (pool->last_complained == now) {
1060 pool->last_complained = now;
1064 * The caller has said the connection is bad. So
1065 * if it's still in use, mark it as unused, and
1066 * close it. If it's not in use, we just try to
1067 * get a new connection.
1070 this->in_use = false;
1072 rad_assert(pool->active > 0);
1075 fr_connection_close(pool, this);
1079 * We failed to create a new socket.
1080 * Try to grab an existing one.
1082 pthread_mutex_unlock(&pool->mutex);
1083 new_conn = fr_connection_get(pool);
1084 if (new_conn) return new_conn;
1086 if (!now) return NULL;
1088 ERROR("%s: Failed to reconnect (%" PRIu64 "), and no other connections available", pool->log_prefix,
1094 pool->delete(pool->ctx, conn);
1095 this->connection = new_conn;
1096 pthread_mutex_unlock(&pool->mutex);