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; //!< Interval between pruning
80 //!< excess connections.
81 uint64_t max_uses; //!< Maximum number of times a
82 //!< connection can be used before being
84 int lifetime; //!< How long a connection can be open
85 //!< before being closed (irrespective
86 //!< of whether it's idle or not).
87 int idle_timeout; //!< How long a connection can be idle
88 //!< before being closed.
90 bool trigger; //!< If true execute connection triggers
91 //!< associated with the connection
94 bool spread; //!< If true requests will be spread
95 //!< across all connections, instead of
96 //!< re-using the most recently used
97 //!< connections first.
99 time_t last_checked; //!< Last time we pruned the connection
101 time_t last_spawned; //!< Last time we spawned a connection.
102 time_t last_failed; //!< Last time we tried to spawn a
103 //!< a connection but failed.
104 time_t last_complained;//!< Last time we complained about
105 //!< configuration parameters.
106 time_t last_throttled; //!< Last time we refused to spawn a
107 //!< connection because the last
108 //!< connection failed, or we were
109 //!< already spawning a connection.
110 time_t last_at_max; //!< Last time we hit the maximum number
111 //!< of allowed connections.
113 uint64_t count; //!< Number of connections spawned over
114 //!< the lifetime of the pool.
115 int num; //!< Number of connections in the pool.
116 int active; //!< Number of currently reserved
119 fr_connection_t *head; //!< Start of the connection list.
120 fr_connection_t *tail; //!< End of the connection list.
122 bool spawning; //!< Whether we are currently attempting
123 //!< to spawn a new connection.
125 #ifdef HAVE_PTHREAD_H
126 pthread_mutex_t mutex; //!< Mutex used to keep consistent state
127 //!< when making modifications in
131 CONF_SECTION *cs; //!< Configuration section holding
132 //!< the section of parsed config file
133 //!< that relates to this pool.
134 void *ctx; //!< Pointer to context data that will
135 //!< be passed to callbacks.
137 char *log_prefix; //!< Log prefix to prepend to all log
138 //!< messages created by the connection
141 fr_connection_create_t create; //!< Function used to create new
143 fr_connection_alive_t alive; //!< Function used to check status
145 fr_connection_delete_t delete; //!< Function used to close existing
149 #define LOG_PREFIX "rlm_%s (%s)"
150 #ifndef HAVE_PTHREAD_H
151 #define pthread_mutex_lock(_x)
152 #define pthread_mutex_unlock(_x)
155 static const CONF_PARSER connection_config[] = {
156 { "start", PW_TYPE_INTEGER, offsetof(fr_connection_pool_t, start),
158 { "min", PW_TYPE_INTEGER, offsetof(fr_connection_pool_t, min),
160 { "max", PW_TYPE_INTEGER, offsetof(fr_connection_pool_t, max),
162 { "spare", PW_TYPE_INTEGER, offsetof(fr_connection_pool_t, spare),
164 { "uses", PW_TYPE_INTEGER, offsetof(fr_connection_pool_t, max_uses),
166 { "lifetime", PW_TYPE_INTEGER, offsetof(fr_connection_pool_t, lifetime),
168 { "cleanup_delay", PW_TYPE_INTEGER, offsetof(fr_connection_pool_t, cleanup_delay),
170 { "idle_timeout", PW_TYPE_INTEGER, offsetof(fr_connection_pool_t, idle_timeout),
172 { "spread", PW_TYPE_BOOLEAN, offsetof(fr_connection_pool_t, spread),
174 { NULL, -1, 0, NULL, NULL }
177 /** Removes a connection from the connection list
179 * @note Must be called with the mutex held.
181 * @param[in,out] pool to modify.
182 * @param[in] this Connection to delete.
184 static void fr_connection_unlink(fr_connection_pool_t *pool,
185 fr_connection_t *this)
188 rad_assert(pool->head != this);
189 this->prev->next = this->next;
191 rad_assert(pool->head == this);
192 pool->head = this->next;
195 rad_assert(pool->tail != this);
196 this->next->prev = this->prev;
198 rad_assert(pool->tail == this);
199 pool->tail = this->prev;
202 this->prev = this->next = NULL;
205 /** Adds a connection to the head of the connection list
207 * @note Must be called with the mutex held.
209 * @param[in,out] pool to modify.
210 * @param[in] this Connection to add.
212 static void fr_connection_link_head(fr_connection_pool_t *pool,
213 fr_connection_t *this)
215 rad_assert(pool != NULL);
216 rad_assert(this != NULL);
217 rad_assert(pool->head != this);
218 rad_assert(pool->tail != this);
221 pool->head->prev = this;
224 this->next = pool->head;
228 rad_assert(this->next == NULL);
231 rad_assert(this->next != NULL);
235 /** Adds a connection to the tail of the connection list
237 * @note Must be called with the mutex held.
239 * @param[in,out] pool to modify.
240 * @param[in] this Connection to add.
242 static void fr_connection_link_tail(fr_connection_pool_t *pool,
243 fr_connection_t *this)
245 rad_assert(pool != NULL);
246 rad_assert(this != NULL);
247 rad_assert(pool->head != this);
248 rad_assert(pool->tail != this);
251 pool->tail->next = this;
253 this->prev = pool->tail;
257 rad_assert(this->prev == NULL);
260 rad_assert(this->prev != NULL);
265 /** Spawns a new connection
267 * Spawns a new connection using the create callback, and returns it for
268 * adding to the connection list.
270 * @note Will call the 'open' trigger.
271 * @note Must be called with the mutex free.
273 * @param[in] pool to modify.
274 * @param[in] now Current time.
275 * @return the new connection struct or NULL on error.
277 static fr_connection_t *fr_connection_spawn(fr_connection_pool_t *pool,
280 fr_connection_t *this;
283 rad_assert(pool != NULL);
286 * Prevent all threads from blocking if the resource
287 * were managing connections for appears to be unavailable.
289 if ((pool->num == 0) && pool->spawning) {
293 pthread_mutex_lock(&pool->mutex);
294 rad_assert(pool->num <= pool->max);
296 if ((pool->last_failed == now) || pool->spawning) {
297 bool complain = false;
299 if (pool->last_throttled != now) {
302 pool->last_throttled = now;
305 pthread_mutex_unlock(&pool->mutex);
308 if (pool->spawning) {
309 ERROR("%s: Cannot open new connection, "
310 "connection spawning already in "
311 "progress", pool->log_prefix);
313 ERROR("%s: Last connection failed, "
314 "throttling connection spawn",
322 pool->spawning = true;
325 * Unlock the mutex while we try to open a new
326 * connection. If there are issues with the back-end,
327 * opening a new connection may take a LONG time. In
328 * that case, we want the other connections to continue
331 pthread_mutex_unlock(&pool->mutex);
333 INFO("%s: Opening additional connection (%" PRIu64 ")", pool->log_prefix, pool->count);
335 this = rad_malloc(sizeof(*this));
336 memset(this, 0, sizeof(*this));
339 * This may take a long time, which prevents other
340 * threads from releasing connections. We don't care
341 * about other threads opening new connections, as we
342 * already have no free connections.
344 conn = pool->create(pool->ctx);
346 ERROR("%s: Opening connection failed (%" PRIu64 ")", pool->log_prefix, pool->count);
348 pool->last_failed = now;
350 pool->spawning = false; /* atomic, so no lock is needed */
355 this->connection = conn;
358 * And lock the mutex again while we link the new
359 * connection back into the pool.
361 pthread_mutex_lock(&pool->mutex);
363 this->number = pool->count++;
364 this->last_used = now;
365 fr_connection_link_head(pool, this);
367 pool->spawning = false;
368 pool->last_spawned = time(NULL);
370 pthread_mutex_unlock(&pool->mutex);
372 if (pool->trigger) exec_trigger(NULL, pool->cs, "open", true);
377 /** Add a new connection to the pool
379 * If conn is not NULL will attempt to add that connection handle to the pool.
380 * If conn is NULL will attempt to spawn a new connection using the create
383 * @note Will call the 'open' trigger.
385 * @param[in,out] pool to add connection to.
386 * @param[in] conn to add.
387 * @return 0 if the connection wasn't added else 1.
389 int fr_connection_add(fr_connection_pool_t *pool, void *conn)
391 fr_connection_t *this;
395 pthread_mutex_lock(&pool->mutex);
398 conn = pool->create(pool->ctx);
400 pthread_mutex_unlock(&pool->mutex);
404 INFO("%s: Opening connection successful (%" PRIu64 ")", pool->log_prefix, pool->count);
408 * Too many connections: can't add it.
410 if (pool->num >= pool->max) {
411 pthread_mutex_unlock(&pool->mutex);
415 this = rad_malloc(sizeof(*this));
416 memset(this, 0, sizeof(*this));
418 this->created = time(NULL);
419 this->connection = conn;
421 this->number = pool->count++;
422 this->last_used = time(NULL);
423 fr_connection_link_head(pool, this);
426 pthread_mutex_unlock(&pool->mutex);
428 if (pool->trigger) exec_trigger(NULL, pool->cs, "open", true);
433 /** Close an existing connection.
435 * Removes the connection from the list, calls the delete callback to close
436 * the connection, then frees memory allocated to the connection.
438 * @note Will call the 'close' trigger.
439 * @note Must be called with the mutex held.
441 * @param[in,out] pool to modify.
442 * @param[in,out] this Connection to delete.
445 static void fr_connection_close(fr_connection_pool_t *pool,
446 fr_connection_t *this)
448 if (pool->trigger) exec_trigger(NULL, pool->cs, "close", true);
450 rad_assert(this->in_use == false);
452 fr_connection_unlink(pool, this);
453 pool->delete(pool->ctx, this->connection);
454 rad_assert(pool->num > 0);
459 /** Find a connection handle in the connection list
461 * Walks over the list of connections searching for a specified connection
462 * handle and returns the first connection that contains that pointer.
464 * @note Will lock mutex and only release mutex if connection handle
465 * is not found, so will usually return will mutex held.
466 * @note Must be called with the mutex free.
468 * @param[in] pool to search in.
469 * @param[in] conn handle to search for.
470 * @return the connection containing the specified handle, or NULL if non is
473 static fr_connection_t *fr_connection_find(fr_connection_pool_t *pool, void *conn)
475 fr_connection_t *this;
477 if (!pool || !conn) return NULL;
479 pthread_mutex_lock(&pool->mutex);
482 * FIXME: This loop could be avoided if we passed a 'void
483 * **connection' instead. We could use "offsetof" in
484 * order to find top of the parent structure.
486 for (this = pool->head; this != NULL; this = this->next) {
487 if (this->connection == conn) return this;
490 pthread_mutex_unlock(&pool->mutex);
494 /** Delete a connection from the connection pool.
496 * Resolves the connection handle to a connection, then (if found)
497 * closes, unlinks and frees that connection.
499 * @note Must be called with the mutex free.
501 * @param[in,out] pool Connection pool to modify.
502 * @param[in] conn to delete.
503 * @return 0 if the connection could not be found, else 1.
505 int fr_connection_del(fr_connection_pool_t *pool, void *conn)
507 fr_connection_t *this;
509 this = fr_connection_find(pool, conn);
513 * If it's in use, release it.
516 rad_assert(this->in_use == true);
517 this->in_use = false;
519 rad_assert(pool->active > 0);
523 INFO("%s: Deleting connection (%" PRIu64 ")", pool->log_prefix, this->number);
525 fr_connection_close(pool, this);
526 fr_connection_pool_check(pool);
530 /** Delete a connection pool
532 * Closes, unlinks and frees all connections in the connection pool, then frees
533 * all memory used by the connection pool.
535 * @note Will call the 'stop' trigger.
536 * @note Must be called with the mutex free.
538 * @param[in,out] pool to delete.
540 void fr_connection_pool_delete(fr_connection_pool_t *pool)
542 fr_connection_t *this, *next;
546 DEBUG("%s: Removing connection pool", pool->log_prefix);
548 pthread_mutex_lock(&pool->mutex);
550 for (this = pool->head; this != NULL; this = next) {
553 INFO("%s: Closing connection (%" PRIu64 ")", pool->log_prefix, this->number);
555 fr_connection_close(pool, this);
558 if (pool->trigger) exec_trigger(NULL, pool->cs, "stop", true);
560 rad_assert(pool->head == NULL);
561 rad_assert(pool->tail == NULL);
562 rad_assert(pool->num == 0);
564 free(pool->log_prefix);
568 /** Create a new connection pool
570 * Allocates structures used by the connection pool, initialises the various
571 * configuration options and counters, and sets the callback functions.
573 * Will also spawn the number of connections specified by the 'start'
574 * configuration options.
576 * @note Will call the 'start' trigger.
578 * @param[in] parent configuration section containing a 'pool' subsection.
579 * @param[in] ctx pointer to pass to callbacks.
580 * @param[in] c Callback to create new connections.
581 * @param[in] a Callback to check the status of connections.
582 * @param[in] d Callback to delete connections.
583 * @param[in] prefix to prepend to all log message, if NULL will create prefix
584 * from parent conf section names.
585 * @return A new connection pool or NULL on error.
587 fr_connection_pool_t *fr_connection_pool_init(CONF_SECTION *parent,
589 fr_connection_create_t c,
590 fr_connection_alive_t a,
591 fr_connection_delete_t d,
595 fr_connection_pool_t *pool;
596 fr_connection_t *this;
597 CONF_SECTION *modules;
599 char const *cs_name1, *cs_name2;
600 time_t now = time(NULL);
602 if (!parent || !ctx || !c || !d) return NULL;
604 cs = cf_section_sub_find(parent, "pool");
605 if (!cs) cs = cf_section_sub_find(parent, "limit");
607 pool = rad_malloc(sizeof(*pool));
608 memset(pool, 0, sizeof(*pool));
616 pool->head = pool->tail = NULL;
618 #ifdef HAVE_PTHREAD_H
619 pthread_mutex_init(&pool->mutex, NULL);
623 modules = cf_item_parent(cf_sectiontoitem(parent));
625 cs_name1 = cf_section_name1(modules);
626 if (cs_name1 && (strcmp(cs_name1, "modules") == 0)) {
627 cs_name1 = cf_section_name1(parent);
628 cs_name2 = cf_section_name2(parent);
633 lp_len = (sizeof(LOG_PREFIX) - 4) + strlen(cs_name1) + strlen(cs_name2);
634 pool->log_prefix = rad_malloc(lp_len);
635 snprintf(pool->log_prefix, lp_len, LOG_PREFIX, cs_name1,
638 } else { /* not a module configuration */
639 cs_name1 = cf_section_name1(parent);
641 pool->log_prefix = strdup(cs_name1);
644 pool->log_prefix = strdup(prefix);
647 DEBUG("%s: Initialising connection pool", pool->log_prefix);
650 if (cf_section_parse(cs, pool, connection_config) < 0) {
654 if (cf_section_sub_find(cs, "trigger")) pool->trigger = true;
660 pool->cleanup_delay = 5;
661 pool->idle_timeout = 60;
667 if (pool->max == 0) {
668 cf_log_err_cs(cs, "Cannot set 'max' to zero");
671 if (pool->min > pool->max) {
672 cf_log_err_cs(cs, "Cannot set 'min' to more than 'max'");
676 if (pool->max > 1024) pool->max = 1024;
677 if (pool->start > pool->max) pool->start = pool->max;
678 if (pool->spare > (pool->max - pool->min)) {
679 pool->spare = pool->max - pool->min;
681 if ((pool->lifetime > 0) && (pool->idle_timeout > pool->lifetime)) {
682 pool->idle_timeout = 0;
686 * Create all of the connections, unless the admin says
689 for (i = 0; i < pool->start; i++) {
690 this = fr_connection_spawn(pool, now);
693 fr_connection_pool_delete(pool);
698 if (pool->trigger) exec_trigger(NULL, pool->cs, "start", true);
704 /** Check whether a connection needs to be removed from the pool
706 * Will verify that the connection is within idle_timeout, max_uses, and
707 * lifetime values. If it is not, the connection will be closed.
709 * @note Will only close connections not in use.
710 * @note Must be called with the mutex held.
712 * @param[in,out] pool to modify.
713 * @param[in,out] this Connection to manage.
714 * @param[in] now Current time.
715 * @return 0 if the connection was closed, otherwise 1.
717 static int fr_connection_manage(fr_connection_pool_t *pool,
718 fr_connection_t *this,
721 rad_assert(pool != NULL);
722 rad_assert(this != NULL);
725 * Don't terminated in-use connections
727 if (this->in_use) return 1;
729 if ((pool->max_uses > 0) &&
730 (this->num_uses >= pool->max_uses)) {
731 DEBUG("%s: Closing expired connection (%" PRIu64 "): Hit max_uses limit", pool->log_prefix,
734 if ((pool->num <= pool->min) &&
735 (pool->last_complained < now)) {
736 WARN("%s: You probably need to lower \"min\"", pool->log_prefix);
738 pool->last_complained = now;
740 fr_connection_close(pool, this);
744 if ((pool->lifetime > 0) &&
745 ((this->created + pool->lifetime) < now)) {
746 DEBUG("%s: Closing expired connection (%" PRIu64 ")", pool->log_prefix, this->number);
750 if ((pool->idle_timeout > 0) &&
751 ((this->last_used + pool->idle_timeout) < now)) {
752 INFO("%s: Closing connection (%" PRIu64 "): Hit idle_timeout, was idle for %u seconds",
753 pool->log_prefix, this->number, (int) (now - this->last_used));
761 /** Check whether any connections needs to be removed from the pool
763 * Maintains the number of connections in the pool as per the configuration
764 * parameters for the connection pool.
766 * @note Will only run checks the first time it's called in a given second,
767 * to throttle connection spawning/closing.
768 * @note Will only close connections not in use.
769 * @note Must be called with the mutex held, will release mutex before
772 * @param[in,out] pool to manage.
775 static int fr_connection_pool_check(fr_connection_pool_t *pool)
778 time_t now = time(NULL);
779 fr_connection_t *this, *next;
781 if (pool->last_checked == now) {
782 pthread_mutex_unlock(&pool->mutex);
786 spare = pool->num - pool->active;
788 if ((pool->num < pool->max) && (spare < pool->spare)) {
789 spawn = pool->spare - spare;
790 if ((spawn + pool->num) > pool->max) {
791 spawn = pool->max - pool->num;
793 if (pool->spawning) spawn = 0;
796 pthread_mutex_unlock(&pool->mutex);
797 fr_connection_spawn(pool, now); /* ignore return code */
798 pthread_mutex_lock(&pool->mutex);
803 * We haven't spawned connections in a while, and there
804 * are too many spare ones. Close the one which has been
805 * idle for the longest.
807 if ((now >= (pool->last_spawned + pool->cleanup_delay)) &&
808 (spare > pool->spare)) {
809 fr_connection_t *idle;
812 for (this = pool->tail; this != NULL; this = this->prev) {
813 if (this->in_use) continue;
816 (this->last_used < idle->last_used)) {
821 rad_assert(idle != NULL);
823 INFO("%s: Closing connection (%" PRIu64 "): Too many free connections (%d > %d)", pool->log_prefix,
824 idle->number, spare, pool->spare);
825 fr_connection_close(pool, idle);
829 * Pass over all of the connections in the pool, limiting
830 * lifetime, idle time, max requests, etc.
832 for (this = pool->head; this != NULL; this = next) {
834 fr_connection_manage(pool, this, now);
837 pool->last_checked = now;
838 pthread_mutex_unlock(&pool->mutex);
843 /** Trigger connection check for a given connection or all connections
845 * If conn is not NULL then we call fr_connection_manage on the connection.
846 * If conn is NULL we call fr_connection_pool_check on the pool.
848 * @note Only connections that are not in use will be closed.
850 * @see fr_connection_manage
851 * @see fr_connection_pool_check
852 * @param[in,out] pool to manage.
853 * @param[in,out] conn to check.
854 * @return 0 if the connection was closed, else 1.
856 int fr_connection_check(fr_connection_pool_t *pool, void *conn)
858 fr_connection_t *this;
865 pthread_mutex_lock(&pool->mutex);
867 if (!conn) return fr_connection_pool_check(pool);
869 for (this = pool->head; this != NULL; this = this->next) {
870 if (this->connection == conn) {
871 ret = fr_connection_manage(pool, conn, now);
876 pthread_mutex_unlock(&pool->mutex);
881 /** Reserve a connection in the connection pool
883 * Will attempt to find an unused connection in the connection pool, if one is
884 * found, will mark it as in in use increment the number of active connections
885 * and return the connection handle.
887 * If no free connections are found will attempt to spawn a new one, conditional
888 * on a connection spawning not already being in progress, and not being at the
889 * 'max' connection limit.
891 * @note fr_connection_release must be called once the caller has finished
892 * using the connection.
894 * @see fr_connection_release
895 * @param[in,out] pool to reserve the connection from.
896 * @return a pointer to the connection handle, or NULL on error.
898 void *fr_connection_get(fr_connection_pool_t *pool)
901 fr_connection_t *this, *next;
903 if (!pool) return NULL;
905 pthread_mutex_lock(&pool->mutex);
908 for (this = pool->head; this != NULL; this = next) {
911 if (!this->in_use) goto do_return;
914 if (pool->num == pool->max) {
915 bool complain = false;
918 * Rate-limit complaints.
920 if (pool->last_at_max != now) {
922 pool->last_at_max = now;
925 pthread_mutex_unlock(&pool->mutex);
928 ERROR("%s: No connections available and at max connection limit", pool->log_prefix);
934 pthread_mutex_unlock(&pool->mutex);
935 this = fr_connection_spawn(pool, now);
936 if (!this) return NULL;
937 pthread_mutex_lock(&pool->mutex);
942 this->last_used = now;
945 pthread_mutex_unlock(&pool->mutex);
947 DEBUG("%s: Reserved connection (%" PRIu64 ")", pool->log_prefix, this->number);
949 return this->connection;
952 /** Release a connection
954 * Will mark a connection as unused and decrement the number of active
957 * @see fr_connection_get
958 * @param[in,out] pool to release the connection in.
959 * @param[in,out] conn to release.
961 void fr_connection_release(fr_connection_pool_t *pool, void *conn)
963 fr_connection_t *this;
965 this = fr_connection_find(pool, conn);
968 rad_assert(this->in_use == true);
969 this->in_use = false;
972 * Determines whether the last used connection gets
977 * Put it at the tail of the list, so
978 * that it will get re-used last.
980 if (this != pool->tail) {
981 fr_connection_unlink(pool, this);
982 fr_connection_link_tail(pool, this);
986 * Put it at the head of the list, so
987 * that it will get re-used quickly.
989 if (this != pool->head) {
990 fr_connection_unlink(pool, this);
991 fr_connection_link_head(pool, this);
995 rad_assert(pool->active > 0);
998 DEBUG("%s: Released connection (%" PRIu64 ")", pool->log_prefix, this->number);
1001 * We mirror the "spawn on get" functionality by having
1002 * "delete on release". If there are too many spare
1003 * connections, go manage the pool && clean some up.
1005 fr_connection_pool_check(pool);
1008 /** Reconnect a suspected inviable connection
1010 * This should be called by the module if it suspects that a connection is
1011 * not viable (e.g. the server has closed it).
1013 * Will attempt to create a new connection handle using the create callback,
1014 * and if this is successful the new handle will be assigned to the existing
1017 * If this is not successful, the connection will be removed from the pool.
1019 * When implementing a module that uses the connection pool API, it is advisable
1020 * to pass a pointer to the pointer to the handle (void **conn)
1021 * to all functions which may call reconnect. This is so that if a new handle
1022 * is created and returned, the handle pointer can be updated up the callstack,
1023 * and a function higher up the stack doesn't attempt to use a now invalid
1024 * connection handle.
1026 * @warning After calling reconnect the caller *MUST NOT* attempt to use
1027 * the old handle in any other operations, as its memory will have been freed.
1029 * @see fr_connection_get
1030 * @param[in,out] pool to reconnect the connection in.
1031 * @param[in,out] conn to reconnect.
1032 * @return ew connection handle if successful else NULL.
1034 void *fr_connection_reconnect(fr_connection_pool_t *pool, void *conn)
1037 fr_connection_t *this;
1038 uint64_t conn_number;
1040 if (!pool || !conn) return NULL;
1042 this = fr_connection_find(pool, conn);
1043 if (!this) return NULL;
1046 * The pool is now locked.
1048 conn_number = this->number;
1050 DEBUG("%s: Reconnecting (%" PRIu64 ")", pool->log_prefix, conn_number);
1052 new_conn = pool->create(pool->ctx);
1054 time_t now = time(NULL);
1056 if (pool->last_complained == now) {
1059 pool->last_complained = now;
1063 * The caller has said the connection is bad. So
1064 * if it's still in use, mark it as unused, and
1065 * close it. If it's not in use, we just try to
1066 * get a new connection.
1069 this->in_use = false;
1071 rad_assert(pool->active > 0);
1074 fr_connection_close(pool, this);
1078 * We failed to create a new socket.
1079 * Try to grab an existing one.
1081 pthread_mutex_unlock(&pool->mutex);
1082 new_conn = fr_connection_get(pool);
1083 if (new_conn) return new_conn;
1085 if (!now) return NULL;
1087 ERROR("%s: Failed to reconnect (%" PRIu64 "), and no other connections available", pool->log_prefix,
1093 pool->delete(pool->ctx, conn);
1094 this->connection = new_conn;
1095 pthread_mutex_unlock(&pool->mutex);