Rename "start" to "created" as there's already another "start"
[freeradius.git] / src / main / connection.c
1 /*
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.
6  *
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.
11  *
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
15  */
16
17 /**
18  * @file connection.c
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.
22  *
23  * @copyright 2012  The FreeRADIUS server project
24  * @copyright 2012  Alan DeKok <aland@deployingradius.com>
25  */
26 RCSID("$Id$")
27
28 #include <freeradius-devel/radiusd.h>
29
30 #include <freeradius-devel/connection.h>
31
32 #include <freeradius-devel/rad_assert.h>
33
34 typedef struct fr_connection fr_connection_t;
35
36 static int fr_connection_pool_check(fr_connection_pool_t *pool);
37
38 /** An individual connection within the connection pool
39  *
40  * Defines connection counters, timestamps, and holds a pointer to the
41  * connection handle itself.
42  *
43  * @see fr_connection_pool_t
44  */
45 struct fr_connection {
46         fr_connection_t *prev;          //!< Previous connection in list.
47         fr_connection_t *next;          //!< Next connection in list.
48         
49         time_t          created;        //!< Time connection was created.
50         time_t          last_used;      //!< Last time the connection was
51                                         //!< reserved.
52                                         
53         int             num_uses;       //!< Number of times the connection
54                                         //!< has been reserved.
55         int             in_use;         //!< Whether the connection is currently
56                                         //!< reserved.
57         int             number;         //!< Unique ID assigned when the
58                                         //!< connection is created, these will
59                                         //!< monotonically increase over the
60                                         //!< lifetime of the connection pool.
61         void            *connection;    //!< Pointer to whatever the module
62                                         //!< uses for a connection handle.
63 };
64
65 /** A connection pool
66  *
67  * Defines the configuration of the connection pool, all the counters and
68  * timestamps related to the connection pool, the mutex that stops multiple
69  * threads leaving the pool in an inconsistent state, and the callbacks
70  * required to open, close and check the status of connections within the pool.
71  *
72  * @see fr_connection
73  */
74 struct fr_connection_pool_t {   
75         int             start;          //!< Number of initial connections
76         int             min;            //!< Minimum number of concurrent
77                                         //!< connections to keep open.
78         int             max;            //!< Maximum number of concurrent
79                                         //!< connections to allow.
80         int             spare;          //!< Number of spare connections to try
81                                         //!< and maintain.
82         int             cleanup_delay;  //!< How long a connection can go unused
83                                         //!< for before it's closed
84                                         //!< (0 is infinite).
85         int             max_uses;       //!< Maximum number of times a
86                                         //!< connection can be used before being
87                                         //!< closed.
88         int             lifetime;       //!< How long a connection can be open
89                                         //!< before being closed (irrespective
90                                         //!< of whether it's idle or not).
91         int             idle_timeout;   //!< How long a connection can be idle
92                                         //!< before being closed.
93                                         
94         int             trigger;        //!< If true execute connection triggers
95                                         //!< associated with the connection
96                                         //!< pool.
97                                         
98         int             spread;         //!< If true requests will be spread 
99                                         //!< across all connections, instead of
100                                         //!< re-using the most recently used
101                                         //!< connections first.
102
103         time_t          last_checked;   //!< Last time we pruned the connection
104                                         //!< pool.
105         time_t          last_spawned;   //!< Last time we spawned a connection.
106         time_t          last_failed;    //!< Last time we tried to spawn a
107                                         //!< a connection but failed.
108         time_t          last_complained;//!< Last time we complained about
109                                         //!< configuration parameters.
110         time_t          last_throttled; //!< Last time we refused to spawn a
111                                         //!< connection because the last
112                                         //!< connection failed, or we were
113                                         //!< already spawning a connection.
114         time_t          last_at_max;    //!< Last time we hit the maximum number
115                                         //!< of allowed connections.
116                                         
117         unsigned int    count;          //!< Number of connections spawned over
118                                         //!< the lifetime of the pool.
119         int             num;            //!< Number of connections in the pool.
120         int             active;         //!< Number of currently reserved
121                                         //!< connections.
122
123         fr_connection_t *head;          //!< Start of the connection list.
124         fr_connection_t *tail;          //!< End of the connection list.
125                                         
126         int             spawning;       //!< Whether we are currently attempting
127                                         //!< to spawn a new connection.
128
129 #ifdef HAVE_PTHREAD_H
130         pthread_mutex_t mutex;          //!< Mutex used to keep consistent state
131                                         //!< when making modifications in
132                                         //!< threaded mode.
133 #endif
134
135         CONF_SECTION    *cs;            //!< Configuration section holding
136                                         //!< the section of parsed config file
137                                         //!< that relates to this pool.
138         void            *ctx;           //!< Pointer to context data that will
139                                         //!< be passed to callbacks.
140         
141         char            *log_prefix;    //!< Log prefix to prepend to all log
142                                         //!< messages created by the connection
143                                         //!< pool code.
144
145         fr_connection_create_t  create; //!< Function used to create new
146                                         //!< connections.
147         fr_connection_alive_t   alive;  //!< Function used to check status
148                                         //!< of connections.
149         fr_connection_delete_t  delete; //!< Function used to close existing
150                                         //!< connections.
151 };
152
153 #define LOG_PREFIX "rlm_%s (%s)"
154 #ifndef HAVE_PTHREAD_H
155 #define pthread_mutex_lock(_x)
156 #define pthread_mutex_unlock(_x)
157 #endif
158
159 static const CONF_PARSER connection_config[] = {
160         { "start",    PW_TYPE_INTEGER, offsetof(fr_connection_pool_t, start),
161           0, "5" },
162         { "min",      PW_TYPE_INTEGER, offsetof(fr_connection_pool_t, min),
163           0, "5" },
164         { "max",      PW_TYPE_INTEGER, offsetof(fr_connection_pool_t, max),
165           0, "10" },
166         { "spare",    PW_TYPE_INTEGER, offsetof(fr_connection_pool_t, spare),
167           0, "3" },
168         { "uses",     PW_TYPE_INTEGER, offsetof(fr_connection_pool_t, max_uses),
169           0, "0" },
170         { "lifetime", PW_TYPE_INTEGER, offsetof(fr_connection_pool_t, lifetime),
171           0, "0" },
172         { "cleanup_delay", PW_TYPE_INTEGER, offsetof(fr_connection_pool_t, cleanup_delay),
173           0, "5" },
174         { "idle_timeout",  PW_TYPE_INTEGER, offsetof(fr_connection_pool_t, idle_timeout),
175           0, "60" },
176         { "spread", PW_TYPE_BOOLEAN, offsetof(fr_connection_pool_t, spread),
177           0, "no" },
178         { NULL, -1, 0, NULL, NULL }
179 };
180
181 /** Removes a connection from the connection list
182  *
183  * @note Must be called with the mutex held.
184  *
185  * @param[in,out] pool to modify.
186  * @param[in] this Connection to delete.
187  */
188 static void fr_connection_unlink(fr_connection_pool_t *pool,
189                                  fr_connection_t *this)
190 {
191         if (this->prev) {
192                 rad_assert(pool->head != this);
193                 this->prev->next = this->next;
194         } else {
195                 rad_assert(pool->head == this);
196                 pool->head = this->next;
197         }
198         if (this->next) {
199                 rad_assert(pool->tail != this);
200                 this->next->prev = this->prev;
201         } else {
202                 rad_assert(pool->tail == this);
203                 pool->tail = this->prev;
204         }
205
206         this->prev = this->next = NULL;
207 }
208
209 /** Adds a connection to the head of the connection list
210  *
211  * @note Must be called with the mutex held.
212  *
213  * @param[in,out] pool to modify.
214  * @param[in] this Connection to add.
215  */
216 static void fr_connection_link_head(fr_connection_pool_t *pool,
217                                     fr_connection_t *this)
218 {
219         rad_assert(pool != NULL);
220         rad_assert(this != NULL);
221         rad_assert(pool->head != this);
222         rad_assert(pool->tail != this);
223
224         if (pool->head) {
225                 pool->head->prev = this;
226         }
227         
228         this->next = pool->head;
229         this->prev = NULL;
230         pool->head = this;
231         if (!pool->tail) {
232                 rad_assert(this->next == NULL);
233                 pool->tail = this;
234         } else {
235                 rad_assert(this->next != NULL);
236         }
237 }
238
239 /** Adds a connection to the tail of the connection list
240  *
241  * @note Must be called with the mutex held.
242  *
243  * @param[in,out] pool to modify.
244  * @param[in] this Connection to add.
245  */
246 static void fr_connection_link_tail(fr_connection_pool_t *pool,
247                                     fr_connection_t *this)
248 {
249         rad_assert(pool != NULL);
250         rad_assert(this != NULL);
251         rad_assert(pool->head != this);
252         rad_assert(pool->tail != this);
253
254         if (pool->tail) {
255                 pool->tail->next = this;
256         }
257         this->prev = pool->tail;
258         this->next = NULL;
259         pool->tail = this;
260         if (!pool->head) {
261                 rad_assert(this->prev == NULL);
262                 pool->head = this;
263         } else {
264                 rad_assert(this->prev != NULL);
265         }
266 }
267
268
269 /** Spawns a new connection
270  *
271  * Spawns a new connection using the create callback, and returns it for
272  * adding to the connection list.
273  *
274  * @note Will call the 'open' trigger.
275  * @note Must be called with the mutex free.
276  *
277  * @param[in] pool to modify.
278  * @param[in] now Current time.
279  * @return the new connection struct or NULL on error.
280  */
281 static fr_connection_t *fr_connection_spawn(fr_connection_pool_t *pool,
282                                             time_t now)
283 {
284         fr_connection_t *this;
285         void *conn;
286         
287         rad_assert(pool != NULL);
288
289         /*
290          *      Prevent all threads from blocking if the resource
291          *      were managing connections for appears to be unavailable.
292          */
293         if ((pool->num == 0) && pool->spawning) {
294                 return NULL;
295         }
296
297         pthread_mutex_lock(&pool->mutex);
298         rad_assert(pool->num <= pool->max);
299
300         if ((pool->last_failed == now) || pool->spawning) {
301                 int complain = false;
302                 
303                 if (pool->last_throttled != now) {
304                         complain = true;
305                         
306                         pool->last_throttled = now;
307                 }
308
309                 pthread_mutex_unlock(&pool->mutex);
310                 
311                 if (complain) {
312                         if (pool->spawning) {
313                                 ERROR("%s: Cannot open new connection, "
314                                        "connection spawning already in "
315                                        "progress", pool->log_prefix);
316                         } else {
317                                 ERROR("%s: Last connection failed, "
318                                        "throttling connection spawn",
319                                        pool->log_prefix);
320                         }
321                 }
322                 
323                 return NULL;
324         }
325
326         pool->spawning = true;
327
328         /*
329          *      Unlock the mutex while we try to open a new
330          *      connection.  If there are issues with the back-end,
331          *      opening a new connection may take a LONG time.  In
332          *      that case, we want the other connections to continue
333          *      to be used.
334          */
335         pthread_mutex_unlock(&pool->mutex);
336
337         INFO("%s: Opening additional connection (%i)",
338                pool->log_prefix, pool->count);
339         
340         this = rad_malloc(sizeof(*this));
341         memset(this, 0, sizeof(*this));
342
343         /*
344          *      This may take a long time, which prevents other
345          *      threads from releasing connections.  We don't care
346          *      about other threads opening new connections, as we
347          *      already have no free connections.
348          */
349         conn = pool->create(pool->ctx);
350         if (!conn) {
351                 ERROR("%s: Opening connection failed (%i)",
352                        pool->log_prefix, pool->count);
353                 
354                 pool->last_failed = now;
355                 free(this);
356                 pool->spawning = false; /* atomic, so no lock is needed */
357                 return NULL;
358         }
359         
360         this->created = now;
361         this->connection = conn;        
362
363         /*
364          *      And lock the mutex again while we link the new
365          *      connection back into the pool.
366          */
367         pthread_mutex_lock(&pool->mutex);
368
369         this->number = pool->count++;
370         this->last_used = now;
371         fr_connection_link_head(pool, this);
372         pool->num++;
373         pool->spawning = false;
374         pool->last_spawned = time(NULL);
375
376         pthread_mutex_unlock(&pool->mutex);
377
378         if (pool->trigger) exec_trigger(NULL, pool->cs, "open", true);
379
380         return this;
381 }
382
383 /** Add a new connection to the pool
384  *
385  * If conn is not NULL will attempt to add that connection handle to the pool.
386  * If conn is NULL will attempt to spawn a new connection using the create
387  * callback.
388  *
389  * @note Will call the 'open' trigger.
390  *
391  * @param[in,out] pool to add connection to.
392  * @param[in] conn to add.
393  * @return 0 if the connection wasn't added else 1.
394  */
395 int fr_connection_add(fr_connection_pool_t *pool, void *conn)
396 {
397         fr_connection_t *this;
398
399         if (!pool) return 0;
400
401         pthread_mutex_lock(&pool->mutex);
402
403         if (!conn) {
404                 conn = pool->create(pool->ctx);
405                 if (!conn) {
406                         pthread_mutex_unlock(&pool->mutex);
407                         return 0;
408                 }
409                 
410                 INFO("%s: Opening connection successful (%i)",
411                        pool->log_prefix, pool->count);
412         }
413
414         /*
415          *      Too many connections: can't add it.
416          */
417         if (pool->num >= pool->max) {
418                 pthread_mutex_unlock(&pool->mutex);
419                 return 0;
420         }
421
422         this = rad_malloc(sizeof(*this));
423         memset(this, 0, sizeof(*this));
424
425         this->created = time(NULL);
426         this->connection = conn;
427
428         this->number = pool->count++;
429         this->last_used = time(NULL);
430         fr_connection_link_head(pool, this);
431         pool->num++;
432
433         pthread_mutex_unlock(&pool->mutex);
434
435         if (pool->trigger) exec_trigger(NULL, pool->cs, "open", true);
436
437         return 1;
438 }
439
440 /** Close an existing connection.
441  *
442  * Removes the connection from the list, calls the delete callback to close
443  * the connection, then frees memory allocated to the connection.
444  *
445  * @note Will call the 'close' trigger.
446  * @note Must be called with the mutex held.
447  *
448  * @param[in,out] pool to modify.
449  * @param[in,out] this Connection to delete.
450
451  */
452 static void fr_connection_close(fr_connection_pool_t *pool,
453                                 fr_connection_t *this)
454 {
455         if (pool->trigger) exec_trigger(NULL, pool->cs, "close", true);
456
457         rad_assert(this->in_use == false);
458
459         fr_connection_unlink(pool, this);
460         pool->delete(pool->ctx, this->connection);
461         rad_assert(pool->num > 0);
462         pool->num--;
463         free(this);
464 }
465
466 /** Find a connection handle in the connection list
467  *
468  * Walks over the list of connections searching for a specified connection
469  * handle and returns the first connection that contains that pointer.
470  *
471  * @note Will lock mutex and only release mutex if connection handle
472  * is not found, so will usually return will mutex held.
473  * @note Must be called with the mutex free.
474  *
475  * @param[in] pool to search in.
476  * @param[in] conn handle to search for.
477  * @return the connection containing the specified handle, or NULL if non is
478  * found.
479  */
480 static fr_connection_t *fr_connection_find(fr_connection_pool_t *pool, void *conn)
481 {
482         fr_connection_t *this;
483
484         if (!pool || !conn) return NULL;
485
486         pthread_mutex_lock(&pool->mutex);
487
488         /*
489          *      FIXME: This loop could be avoided if we passed a 'void
490          *      **connection' instead.  We could use "offsetof" in
491          *      order to find top of the parent structure.
492          */
493         for (this = pool->head; this != NULL; this = this->next) {
494                 if (this->connection == conn) return this;
495         }
496
497         pthread_mutex_unlock(&pool->mutex);
498         return NULL;
499 }
500
501 /** Delete a connection from the connection pool.
502  *
503  * Resolves the connection handle to a connection, then (if found)
504  * closes, unlinks and frees that connection.
505  *
506  * @note Must be called with the mutex free.
507  *
508  * @param[in,out] pool Connection pool to modify.
509  * @param[in] conn to delete.
510  * @return 0 if the connection could not be found, else 1.
511  */
512 int fr_connection_del(fr_connection_pool_t *pool, void *conn)
513 {
514         fr_connection_t *this;
515
516         this = fr_connection_find(pool, conn);
517         if (!this) return 0;
518
519         /*
520          *      If it's in use, release it.
521          */
522         if (this->in_use) {
523                 rad_assert(this->in_use == true);
524                 this->in_use = false;
525                 
526                 rad_assert(pool->active > 0);
527                 pool->active--;
528         }
529
530         INFO("%s: Deleting connection (%i)", pool->log_prefix,
531                this->number);
532
533         fr_connection_close(pool, this);
534         fr_connection_pool_check(pool);
535         return 1;
536 }
537
538 /** Delete a connection pool
539  *
540  * Closes, unlinks and frees all connections in the connection pool, then frees
541  * all memory used by the connection pool.
542  *
543  * @note Will call the 'stop' trigger.
544  * @note Must be called with the mutex free.
545  *
546  * @param[in,out] pool to delete.
547  */
548 void fr_connection_pool_delete(fr_connection_pool_t *pool)
549 {
550         fr_connection_t *this, *next;
551
552         if (!pool) return;
553
554         DEBUG("%s: Removing connection pool", pool->log_prefix);
555
556         pthread_mutex_lock(&pool->mutex);
557
558         for (this = pool->head; this != NULL; this = next) {
559                 next = this->next;
560                 
561                 INFO("%s: Closing connection (%i)", pool->log_prefix,
562                        this->number);
563                 
564                 fr_connection_close(pool, this);
565         }
566
567         if (pool->trigger) exec_trigger(NULL, pool->cs, "stop", true);
568
569         rad_assert(pool->head == NULL);
570         rad_assert(pool->tail == NULL);
571         rad_assert(pool->num == 0);
572         
573         free(pool->log_prefix);
574         free(pool);
575 }
576
577 /** Create a new connection pool
578  *
579  * Allocates structures used by the connection pool, initialises the various
580  * configuration options and counters, and sets the callback functions.
581  *
582  * Will also spawn the number of connections specified by the 'start'
583  * configuration options.
584  *
585  * @note Will call the 'start' trigger.
586  *
587  * @param[in] parent configuration section containing a 'pool' subsection.
588  * @param[in] ctx pointer to pass to callbacks.
589  * @param[in] c Callback to create new connections.
590  * @param[in] a Callback to check the status of connections.
591  * @param[in] d Callback to delete connections.
592  * @param[in] prefix to prepend to all log message, if NULL will create prefix
593  *      from parent conf section names. 
594  * @return A new connection pool or NULL on error.
595  */
596 fr_connection_pool_t *fr_connection_pool_init(CONF_SECTION *parent,
597                                               void *ctx,
598                                               fr_connection_create_t c,
599                                               fr_connection_alive_t a,
600                                               fr_connection_delete_t d,
601                                               char *prefix)
602 {
603         int i, lp_len;
604         fr_connection_pool_t *pool;
605         fr_connection_t *this;
606         CONF_SECTION *modules;
607         CONF_SECTION *cs;
608         char const *cs_name1, *cs_name2;
609         time_t now = time(NULL);
610
611         if (!parent || !ctx || !c || !d) return NULL;
612
613         cs = cf_section_sub_find(parent, "pool");
614         if (!cs) cs = cf_section_sub_find(parent, "limit");
615
616         pool = rad_malloc(sizeof(*pool));
617         memset(pool, 0, sizeof(*pool));
618
619         pool->cs = cs;
620         pool->ctx = ctx;
621         pool->create = c;
622         pool->alive = a;
623         pool->delete = d;
624         
625         pool->head = pool->tail = NULL;
626
627 #ifdef HAVE_PTHREAD_H
628         pthread_mutex_init(&pool->mutex, NULL);
629 #endif
630
631         if (!prefix) {
632                 modules = cf_item_parent(cf_sectiontoitem(parent));
633                 if (modules) {
634                         cs_name1 = cf_section_name1(modules);
635                         if (cs_name1 && (strcmp(cs_name1, "modules") == 0)) {
636                                 cs_name1 = cf_section_name1(parent);
637                                 cs_name2 = cf_section_name2(parent);
638                                 if (!cs_name2) {
639                                         cs_name2 = cs_name1;
640                                 }
641
642                                 lp_len = (sizeof(LOG_PREFIX) - 4) + strlen(cs_name1) + strlen(cs_name2);
643                                 pool->log_prefix = rad_malloc(lp_len);
644                                 snprintf(pool->log_prefix, lp_len, LOG_PREFIX, cs_name1,
645                                          cs_name2);
646                         }
647                 } else {                /* not a module configuration */
648                         cs_name1 = cf_section_name1(parent);
649
650                         pool->log_prefix = strdup(cs_name1);
651                 }
652         } else {
653                 pool->log_prefix = strdup(prefix);
654         }
655         
656         DEBUG("%s: Initialising connection pool", pool->log_prefix);
657
658         if (cs) {
659                 if (cf_section_parse(cs, pool, connection_config) < 0) {
660                         goto error;
661                 }
662
663                 if (cf_section_sub_find(cs, "trigger")) pool->trigger = true;
664         } else {
665                 pool->start = 5;
666                 pool->min = 5;
667                 pool->max = 10;
668                 pool->spare = 3;
669                 pool->cleanup_delay = 5;
670                 pool->idle_timeout = 60;
671         }
672
673         /*
674          *      Some simple limits
675          */
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;
680         }
681         if ((pool->lifetime > 0) && (pool->idle_timeout > pool->lifetime)) {
682                 pool->idle_timeout = 0;
683         }
684
685         /*
686          *      Create all of the connections, unless the admin says
687          *      not to.
688          */
689         for (i = 0; i < pool->start; i++) {
690                 this = fr_connection_spawn(pool, now);  
691                 if (!this) {
692                 error:
693                         fr_connection_pool_delete(pool);
694                         return NULL;
695                 }
696         }
697
698         if (pool->trigger) exec_trigger(NULL, pool->cs, "start", true);
699
700         return pool;
701 }
702
703
704 /** Check whether a connection needs to be removed from the pool
705  *
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.
708  *
709  * @note Will only close connections not in use.
710  * @note Must be called with the mutex held.
711  *
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.
716  */
717 static int fr_connection_manage(fr_connection_pool_t *pool,
718                                 fr_connection_t *this,
719                                 time_t now)
720 {
721         rad_assert(pool != NULL);
722         rad_assert(this != NULL);
723         
724         /*
725          *      Don't terminated in-use connections
726          */
727         if (this->in_use) return 1;
728
729         if ((pool->max_uses > 0) &&
730             (this->num_uses >= pool->max_uses)) {
731                 DEBUG("%s: Closing expired connection (%i): Hit max_uses limit",
732                       pool->log_prefix, this->number);
733         do_delete:
734                 if ((pool->num <= pool->min) &&
735                     (pool->last_complained < now)) {
736                         WARN("%s: You probably need to lower \"min\"", pool->log_prefix);
737                         
738                         pool->last_complained = now;
739                 }
740                 fr_connection_close(pool, this);
741                 return 0;
742         }
743
744         if ((pool->lifetime > 0) &&
745             ((this->created + pool->lifetime) < now)) {
746                 DEBUG("%s: Closing expired connection (%i) ", pool->log_prefix,
747                       this->number);
748                 goto do_delete;
749         }
750
751         if ((pool->idle_timeout > 0) &&
752             ((this->last_used + pool->idle_timeout) < now)) {
753                 INFO("%s: Closing connection (%i): Hit idle_timeout, "
754                        "was idle for %u seconds", pool->log_prefix, this->number,
755                        (int) (now - this->last_used));
756                 goto do_delete;
757         }
758         
759         return 1;
760 }
761
762
763 /** Check whether any connections needs to be removed from the pool
764  *
765  * Maintains the number of connections in the pool as per the configuration
766  * parameters for the connection pool.
767  *
768  * @note Will only run checks the first time it's called in a given second,
769  * to throttle connection spawning/closing.
770  * @note Will only close connections not in use.
771  * @note Must be called with the mutex held, will release mutex before
772  * returning.
773  *
774  * @param[in,out] pool to manage.
775  * @return 1
776  */
777 static int fr_connection_pool_check(fr_connection_pool_t *pool)
778 {
779         int spare, spawn;
780         time_t now = time(NULL);
781         fr_connection_t *this, *next;
782
783         if (pool->last_checked == now) {
784                 pthread_mutex_unlock(&pool->mutex);
785                 return 1;
786         }
787
788         spare = pool->num - pool->active;
789
790         spawn = 0;
791         if ((pool->num < pool->max) && (spare < pool->spare)) {
792                 spawn = pool->spare - spare;
793                 if ((spawn + pool->num) > pool->max) {
794                         spawn = pool->max - pool->num;
795                 }
796                 if (pool->spawning) spawn = 0;
797
798                 if (spawn) {
799                         pthread_mutex_unlock(&pool->mutex);
800                         fr_connection_spawn(pool, now); /* ignore return code */
801                         pthread_mutex_lock(&pool->mutex);
802                 }
803         }
804
805         /*
806          *      We haven't spawned connections in a while, and there
807          *      are too many spare ones.  Close the one which has been
808          *      idle for the longest.
809          */
810         if ((now >= (pool->last_spawned + pool->cleanup_delay)) &&
811             (spare > pool->spare)) {
812                 fr_connection_t *idle;
813
814                 idle = NULL;
815                 for (this = pool->tail; this != NULL; this = this->prev) {
816                         if (this->in_use) continue;
817
818                         if (!idle ||
819                            (this->last_used < idle->last_used)) {
820                                 idle = this;
821                         }
822                 }
823
824                 rad_assert(idle != NULL);
825                 
826                 INFO("%s: Closing connection (%i): Too many "
827                        "free connections (%d > %d)", pool->log_prefix,
828                        idle->number, spare, pool->spare);
829                 fr_connection_close(pool, idle);
830         }
831
832         /*
833          *      Pass over all of the connections in the pool, limiting
834          *      lifetime, idle time, max requests, etc.
835          */
836         for (this = pool->head; this != NULL; this = next) {
837                 next = this->next;
838                 fr_connection_manage(pool, this, now);
839         }
840
841         pool->last_checked = now;
842         pthread_mutex_unlock(&pool->mutex);
843
844         return 1;
845 }
846
847 /** Trigger connection check for a given connection or all connections
848  *
849  * If conn is not NULL then we call fr_connection_manage on the connection.
850  * If conn is NULL we call fr_connection_pool_check on the pool.
851  *
852  * @note Only connections that are not in use will be closed.
853  *
854  * @see fr_connection_manage
855  * @see fr_connection_pool_check
856  * @param[in,out] pool to manage.
857  * @param[in,out] conn to check.
858  * @return 0 if the connection was closed, else 1.
859  */
860 int fr_connection_check(fr_connection_pool_t *pool, void *conn)
861 {
862         fr_connection_t *this;
863         time_t now;
864         int ret = 1;
865         
866         if (!pool) return 1;
867
868         now = time(NULL);
869         pthread_mutex_lock(&pool->mutex);
870
871         if (!conn) return fr_connection_pool_check(pool);
872
873         for (this = pool->head; this != NULL; this = this->next) {
874                 if (this->connection == conn) {
875                         ret = fr_connection_manage(pool, conn, now);
876                         break;
877                 }
878         }
879
880         pthread_mutex_unlock(&pool->mutex);
881
882         return ret;
883 }
884
885 /** Reserve a connection in the connection pool
886  *
887  * Will attempt to find an unused connection in the connection pool, if one is
888  * found, will mark it as in in use increment the number of active connections
889  * and return the connection handle.
890  *
891  * If no free connections are found will attempt to spawn a new one, conditional
892  * on a connection spawning not already being in progress, and not being at the
893  * 'max' connection limit.
894  *
895  * @note fr_connection_release must be called once the caller has finished
896  * using the connection.
897  *
898  * @see fr_connection_release
899  * @param[in,out] pool to reserve the connection from.
900  * @return a pointer to the connection handle, or NULL on error.
901  */
902 void *fr_connection_get(fr_connection_pool_t *pool)
903 {
904         time_t now;
905         fr_connection_t *this, *next;
906
907         if (!pool) return NULL;
908
909         pthread_mutex_lock(&pool->mutex);
910
911         now = time(NULL);
912         for (this = pool->head; this != NULL; this = next) {
913                 next = this->next;
914
915                 if (!this->in_use) goto do_return;
916         }
917
918         if (pool->num == pool->max) {
919                 int complain = false;
920
921                 /*
922                  *      Rate-limit complaints.
923                  */
924                 if (pool->last_at_max != now) {
925                         complain = true;
926                         pool->last_at_max = now;
927                 }
928                 
929                 pthread_mutex_unlock(&pool->mutex);
930                 
931                 if (complain) {
932                         ERROR("%s: No connections available and at max "
933                                "connection limit", pool->log_prefix);
934                 }
935                 
936                 return NULL;
937         }
938
939         pthread_mutex_unlock(&pool->mutex);
940         this = fr_connection_spawn(pool, now);
941         if (!this) return NULL;
942         pthread_mutex_lock(&pool->mutex);
943
944 do_return:
945         pool->active++;
946         this->num_uses++;
947         this->last_used = now;
948         this->in_use = true;
949
950         pthread_mutex_unlock(&pool->mutex);
951         
952         DEBUG("%s: Reserved connection (%i)", pool->log_prefix, this->number);
953         
954         return this->connection;
955 }
956
957 /** Release a connection
958  *
959  * Will mark a connection as unused and decrement the number of active
960  * connections.
961  *
962  * @see fr_connection_get
963  * @param[in,out] pool to release the connection in.
964  * @param[in,out] conn to release.
965  */
966 void fr_connection_release(fr_connection_pool_t *pool, void *conn)
967 {
968         fr_connection_t *this;
969
970         this = fr_connection_find(pool, conn);
971         if (!this) return;
972
973         rad_assert(this->in_use == true);
974         this->in_use = false;
975         
976         /*
977          *      Determines whether the last used connection gets
978          *      re-used first.
979          */
980         if (pool->spread) {
981                 /*
982                  *      Put it at the tail of the list, so
983                  *      that it will get re-used last.
984                  */
985                 if (this != pool->tail) {
986                         fr_connection_unlink(pool, this);
987                         fr_connection_link_tail(pool, this);
988                 }
989         } else {
990                 /*
991                  *      Put it at the head of the list, so
992                  *      that it will get re-used quickly.
993                  */
994                 if (this != pool->head) {
995                         fr_connection_unlink(pool, this);
996                         fr_connection_link_head(pool, this);
997                 }
998         }
999
1000         rad_assert(pool->active > 0);
1001         pool->active--;
1002
1003         DEBUG("%s: Released connection (%i)", pool->log_prefix, this->number);
1004
1005         /*
1006          *      We mirror the "spawn on get" functionality by having
1007          *      "delete on release".  If there are too many spare
1008          *      connections, go manage the pool && clean some up.
1009          */
1010         fr_connection_pool_check(pool);
1011 }
1012
1013 /** Reconnect a suspected inviable connection
1014  *
1015  * This should be called by the module if it suspects that a connection is
1016  * not viable (e.g. the server has closed it).
1017  *
1018  * Will attempt to create a new connection handle using the create callback,
1019  * and if this is successful the new handle will be assigned to the existing
1020  * pool connection.
1021  *
1022  * If this is not successful, the connection will be removed from the pool.
1023  *
1024  * When implementing a module that uses the connection pool API, it is advisable
1025  * to pass a pointer to the pointer to the handle (void **conn)
1026  * to all functions which may call reconnect. This is so that if a new handle
1027  * is created and returned, the handle pointer can be updated up the callstack,
1028  * and a function higher up the stack doesn't attempt to use a now invalid
1029  * connection handle.
1030  *
1031  * @warning After calling reconnect the caller *MUST NOT* attempt to use
1032  * the old handle in any other operations, as its memory will have been freed.
1033  *
1034  * @see fr_connection_get
1035  * @param[in,out] pool to reconnect the connection in.
1036  * @param[in,out] conn to reconnect.
1037  * @return ew connection handle if successful else NULL.
1038  */
1039 void *fr_connection_reconnect(fr_connection_pool_t *pool, void *conn)
1040 {
1041         void *new_conn;
1042         fr_connection_t *this;
1043         int conn_number;
1044
1045         if (!pool || !conn) return NULL;
1046
1047         this = fr_connection_find(pool, conn);
1048         if (!this) return NULL;
1049         
1050         conn_number = this->number;
1051
1052         rad_assert(this->in_use == true);
1053         
1054         DEBUG("%s: Reconnecting (%i)", pool->log_prefix, conn_number);
1055         
1056         new_conn = pool->create(pool->ctx);
1057         if (!new_conn) {
1058                 time_t now = time(NULL);
1059                 
1060                 if (pool->last_complained == now) {
1061                         now = 0;
1062                 } else {
1063                         pool->last_complained = now;
1064                 }
1065         
1066                 this->in_use = false;
1067
1068                 rad_assert(pool->active > 0);
1069                 pool->active--;
1070         
1071                 fr_connection_close(pool, this);
1072                 pthread_mutex_unlock(&pool->mutex);
1073                 
1074                 /*
1075                  *      Can't create a new socket.
1076                  *      Try grabbing a pre-existing one.
1077                  */
1078                 new_conn = fr_connection_get(pool);
1079                 if (new_conn) return new_conn;
1080                 
1081                 if (!now) return NULL;
1082                 
1083                 ERROR("%s: Failed to reconnect (%i), and no other "
1084                        "connections available", pool->log_prefix, conn_number);
1085                 
1086                 return NULL;
1087         }
1088         
1089         pool->delete(pool->ctx, conn);
1090         this->connection = new_conn;
1091         pthread_mutex_unlock(&pool->mutex);
1092         return new_conn;
1093 }