The FreeRADIUS server
$Id: 15bac2a4c627c01d1aa2047687b3418955ac7f00 $
|
Handle pools of connections (threads, sockets, etc.) More...
#include <freeradius-devel/server/main_config.h>
#include <freeradius-devel/server/modpriv.h>
#include <freeradius-devel/server/trigger.h>
#include <freeradius-devel/util/debug.h>
#include <freeradius-devel/util/heap.h>
#include <freeradius-devel/util/misc.h>
#include <time.h>
Go to the source code of this file.
Data Structures | |
struct | fr_pool_connection_s |
An individual connection within the connection pool. More... | |
struct | fr_pool_s |
A connection pool. More... | |
Macros | |
#define | LOG_PREFIX pool->log_prefix |
Typedefs | |
typedef struct fr_pool_connection_s | fr_pool_connection_t |
Functions | |
static int | connection_check (fr_pool_t *pool, request_t *request) |
Check whether any connections need to be removed from the pool. More... | |
static void | connection_close_internal (fr_pool_t *pool, fr_pool_connection_t *this) |
Close an existing connection. More... | |
static fr_pool_connection_t * | connection_find (fr_pool_t *pool, void *conn) |
Find a connection handle in the connection list. More... | |
static void * | connection_get_internal (fr_pool_t *pool, request_t *request, bool spawn) |
Get a connection from the connection pool. More... | |
static void | connection_link_head (fr_pool_t *pool, fr_pool_connection_t *this) |
Adds a connection to the head of the connection list. More... | |
static int | connection_manage (fr_pool_t *pool, request_t *request, fr_pool_connection_t *this, fr_time_t now) |
Check whether a connection needs to be removed from the pool. More... | |
static fr_pool_connection_t * | connection_spawn (fr_pool_t *pool, request_t *request, fr_time_t now, bool in_use, bool unlock) |
Spawns a new connection. More... | |
static void | connection_unlink (fr_pool_t *pool, fr_pool_connection_t *this) |
Removes a connection from the connection list. More... | |
int | fr_pool_connection_close (fr_pool_t *pool, request_t *request, void *conn) |
Delete a connection from the connection pool. More... | |
void * | fr_pool_connection_get (fr_pool_t *pool, request_t *request) |
Reserve a connection in the connection pool. More... | |
void * | fr_pool_connection_reconnect (fr_pool_t *pool, request_t *request, void *conn) |
Reconnect a suspected inviable connection. More... | |
void | fr_pool_connection_release (fr_pool_t *pool, request_t *request, void *conn) |
Release a connection. More... | |
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. More... | |
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. More... | |
void | fr_pool_free (fr_pool_t *pool) |
Delete a connection pool. More... | |
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. More... | |
void const * | fr_pool_opaque (fr_pool_t *pool) |
Return the opaque data associated with a connection pool. More... | |
int | fr_pool_reconnect (fr_pool_t *pool, request_t *request) |
Mark connections for reconnection, and spawn at least 'start' connections. More... | |
void | fr_pool_reconnect_func (fr_pool_t *pool, fr_pool_reconnect_t reconnect) |
Set a reconnection callback for the connection pool. More... | |
void | fr_pool_ref (fr_pool_t *pool) |
Increment pool reference by one. More... | |
int | fr_pool_start (fr_pool_t *pool) |
int | fr_pool_start_num (fr_pool_t *pool) |
Connection pool get start. More... | |
fr_pool_state_t const * | fr_pool_state (fr_pool_t *pool) |
Get the number of connections currently in the pool. More... | |
fr_time_delta_t | fr_pool_timeout (fr_pool_t *pool) |
Connection pool get timeout. More... | |
static void | fr_pool_trigger_exec (fr_pool_t *pool, char const *event) |
Send a connection pool trigger. More... | |
static int8_t | last_released_cmp (void const *one, void const *two) |
Order connections by released longest ago. More... | |
static int8_t | last_reserved_cmp (void const *one, void const *two) |
Order connections by reserved most recently. More... | |
static int | max_dflt (CONF_PAIR **out, UNUSED void *parent, CONF_SECTION *cs, fr_token_t quote, conf_parser_t const *rule) |
static int | max_dflt (CONF_PAIR **out, void *parent, CONF_SECTION *cs, fr_token_t quote, conf_parser_t const *rule) |
Variables | |
static const conf_parser_t | pool_config [] |
Handle pools of connections (threads, sockets, etc.)
Definition in file pool.c.
struct fr_pool_connection_s |
An individual connection within the connection pool.
Defines connection counters, timestamps, and holds a pointer to the connection handle itself.
Data Fields | ||
---|---|---|
void * | connection | Pointer to whatever the module uses for a connection handle. |
fr_time_t | created | Time connection was created. |
fr_heap_index_t | heap_id | For the next connection heap. |
bool | in_use | Whether the connection is currently reserved. |
fr_time_t | last_released | Time the connection was released. |
fr_time_t | last_reserved | Last time the connection was reserved. |
bool | needs_reconnecting | Reconnect this connection before use. |
fr_pool_connection_t * | next | Next connection in list. |
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. |
fr_pool_connection_t * | prev | Previous connection in list. |
struct fr_pool_s |
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.
Data Fields | ||
---|---|---|
fr_pool_connection_alive_t | alive | Function used to check status of connections. |
fr_time_delta_t | cleanup_interval |
Initial timer for how often we sweep the pool for free connections. (0 is infinite). |
fr_time_delta_t | connect_timeout | New connection timeout, enforced by the create callback. |
fr_pool_connection_create_t | create | Function used to create new connections. |
CONF_SECTION const * | cs | Configuration section holding the section of parsed config file that relates to this pool. |
fr_time_delta_t | delay_interval |
When we next do a cleanup. Initialized to cleanup_interval, and increase from there based on the delay. |
pthread_cond_t | done_reconnecting | Before calling the create callback, threads should block on this condition if reconnecting == true. |
pthread_cond_t | done_spawn | Threads that need to ensure no spawning is in progress, should block on this condition if pending != 0. |
fr_pool_connection_t * | head | Start of the connection list. |
fr_heap_t * | heap | For the next connection heap. |
fr_time_delta_t | held_trigger_max | If a connection is held for longer than the specified period, fire a trigger. |
fr_time_delta_t | held_trigger_min | If a connection is held for less than the specified period, fire a trigger. |
fr_time_delta_t | idle_timeout | How long a connection can be idle before being closed. |
fr_time_delta_t | lifetime | How long a connection can be open before being closed (irrespective of whether it's idle or not). |
char const * | log_prefix | Log prefix to prepend to all log messages created by the connection pool code. |
uint32_t | max | Maximum number of concurrent connections to allow. |
uint32_t | max_pending | Max number of pending connections to allow. |
uint64_t | max_uses | Maximum number of times a connection can be used before being closed. |
uint32_t | min | Minimum number of concurrent connections to keep open. |
pthread_mutex_t | mutex | Mutex used to keep consistent state when making modifications in threaded mode. |
void * | opaque | Pointer to context data that will be passed to callbacks. |
uint32_t | pending_window | Sliding window of pending connections. |
fr_pool_reconnect_t | reconnect | Called during connection pool reconnect. |
int | ref | Reference counter to prevent connection pool being freed multiple times. |
fr_time_delta_t | retry_delay | seconds to delay re-open after a failed open. |
uint32_t | spare | Number of spare connections to try. |
bool | spread | If true we spread requests over the connections, using the connection released longest ago, first. |
uint32_t | start | Number of initial connections. |
fr_pool_state_t | state | Stats and state of the connection pool. |
fr_pool_connection_t * | tail | End of the connection list. |
fr_pair_list_t | trigger_args | Arguments to make available in connection pool triggers. |
char const * | trigger_prefix | Prefix to prepend to names of all triggers fired by the connection pool code. |
bool | triggers_enabled | Whether we call the trigger functions. |
typedef struct fr_pool_connection_s fr_pool_connection_t |
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.
[in] | pool | to manage. |
[in] | request | The current request. |
Definition at line 657 of file pool.c.
|
static |
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.
[in] | pool | to modify. |
[in] | this | Connection to delete. |
Definition at line 549 of file pool.c.
|
static |
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.
[in] | pool | to search in. |
[in] | conn | handle to search for. |
Definition at line 289 of file pool.c.
Get a connection from the connection pool.
[in] | pool | to reserve the connection from. |
[in] | request | The current request. |
[in] | spawn | whether to spawn a new connection |
Definition at line 836 of file pool.c.
|
static |
|
static |
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.
[in] | pool | to modify. |
[in] | request | The current request. |
[in] | this | Connection to manage. |
[in] | now | Current time. |
Definition at line 597 of file pool.c.
|
static |
Spawns a new connection.
Spawns a new connection using the create callback, and returns it for adding to the connection list.
[in] | pool | to modify. |
[in] | request | The current request. |
[in] | now | Current time. |
[in] | in_use | whether the new connection should be "in_use" or not |
[in] | unlock | whether we should unlock the mutex before returning |
Definition at line 337 of file pool.c.
|
static |
Delete a connection from the connection pool.
Resolves the connection handle to a connection, then (if found) closes, unlinks and frees that connection.
[in] | pool | Connection pool to modify. |
[in] | request | The current request. |
[in] | conn | to delete. |
Definition at line 1537 of file pool.c.
Reserve a connection in the connection pool.
Will attempt to find an unused connection in the connection pool, if one is found, will mark it as in in use increment the number of active connections and return the connection handle.
If no free connections are found will attempt to spawn a new one, conditional on a connection spawning not already being in progress, and not being at the 'max' connection limit.
[in] | pool | to reserve the connection from. |
[in] | request | The current request. |
Definition at line 1392 of file pool.c.
Reconnect a suspected inviable connection.
This should be called by the module if it suspects that a connection is not viable (e.g. the server has closed it).
When implementing a module that uses the connection pool API, it is advisable to pass a pointer to the pointer to the handle (void **conn) to all functions which may call reconnect. This is so that if a new handle is created and returned, the handle pointer can be updated up the callstack, and a function higher up the stack doesn't attempt to use a now invalid connection handle.
[in] | pool | to reconnect the connection in. |
[in] | request | The current request. |
[in] | conn | to reconnect. |
Definition at line 1500 of file pool.c.
Release a connection.
Will mark a connection as unused and decrement the number of active connections.
[in] | pool | to release the connection in. |
[in] | request | The current request. |
[in] | conn | to release. |
Definition at line 1407 of file pool.c.
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.
[in] | pool | to enable triggers for. |
[in] | trigger_prefix | prefix to prepend to all trigger names. Usually a path to the module's trigger configuration .e.g. modules.<name>.pool <trigger name>is appended to form the complete path. |
[in] | trigger_args | to make available in any triggers executed by the connection pool. These will usually be fr_pair_t (s) describing the host associated with the pool. Trigger args will be copied, input trigger_args should be freed if necessary. |
Definition at line 933 of file pool.c.
void fr_pool_free | ( | fr_pool_t * | pool | ) |
Delete a connection pool.
Closes, unlinks and frees all connections in the connection pool, then frees all memory used by the connection pool.
[in,out] | pool | to delete. |
Definition at line 1329 of file pool.c.
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.
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.
[in] | ctx | Context to link pool's destruction to. |
[in] | cs | pool section. |
[in] | opaque | data pointer to pass to callbacks. |
[in] | c | Callback to create new connections. |
[in] | a | Callback to check the status of connections. |
[in] | log_prefix | prefix to prepend to all log messages. |
Definition at line 967 of file pool.c.
void const* fr_pool_opaque | ( | fr_pool_t * | pool | ) |
Mark connections for reconnection, and spawn at least 'start' connections.
This intended to be called on a connection pool that's in use, to have it reflect a configuration change, or because the administrator knows that all connections in the pool are inviable and need to be reconnected.
[in] | pool | to reconnect. |
[in] | request | The current request. |
Definition at line 1244 of file pool.c.
void fr_pool_reconnect_func | ( | fr_pool_t * | pool, |
fr_pool_reconnect_t | reconnect | ||
) |
Set a reconnection callback for the connection pool.
This can be called at any time during the pool's lifecycle.
[in] | pool | to set reconnect callback for. |
reconnect | callback to call when reconnecting pool's connections. |
Definition at line 1224 of file pool.c.
void fr_pool_ref | ( | fr_pool_t * | pool | ) |
int fr_pool_start | ( | fr_pool_t * | pool | ) |
int fr_pool_start_num | ( | fr_pool_t * | pool | ) |
fr_pool_state_t const* fr_pool_state | ( | fr_pool_t * | pool | ) |
fr_time_delta_t fr_pool_timeout | ( | fr_pool_t * | pool | ) |
|
inlinestatic |
|
static |
|
static |
|
static |
|
static |
|
static |