The FreeRADIUS server  $Id: 15bac2a4c627c01d1aa2047687b3418955ac7f00 $
pool.h
Go to the documentation of this file.
1 #pragma once
2 /*
3  * This program is free software; you can redistribute it and/or modify
4  * it under the terms of the GNU General Public License as published by
5  * the Free Software Foundation; either version 2 of the License, or
6  * (at your option) any later version.
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  * GNU General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this program; if not, write to the Free Software
15  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
16  */
17 
18 /**
19  * $Id: 7f02e5682a89ad8d15be72afd8331064df14611d $
20  *
21  * @file lib/server/pool.h
22  * @brief API to manage pools of persistent connections to external resources.
23  *
24  * @copyright 2012 The FreeRADIUS server project
25  * @copyright 2012 Alan DeKok (aland@deployingradius.com)
26  */
27 RCSIDH(pool_h, "$Id: 7f02e5682a89ad8d15be72afd8331064df14611d $")
28 
29 #ifdef __cplusplus
30 extern "C" {
31 #endif
32 
33 typedef struct fr_pool_s fr_pool_t;
34 typedef struct fr_pool_state_s fr_pool_state_t;
35 
36 #ifdef __cplusplus
37 }
38 #endif
39 
40 #include <freeradius-devel/server/stats.h>
41 
42 #ifdef __cplusplus
43 extern "C" {
44 #endif
45 
47  uint32_t pending; //!< Number of pending open connections.
48  fr_time_t last_checked; //!< Last time we pruned the connection pool.
49  fr_time_t last_spawned; //!< Last time we spawned a connection.
50  fr_time_t last_failed; //!< Last time we tried to spawn a connection but failed.
51  fr_time_t last_throttled; //!< Last time we refused to spawn a connection because
52  //!< the last connection failed, or we were already spawning
53  //!< a connection.
54  fr_time_t last_at_max; //!< Last time we hit the maximum number of allowed
55  //!< connections.
56  fr_time_t last_released; //!< Last time a connection was released.
57  fr_time_t last_closed; //!< Last time a connection was closed.
58 
59  fr_time_t last_held_min; //!< Last time we warned about a low latency event.
60  fr_time_t last_held_max; //!< Last time we warned about a high latency event.
61 
62  fr_time_delta_t next_delay; //!< The next delay time. cleanup. Initialized to
63  //!< cleanup_interval, and decays from there.
64 
65  uint64_t count; //!< Number of connections spawned over the lifetime
66  //!< of the pool.
67  uint32_t num; //!< Number of connections in the pool.
68  uint32_t active; //!< Number of currently reserved connections.
69 
70  bool reconnecting; //!< We are currently reconnecting the pool.
71 };
72 
73 /** Alter the opaque data of a connection pool during reconnection event
74  *
75  * This function will be called whenever we have been signalled to
76  * reconnect all the connections in a pool.
77  *
78  * It is called at a point where we have determined that no connection
79  * spawning is in progress, so it is safe to modify any pointers or
80  * memory associated with the opaque data.
81  *
82  * @param[in] pool being reconnected.
83  * @param[in] opaque pointer passed to fr_pool_init.
84  */
85 typedef void (*fr_pool_reconnect_t)(fr_pool_t *pool, void *opaque);
86 
87 /** Create a new connection handle
88  *
89  * This function will be called whenever the connection pool manager needs
90  * to spawn a new connection, and on reconnect.
91  *
92  * Memory should be talloced in the provided context to hold the module's
93  * connection structure. The context is allocated in the NULL context,
94  * but will be freed when fr_pool_t is freed via some internal magic.
95  *
96  * There is no delete callback, so operations such as closing sockets and
97  * freeing library connection handles should be done by a destructor attached
98  * to memory allocated beneath the provided ctx.
99  *
100  * @note A function pointer matching this prototype must be passed
101  * to fr_pool_init.
102  *
103  * @param[in,out] ctx to allocate memory in.
104  * @param[in] opaque pointer passed to fr_pool_init.
105  * @param[in] timeout The maximum time in ms the function has to complete
106  * the connection. Should be enforced by the function.
107  * @return
108  * - NULL on error.
109  * - A connection handle on success.
110  */
111 typedef void *(*fr_pool_connection_create_t)(TALLOC_CTX *ctx, void *opaque, fr_time_delta_t timeout);
112 
113 /** Check a connection handle is still viable
114  *
115  * Should check the state of a connection handle.
116  *
117  * @note NULL may be passed to fr_pool_init, if there is no way to check
118  * the state of a connection handle.
119  * @note Not currently use by connection pool manager.
120  * @param[in] opaque pointer passed to fr_pool_init.
121  * @param[in] connection handle returned by fr_pool_connection_create_t.
122  * @return
123  * - 0 on success.
124  * - < 0 on error or if the connection is unusable.
125  */
126 typedef int (*fr_pool_connection_alive_t)(void *opaque, void *connection);
127 
128 /*
129  * Pool allocation/initialisation
130  */
131 fr_pool_t *fr_pool_init(TALLOC_CTX *ctx,
132  CONF_SECTION const *cs,
133  void *opaque,
136  char const *log_prefix);
137 int fr_pool_start(fr_pool_t *pool);
138 
139 fr_pool_t *fr_pool_copy(TALLOC_CTX *ctx, fr_pool_t *pool, void *opaque);
140 
141 
142 /*
143  * Pool get/set
144  */
146  char const *trigger_prefix, fr_pair_list_t *trigger_args);
147 
149 
150 int fr_pool_start_num(fr_pool_t *pool);
151 
152 void const *fr_pool_opaque(fr_pool_t *pool);
153 
154 void fr_pool_ref(fr_pool_t *pool);
155 
157 
159 
160 /*
161  * Pool management
162  */
163 int fr_pool_reconnect(fr_pool_t *pool, request_t *request);
164 
165 void fr_pool_free(fr_pool_t *pool);
166 
167 /*
168  * Connection lifecycle
169  */
170 CC_ACQUIRE_HANDLE("conn_pool_handle")
171 void *fr_pool_connection_get(fr_pool_t *pool, request_t *request);
172 
174  CC_RELEASE_HANDLE("conn_pool_handle") void *conn);
175 
177  CC_RELEASE_HANDLE("conn_pool_handle") void *conn);
178 
180  request_t *request, CC_RELEASE_HANDLE("conn_pool_handle") void *conn);
181 
182 #ifdef __cplusplus
183 }
184 #endif
#define CC_RELEASE_HANDLE(_tag)
Definition: build.h:396
#define RCSIDH(h, id)
Definition: build.h:445
#define CC_ACQUIRE_HANDLE(_tag)
Definition: build.h:394
A section grouping multiple CONF_PAIR.
Definition: cf_priv.h:89
static fr_time_delta_t timeout
Definition: dhcpclient.c:54
unsigned int uint32_t
Definition: merged_model.c:33
A connection pool.
Definition: pool.c:85
fr_time_t last_failed
Last time we tried to spawn a connection but failed.
Definition: pool.h:50
fr_time_delta_t fr_pool_timeout(fr_pool_t *pool)
Connection pool get timeout.
Definition: pool.c:1181
int fr_pool_start(fr_pool_t *pool)
Definition: pool.c:1114
fr_time_t last_closed
Last time a connection was closed.
Definition: pool.h:57
fr_time_t last_throttled
Last time we refused to spawn a connection because the last connection failed, or we were already spa...
Definition: pool.h:51
uint32_t active
Number of currently reserved connections.
Definition: pool.h:68
fr_time_t last_released
Last time a connection was released.
Definition: pool.h:56
void const * fr_pool_opaque(fr_pool_t *pool)
Return the opaque data associated with a connection pool.
Definition: pool.c:1201
fr_time_delta_t next_delay
The next delay time.
Definition: pool.h:62
void fr_pool_free(fr_pool_t *pool)
Delete a connection pool.
Definition: pool.c:1327
void fr_pool_ref(fr_pool_t *pool)
Increment pool reference by one.
Definition: pool.c:1210
fr_pool_state_t const * fr_pool_state(fr_pool_t *pool)
Get the number of connections currently in the pool.
Definition: pool.c:1171
int fr_pool_reconnect(fr_pool_t *pool, request_t *request)
Mark connections for reconnection, and spawn at least 'start' connections.
Definition: pool.c:1242
void * fr_pool_connection_reconnect(fr_pool_t *pool, request_t *request, CC_RELEASE_HANDLE("conn_pool_handle") void *conn)
fr_time_t last_checked
Last time we pruned the connection pool.
Definition: pool.h:48
void * fr_pool_connection_get(fr_pool_t *pool, request_t *request)
Reserve a connection in the connection pool.
Definition: pool.c:1390
void(* fr_pool_reconnect_t)(fr_pool_t *pool, void *opaque)
Alter the opaque data of a connection pool during reconnection event.
Definition: pool.h:85
uint64_t count
Number of connections spawned over the lifetime of the pool.
Definition: pool.h:65
fr_time_t last_held_min
Last time we warned about a low latency event.
Definition: pool.h:59
fr_time_t last_at_max
Last time we hit the maximum number of allowed connections.
Definition: pool.h:54
void *(* fr_pool_connection_create_t)(TALLOC_CTX *ctx, void *opaque, fr_time_delta_t timeout)
Create a new connection handle.
Definition: pool.h:111
uint32_t pending
Number of pending open connections.
Definition: pool.h:47
void fr_pool_connection_release(fr_pool_t *pool, request_t *request, CC_RELEASE_HANDLE("conn_pool_handle") void *conn)
fr_time_t last_spawned
Last time we spawned a connection.
Definition: pool.h:49
fr_time_t last_held_max
Last time we warned about a high latency event.
Definition: pool.h:60
bool reconnecting
We are currently reconnecting the pool.
Definition: pool.h:70
uint32_t num
Number of connections in the pool.
Definition: pool.h:67
int(* fr_pool_connection_alive_t)(void *opaque, void *connection)
Check a connection handle is still viable.
Definition: pool.h:126
int fr_pool_start_num(fr_pool_t *pool)
Connection pool get start.
Definition: pool.c:1191
void fr_pool_enable_triggers(fr_pool_t *pool, char const *trigger_prefix, fr_pair_list_t *trigger_args)
Enable triggers for a connection pool.
Definition: pool.c:931
fr_pool_t * fr_pool_init(TALLOC_CTX *ctx, CONF_SECTION const *cs, void *opaque, fr_pool_connection_create_t c, fr_pool_connection_alive_t a, char const *log_prefix)
Create a new connection pool.
Definition: pool.c:965
fr_pool_t * fr_pool_copy(TALLOC_CTX *ctx, fr_pool_t *pool, void *opaque)
Allocate a new pool using an existing one as a template.
Definition: pool.c:1154
int fr_pool_connection_close(fr_pool_t *pool, request_t *request, CC_RELEASE_HANDLE("conn_pool_handle") void *conn)
void fr_pool_reconnect_func(fr_pool_t *pool, fr_pool_reconnect_t reconnect)
Set a reconnection callback for the connection pool.
Definition: pool.c:1222
A time delta, a difference in time measured in nanoseconds.
Definition: time.h:80
"server local" time.
Definition: time.h:69