-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
connection.c
1516 lines (1284 loc) · 44.2 KB
/
connection.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
*/
/**
* @file connection.c
* @brief Handle pools of connections (threads, sockets, etc.)
* @note This API must be used by all modules in the public distribution that
* maintain pools of connections.
*
* @copyright 2012 The FreeRADIUS server project
* @copyright 2012 Alan DeKok <aland@deployingradius.com>
*/
RCSID("$Id$")
#define LOG_PREFIX "%s - "
#define LOG_PREFIX_ARGS pool->log_prefix
#include <freeradius-devel/radiusd.h>
#include <freeradius-devel/heap.h>
#include <freeradius-devel/modpriv.h>
#include <freeradius-devel/rad_assert.h>
typedef struct fr_connection fr_connection_t;
static int fr_connection_pool_check(fr_connection_pool_t *pool, REQUEST *request);
/** An individual connection within the connection pool
*
* Defines connection counters, timestamps, and holds a pointer to the
* connection handle itself.
*
* @see fr_connection_pool_t
*/
struct fr_connection {
fr_connection_t *prev; //!< Previous connection in list.
fr_connection_t *next; //!< Next connection in list.
time_t created; //!< Time connection was created.
struct timeval last_reserved; //!< Last time the connection was reserved.
struct timeval last_released; //!< Time the connection was released.
uint32_t num_uses; //!< Number of times the connection has been reserved.
uint64_t number; //!< Unique ID assigned when the connection is created,
//!< these will monotonically increase over the
//!< lifetime of the connection pool.
void *connection; //!< Pointer to whatever the module uses for a connection
//!< handle.
bool in_use; //!< Whether the connection is currently reserved.
int heap; //!< For the next connection heap.
bool needs_reconnecting; //!< Reconnect this connection before use.
#ifdef PTHREAD_DEBUG
pthread_t pthread_id; //!< When 'in_use == true'.
#endif
};
/** A connection pool
*
* Defines the configuration of the connection pool, all the counters and
* timestamps related to the connection pool, the mutex that stops multiple
* threads leaving the pool in an inconsistent state, and the callbacks
* required to open, close and check the status of connections within the pool.
*
* @see fr_connection
*/
struct fr_connection_pool_t {
int ref; //!< Reference counter to prevent connection
//!< pool being freed multiple times.
uint32_t start; //!< Number of initial connections.
uint32_t min; //!< Minimum number of concurrent connections to keep open.
uint32_t max; //!< Maximum number of concurrent connections to allow.
uint32_t max_pending; //!< Max number of pending connections to allow.
uint32_t spare; //!< Number of spare connections to try.
uint32_t retry_delay; //!< seconds to delay re-open after a failed open.
uint32_t cleanup_interval; //!< Initial timer for how often we sweep the pool
//!< for free connections. (0 is infinite).
int delay_interval; //!< When we next do a cleanup. Initialized to
//!< cleanup_interval, and increase from there based
//!< on the delay.
uint64_t max_uses; //!< Maximum number of times a connection can be used
//!< before being closed.
uint32_t pending_window; //!< Sliding window of pending connections.
uint32_t lifetime; //!< How long a connection can be open before being
//!< closed (irrespective of whether it's idle or not).
uint32_t idle_timeout; //!< How long a connection can be idle before
//!< being closed.
struct timeval connect_timeout; //!< New connection timeout, enforced by the create
//!< callback.
bool spread; //!< If true we spread requests over the connections,
//!< using the connection released longest ago, first.
fr_heap_t *heap; //!< For the next connection heap
fr_connection_t *head; //!< Start of the connection list.
fr_connection_t *tail; //!< End of the connection list.
pthread_mutex_t mutex; //!< Mutex used to keep consistent state when making
//!< modifications in threaded mode.
pthread_cond_t done_spawn; //!< Threads that need to ensure no spawning is in progress,
//!< should block on this condition if pending != 0.
pthread_cond_t done_reconnecting; //!< Before calling the create callback, threads should
//!< block on this condition if reconnecting == true.
CONF_SECTION const *cs; //!< Configuration section holding the section of parsed
//!< config file that relates to this pool.
void *opaque; //!< Pointer to context data that will be passed to callbacks.
char const *log_prefix; //!< Log prefix to prepend to all log messages created
//!< by the connection pool code.
char const *trigger_prefix; //!< Prefix to prepend to names of all triggers
//!< fired by the connection pool code.
VALUE_PAIR *trigger_args; //!< Arguments to make available in connection pool triggers.
struct timeval held_trigger_min; //!< If a connection is held for less than the specified
//!< period, fire a trigger.
struct timeval held_trigger_max; //!< If a connection is held for longer than the specified
//!< period, fire a trigger.
fr_connection_create_t create; //!< Function used to create new connections.
fr_connection_alive_t alive; //!< Function used to check status of connections.
fr_connection_pool_reconnect_t reconnect; //!< Called during connection pool reconnect.
fr_connection_pool_state_t state; //!< Stats and state of the connection pool.
};
static const CONF_PARSER connection_config[] = {
{ FR_CONF_OFFSET("start", PW_TYPE_INTEGER, fr_connection_pool_t, start), .dflt = "5" },
{ FR_CONF_OFFSET("min", PW_TYPE_INTEGER, fr_connection_pool_t, min), .dflt = "5" },
{ FR_CONF_OFFSET("max", PW_TYPE_INTEGER, fr_connection_pool_t, max), .dflt = "10" },
{ FR_CONF_OFFSET("max_pending", PW_TYPE_INTEGER, fr_connection_pool_t, max_pending), .dflt = "0" },
{ FR_CONF_OFFSET("spare", PW_TYPE_INTEGER, fr_connection_pool_t, spare), .dflt = "3" },
{ FR_CONF_OFFSET("uses", PW_TYPE_INTEGER64, fr_connection_pool_t, max_uses), .dflt = "0" },
{ FR_CONF_OFFSET("lifetime", PW_TYPE_INTEGER, fr_connection_pool_t, lifetime), .dflt = "0" },
{ FR_CONF_OFFSET("cleanup_delay", PW_TYPE_INTEGER, fr_connection_pool_t, cleanup_interval) },
{ FR_CONF_OFFSET("cleanup_interval", PW_TYPE_INTEGER, fr_connection_pool_t, cleanup_interval), .dflt = "30" },
{ FR_CONF_OFFSET("idle_timeout", PW_TYPE_INTEGER, fr_connection_pool_t, idle_timeout), .dflt = "60" },
{ FR_CONF_OFFSET("connect_timeout", PW_TYPE_TIMEVAL, fr_connection_pool_t, connect_timeout), .dflt = "3.0" },
{ FR_CONF_OFFSET("held_trigger_min", PW_TYPE_TIMEVAL, fr_connection_pool_t, held_trigger_min), .dflt = "0.0" },
{ FR_CONF_OFFSET("held_trigger_max", PW_TYPE_TIMEVAL, fr_connection_pool_t, held_trigger_max), .dflt = "0.5" },
{ FR_CONF_OFFSET("retry_delay", PW_TYPE_INTEGER, fr_connection_pool_t, retry_delay), .dflt = "1" },
{ FR_CONF_OFFSET("spread", PW_TYPE_BOOLEAN, fr_connection_pool_t, spread), .dflt = "no" },
CONF_PARSER_TERMINATOR
};
/** Order connections by reserved most recently
*/
static int last_reserved_cmp(void const *one, void const *two)
{
fr_connection_t const *a = one;
fr_connection_t const *b = two;
if (a->last_reserved.tv_sec < b->last_reserved.tv_sec) return -1;
if (a->last_reserved.tv_sec > b->last_reserved.tv_sec) return +1;
if (a->last_reserved.tv_usec < b->last_reserved.tv_usec) return -1;
if (a->last_reserved.tv_usec > b->last_reserved.tv_usec) return +1;
return 0;
}
/** Order connections by released longest ago
*/
static int last_released_cmp(void const *one, void const *two)
{
fr_connection_t const *a = one;
fr_connection_t const *b = two;
if (b->last_released.tv_sec < a->last_released.tv_sec) return -1;
if (b->last_released.tv_sec > a->last_released.tv_sec) return +1;
if (b->last_released.tv_usec < a->last_released.tv_usec) return -1;
if (b->last_released.tv_usec > a->last_released.tv_usec) return +1;
return 0;
}
/** Removes a connection from the connection list
*
* @note Must be called with the mutex held.
*
* @param[in] pool to modify.
* @param[in] this Connection to delete.
*/
static void fr_connection_unlink(fr_connection_pool_t *pool, fr_connection_t *this)
{
if (this->prev) {
rad_assert(pool->head != this);
this->prev->next = this->next;
} else {
rad_assert(pool->head == this);
pool->head = this->next;
}
if (this->next) {
rad_assert(pool->tail != this);
this->next->prev = this->prev;
} else {
rad_assert(pool->tail == this);
pool->tail = this->prev;
}
this->prev = this->next = NULL;
}
/** Adds a connection to the head of the connection list
*
* @note Must be called with the mutex held.
*
* @param[in] pool to modify.
* @param[in] this Connection to add.
*/
static void fr_connection_link_head(fr_connection_pool_t *pool, fr_connection_t *this)
{
rad_assert(pool != NULL);
rad_assert(this != NULL);
rad_assert(pool->head != this);
rad_assert(pool->tail != this);
if (pool->head) {
pool->head->prev = this;
}
this->next = pool->head;
this->prev = NULL;
pool->head = this;
if (!pool->tail) {
rad_assert(this->next == NULL);
pool->tail = this;
} else {
rad_assert(this->next != NULL);
}
}
/** Send a connection pool trigger.
*
* @param[in] pool to send trigger for.
* @param[in] request The current request (may be NULL).
* @param[in] event trigger name suffix.
*/
static inline void fr_connection_trigger_exec(fr_connection_pool_t *pool, REQUEST *request, char const *event)
{
char name[128];
rad_assert(pool != NULL);
rad_assert(event != NULL);
if (!pool->trigger_prefix) return;
snprintf(name, sizeof(name), "%s.%s", pool->trigger_prefix, event);
trigger_exec(request, pool->cs, name, true, pool->trigger_args);
}
/** Find a connection handle in the connection list
*
* Walks over the list of connections searching for a specified connection
* handle and returns the first connection that contains that pointer.
*
* @note Will lock mutex and only release mutex if connection handle
* is not found, so will usually return will mutex held.
* @note Must be called with the mutex free.
*
* @param[in] pool to search in.
* @param[in] conn handle to search for.
* @return
* - Connection containing the specified handle.
* - NULL if non if connection was found.
*/
static fr_connection_t *fr_connection_find(fr_connection_pool_t *pool, void *conn)
{
fr_connection_t *this;
if (!pool || !conn) return NULL;
pthread_mutex_lock(&pool->mutex);
/*
* FIXME: This loop could be avoided if we passed a 'void
* **connection' instead. We could use "offsetof" in
* order to find top of the parent structure.
*/
for (this = pool->head; this != NULL; this = this->next) {
if (this->connection == conn) {
#ifdef PTHREAD_DEBUG
pthread_t pthread_id;
pthread_id = pthread_self();
rad_assert(pthread_equal(this->pthread_id, pthread_id) != 0);
#endif
rad_assert(this->in_use == true);
return this;
}
}
pthread_mutex_unlock(&pool->mutex);
return NULL;
}
/** Spawns a new connection
*
* Spawns a new connection using the create callback, and returns it for
* adding to the connection list.
*
* @note Will call the 'open' trigger.
* @note Must be called with the mutex free.
*
* @param[in] pool to modify.
* @param[in] request The current request.
* @param[in] now Current time.
* @param[in] in_use whether the new connection should be "in_use" or not
* @param[in] unlock whether we should unlock the mutex before returning
* @return
* - New connection struct.
* - NULL on error.
*/
static fr_connection_t *fr_connection_spawn(fr_connection_pool_t *pool, REQUEST *request, time_t now, bool in_use, bool unlock)
{
uint64_t number;
uint32_t pending_window;
TALLOC_CTX *ctx;
fr_connection_t *this;
void *conn;
rad_assert(pool != NULL);
/*
* If we have NO connections, and we've previously failed
* opening connections, don't open multiple connections until
* we successfully open at least one.
*/
if ((pool->state.num == 0) && pool->state.pending && pool->state.last_failed) return NULL;
pthread_mutex_lock(&pool->mutex);
rad_assert(pool->state.num <= pool->max);
/*
* Don't spawn too many connections at the same time.
*/
if ((pool->state.num + pool->state.pending) >= pool->max) {
pthread_mutex_unlock(&pool->mutex);
ROPTIONAL(RERROR, ERROR, "Cannot open new connection, already at max");
return NULL;
}
/*
* If the last attempt failed, wait a bit before
* retrying.
*/
if (pool->state.last_failed && ((pool->state.last_failed + pool->retry_delay) > now)) {
bool complain = false;
if (pool->state.last_throttled != now) {
complain = true;
pool->state.last_throttled = now;
}
pthread_mutex_unlock(&pool->mutex);
if (!RATE_LIMIT_ENABLED || complain) {
ROPTIONAL(RERROR, ERROR, "Last connection attempt failed, waiting %d seconds before retrying",
(int)((pool->state.last_failed + pool->retry_delay) - now));
}
return NULL;
}
/*
* We limit the rate of new connections after a failed attempt.
*/
if (pool->state.pending > pool->pending_window) {
pthread_mutex_unlock(&pool->mutex);
RATE_LIMIT(ROPTIONAL(RWARN, WARN, "Cannot open a new connection due to rate limit after failure"));
return NULL;
}
pool->state.pending++;
number = pool->state.count++;
/*
* Don't starve out the thread trying to reconnect
* the pool, by continuously opening new connections.
*/
while (pool->state.reconnecting) pthread_cond_wait(&pool->done_reconnecting, &pool->mutex);
/*
* Unlock the mutex while we try to open a new
* connection. If there are issues with the back-end,
* opening a new connection may take a LONG time. In
* that case, we want the other connections to continue
* to be used.
*/
pthread_mutex_unlock(&pool->mutex);
/*
* The true value for pending_window is the smaller of
* free connection slots, or pool->pending_window.
*/
pending_window = (pool->max - pool->state.num);
if (pool->pending_window < pending_window) pending_window = pool->pending_window;
ROPTIONAL(RDEBUG, DEBUG, "Opening additional connection (%" PRIu64 "), %u of %u pending slots used",
number, pool->state.pending, pending_window);
/*
* Allocate a new top level ctx for the create callback
* to hang its memory off of.
*/
ctx = talloc_init("fr_connection_ctx");
if (!ctx) return NULL;
/*
* This may take a long time, which prevents other
* threads from releasing connections. We don't care
* about other threads opening new connections, as we
* already have no free connections.
*/
conn = pool->create(ctx, pool->opaque, &pool->connect_timeout);
if (!conn) {
ROPTIONAL(RERROR, ERROR, "Opening connection failed (%" PRIu64 ")", number);
pool->state.last_failed = now;
pthread_mutex_lock(&pool->mutex);
pool->pending_window = 1;
pool->state.pending--;
/*
* Must be done inside the mutex, reconnect callback
* may modify args.
*/
fr_connection_trigger_exec(pool, request, "fail");
pthread_cond_broadcast(&pool->done_spawn);
pthread_mutex_unlock(&pool->mutex);
talloc_free(ctx);
return NULL;
}
/*
* And lock the mutex again while we link the new
* connection back into the pool.
*/
pthread_mutex_lock(&pool->mutex);
this = talloc_zero(pool, fr_connection_t);
if (!this) {
pthread_cond_broadcast(&pool->done_spawn);
pthread_mutex_unlock(&pool->mutex);
talloc_free(ctx);
return NULL;
}
fr_talloc_link_ctx(this, ctx);
this->created = now;
this->connection = conn;
this->in_use = in_use;
this->number = number;
gettimeofday(&this->last_reserved, NULL);
this->last_released = this->last_reserved;
/*
* The connection pool is starting up. Insert the
* connection into the heap.
*/
if (!in_use) fr_heap_insert(pool->heap, this);
fr_connection_link_head(pool, this);
/*
* Do NOT insert the connection into the heap. That's
* done when the connection is released.
*/
pool->state.num++;
rad_assert(pool->state.pending > 0);
pool->state.pending--;
/*
* We've successfully opened one more connection. Allow
* more connections to open in parallel.
*/
if ((pool->pending_window < pool->max) &&
((pool->max_pending == 0) || (pool->pending_window < pool->max_pending))) {
pool->pending_window++;
}
pool->state.last_spawned = time(NULL);
pool->delay_interval = pool->cleanup_interval;
pool->state.next_delay = pool->cleanup_interval;
pool->state.last_failed = 0;
/*
* Must be done inside the mutex, reconnect callback
* may modify args.
*/
fr_connection_trigger_exec(pool, request, "open");
pthread_cond_broadcast(&pool->done_spawn);
if (unlock) pthread_mutex_unlock(&pool->mutex);
return this;
}
/** Close an existing connection.
*
* Removes the connection from the list, calls the delete callback to close
* the connection, then frees memory allocated to the connection.
*
* @note Will call the 'close' trigger.
* @note Must be called with the mutex held.
*
* @param[in] pool to modify.
* @param[in] request The current request.
* @param[in] this Connection to delete.
*/
static void fr_connection_close_internal(fr_connection_pool_t *pool, REQUEST *request, fr_connection_t *this)
{
/*
* If it's in use, release it.
*/
if (this->in_use) {
#ifdef PTHREAD_DEBUG
pthread_t pthread_id = pthread_self();
rad_assert(pthread_equal(this->pthread_id, pthread_id) != 0);
#endif
this->in_use = false;
rad_assert(pool->state.active != 0);
pool->state.active--;
} else {
/*
* Connection isn't used, remove it from the heap.
*/
fr_heap_extract(pool->heap, this);
}
fr_connection_trigger_exec(pool, request, "close");
fr_connection_unlink(pool, this);
rad_assert(pool->state.num > 0);
pool->state.num--;
talloc_free(this);
}
/** Check whether a connection needs to be removed from the pool
*
* Will verify that the connection is within idle_timeout, max_uses, and
* lifetime values. If it is not, the connection will be closed.
*
* @note Will only close connections not in use.
* @note Must be called with the mutex held.
*
* @param[in] pool to modify.
* @param[in] request The current request.
* @param[in] this Connection to manage.
* @param[in] now Current time.
* @return
* - 0 if connection was closed.
* - 1 if connection handle was left open.
*/
static int fr_connection_manage(fr_connection_pool_t *pool,
REQUEST *request,
fr_connection_t *this,
time_t now)
{
rad_assert(pool != NULL);
rad_assert(this != NULL);
/*
* Don't terminated in-use connections
*/
if (this->in_use) return 1;
if (this->needs_reconnecting) {
ROPTIONAL(RDEBUG2, DEBUG2, "Closing expired connection (%" PRIu64 "): Needs reconnecting",
this->number);
do_delete:
if (pool->state.num <= pool->min) {
ROPTIONAL(RDEBUG2, DEBUG2, "You probably need to lower \"min\"");
}
fr_connection_close_internal(pool, request, this);
return 0;
}
if ((pool->max_uses > 0) &&
(this->num_uses >= pool->max_uses)) {
ROPTIONAL(RDEBUG2, DEBUG2, "Closing expired connection (%" PRIu64 "): Hit max_uses limit",
this->number);
goto do_delete;
}
if ((pool->lifetime > 0) &&
((this->created + pool->lifetime) < now)) {
ROPTIONAL(RDEBUG2, DEBUG2, "Closing expired connection (%" PRIu64 "): Hit lifetime limit",
this->number);
goto do_delete;
}
if ((pool->idle_timeout > 0) &&
((this->last_released.tv_sec + pool->idle_timeout) < now)) {
ROPTIONAL(RINFO, INFO, "Closing connection (%" PRIu64 "): Hit idle_timeout, was idle for %u seconds",
this->number, (int) (now - this->last_released.tv_sec));
goto do_delete;
}
return 1;
}
/** Check whether any connections need to be removed from the pool
*
* Maintains the number of connections in the pool as per the configuration
* parameters for the connection pool.
*
* @note Will only run checks the first time it's called in a given second,
* to throttle connection spawning/closing.
* @note Will only close connections not in use.
* @note Must be called with the mutex held, will release mutex before returning.
*
* @param[in] pool to manage.
* @param[in] request The current request.
* @return 1
*/
static int fr_connection_pool_check(fr_connection_pool_t *pool, REQUEST *request)
{
uint32_t spawn, idle, extra;
time_t now = time(NULL);
fr_connection_t *this, *next;
if (pool->state.last_checked == now) {
pthread_mutex_unlock(&pool->mutex);
return 1;
}
/*
* Some idle connections are OK, if they're within the
* configured "spare" range. Any extra connections
* outside of that range can be closed.
*/
idle = pool->state.num - pool->state.active;
if (idle <= pool->spare) {
extra = 0;
} else {
extra = idle - pool->spare;
}
/*
* The other end can close connections. If so, we'll
* have fewer than "min". When that happens, open more
* connections to enforce "min".
*/
if ((pool->state.num + pool->state.pending) < pool->min) {
spawn = pool->min - (pool->state.num + pool->state.pending);
extra = 0;
ROPTIONAL(RINFO, INFO, "Need %i more connections to reach min connections (%i)", spawn, pool->min);
/*
* If we're about to create more than "max",
* don't create more.
*/
} else if ((pool->state.num + pool->state.pending) >= pool->max) {
/*
* Ensure we don't spawn more connections. If
* there are extra idle connections, we can
* delete all of them.
*/
spawn = 0;
/* leave extra alone from above */
/*
* min < num < max
*
* AND we don't have enough idle connections.
* Open some more.
*/
} else if (idle < pool->spare) {
/*
* Not enough spare connections. Spawn a few.
* But cap the pool size at "max"
*/
spawn = pool->spare - idle;
extra = 0;
if ((pool->state.num + pool->state.pending + spawn) > pool->max) {
spawn = pool->max - (pool->state.num + pool->state.pending);
}
ROPTIONAL(RINFO, INFO, "Need %i more connections to reach %i spares", spawn, pool->spare);
/*
* min < num < max
*
* We have more than enough idle connections, AND
* some are pending. Don't open or close any.
*/
} else if (pool->state.pending) {
spawn = 0;
extra = 0;
/*
* We have too many idle connections, but closing
* some would take us below "min", so we only
* close enough to take us to "min".
*/
} else if ((pool->min + extra) >= pool->state.num) {
spawn = 0;
extra = pool->state.num - pool->min;
} else {
/*
* Closing the "extra" connections won't take us
* below "min". It's therefore safe to close
* them all.
*/
spawn = 0;
/* leave extra alone from above */
}
/*
* Only try to open spares if we're not already attempting to open
* a connection. Avoids spurious log messages.
*/
if (spawn) {
pthread_mutex_unlock(&pool->mutex);
(void) fr_connection_spawn(pool, request, now, false, true);
pthread_mutex_lock(&pool->mutex);
}
/*
* We haven't spawned connections in a while, and there
* are too many spare ones. Close the one which has been
* unused for the longest.
*/
if (extra && (now >= (pool->state.last_spawned + pool->delay_interval))) {
fr_connection_t *found = NULL;
for (this = pool->tail; this != NULL; this = this->prev) {
if (this->in_use) continue;
if (!found || (fr_timeval_cmp(&this->last_reserved, &found->last_reserved) < 0)) {
found = this;
}
}
if (!rad_cond_assert(found)) goto done;
ROPTIONAL(RDEBUG, DEBUG, "Closing connection (%" PRIu64 "), from %d unused connections",
found->number, extra);
fr_connection_close_internal(pool, request, found);
/*
* Decrease the delay for the next time we clean up.
*/
pool->state.next_delay >>= 1;
if (pool->state.next_delay == 0) pool->state.next_delay = 1;
pool->delay_interval += pool->state.next_delay;
}
/*
* Pass over all of the connections in the pool, limiting
* lifetime, idle time, max requests, etc.
*/
for (this = pool->head; this != NULL; this = next) {
next = this->next;
fr_connection_manage(pool, request, this, now);
}
pool->state.last_checked = now;
done:
pthread_mutex_unlock(&pool->mutex);
return 1;
}
/** Get a connection from the connection pool
*
* @note Must be called with the mutex free.
*
* @param[in] pool to reserve the connection from.
* @param[in] request The current request.
* @param[in] spawn whether to spawn a new connection
* @return
* - A pointer to the connection handle.
* - NULL on error.
*/
static void *fr_connection_get_internal(fr_connection_pool_t *pool, REQUEST *request, bool spawn)
{
time_t now;
fr_connection_t *this;
if (!pool) return NULL;
pthread_mutex_lock(&pool->mutex);
now = time(NULL);
/*
* Grab the link with the lowest latency, and check it
* for limits. If "connection manage" says the link is
* no longer usable, go grab another one.
*/
do {
this = fr_heap_peek(pool->heap);
if (!this) break;
} while (!fr_connection_manage(pool, request, this, now));
/*
* We have a working connection. Extract it from the
* heap and use it.
*/
if (this) {
fr_heap_extract(pool->heap, this);
goto do_return;
}
if (pool->state.num == pool->max) {
bool complain = false;
/*
* Rate-limit complaints.
*/
if (pool->state.last_at_max != now) {
complain = true;
pool->state.last_at_max = now;
}
pthread_mutex_unlock(&pool->mutex);
if (!RATE_LIMIT_ENABLED || complain) {
ROPTIONAL(RERROR, ERROR, "No connections available and at max connection limit");
/*
* Must be done inside the mutex, reconnect callback
* may modify args.
*/
fr_connection_trigger_exec(pool, request, "none");
}
return NULL;
}
pthread_mutex_unlock(&pool->mutex);
if (!spawn) return NULL;
ROPTIONAL(RDEBUG2, DEBUG2, "%i of %u connections in use. You may need to increase \"spare\"",
pool->state.active, pool->state.num);
/*
* Returns unlocked on failure, or locked on success
*/
this = fr_connection_spawn(pool, request, now, true, false);
if (!this) return NULL;
do_return:
pool->state.active++;
this->num_uses++;
gettimeofday(&this->last_reserved, NULL);
this->in_use = true;
#ifdef PTHREAD_DEBUG
this->pthread_id = pthread_self();
#endif
pthread_mutex_unlock(&pool->mutex);
ROPTIONAL(RDEBUG2, DEBUG2, "Reserved connection (%" PRIu64 ")", this->number);
return this->connection;
}
/** Enable triggers for a connection pool
*
* @param[in] pool to enable triggers for.
* @param[in] trigger_prefix prefix to prepend to all trigger names. Usually a path
* to the module's trigger configuration .e.g.
* @verbatim modules.<name>.pool @endverbatim
* @verbatim <trigger name> @endverbatim is appended to form
* the complete path.
* @param[in] trigger_args to make available in any triggers executed by the connection pool.
* These will usually be VALUE_PAIR (s) describing the host
* associated with the pool.
* Trigger args will be copied, input trigger_args should be freed
* if necessary.
*/
void fr_connection_pool_enable_triggers(fr_connection_pool_t *pool,
char const *trigger_prefix, VALUE_PAIR *trigger_args)
{
talloc_const_free(pool->trigger_prefix);
MEM(pool->trigger_prefix = trigger_prefix ? talloc_typed_strdup(pool, trigger_prefix) : "");
fr_pair_list_free(&pool->trigger_args);
if (!trigger_args) return;
MEM(pool->trigger_args = fr_pair_list_copy(pool, trigger_args));
}
/** Create a new connection pool
*
* Allocates structures used by the connection pool, initialises the various
* configuration options and counters, and sets the callback functions.
*
* Will also spawn the number of connections specified by the 'start' configuration
* option.
*
* @note Will call the 'start' trigger.
*
* @param[in] ctx Context to link pool's destruction to.
* @param[in] cs pool section.
* @param[in] opaque data pointer to pass to callbacks.
* @param[in] c Callback to create new connections.
* @param[in] a Callback to check the status of connections.
* @param[in] log_prefix prefix to prepend to all log messages.
* @return
* - New connection pool.
* - NULL on error.
*/
fr_connection_pool_t *fr_connection_pool_init(TALLOC_CTX *ctx,
CONF_SECTION const *cs,
void *opaque,
fr_connection_create_t c,
fr_connection_alive_t a,
char const *log_prefix)
{
uint32_t i;
fr_connection_pool_t *pool = NULL;
fr_connection_t *this;
time_t now;
if (!cs || !opaque || !c) return NULL;
now = time(NULL);
/*
* Pool is allocated in the NULL context as
* threads are likely to allocate memory
* beneath the pool.
*/
pool = talloc_zero(NULL, fr_connection_pool_t);
if (!pool) {
/* Simply using ERROR here results in a null pointer dereference */
fr_log(&default_log, L_ERR, "%s: Out of memory", __FUNCTION__);
return NULL;
}
/*
* Ensure the pool is freed at the same time
* as its parent.
*/
if (fr_talloc_link_ctx(ctx, pool) < 0) {
ERROR("%s: Failed linking pool ctx", __FUNCTION__);
talloc_free(pool);
return NULL;
}
pool->cs = cs;
pool->opaque = opaque;
pool->create = c;
pool->alive = a;
pool->head = pool->tail = NULL;
/*
* We keep a heap of connections, sorted by the last time
* we STARTED using them. Newly opened connections
* aren't in the heap. They're only inserted in the list
* once they're released.
*
* We do "most recently started" instead of "most
* recently used", because MRU is done as most recently
* *released*. We want to order connections by
* responsiveness, and MRU prioritizes high latency
* connections.