Skip to content

Commit

Permalink
add and use idle_timeout configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
alandekok committed Dec 26, 2024
1 parent 07e6b87 commit c966f70
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 0 deletions.
39 changes: 39 additions & 0 deletions src/lib/server/trunk.c
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,7 @@ conf_parser_t const trunk_config[] = {
{ FR_CONF_OFFSET("connecting", trunk_conf_t, connecting), .dflt = "2" },
{ FR_CONF_OFFSET("uses", trunk_conf_t, max_uses), .dflt = "0" },
{ FR_CONF_OFFSET("lifetime", trunk_conf_t, lifetime), .dflt = "0" },
{ FR_CONF_OFFSET("idle_timeout", trunk_conf_t, idle_timeout), .dflt = "0" },

{ FR_CONF_OFFSET("open_delay", trunk_conf_t, open_delay), .dflt = "0.2" },
{ FR_CONF_OFFSET("close_delay", trunk_conf_t, close_delay), .dflt = "10.0" },
Expand Down Expand Up @@ -4167,6 +4168,34 @@ static void trunk_manage(trunk_t *trunk, fr_time_t now)
while ((treq = fr_dlist_tail(&trunk->free_requests)) &&
fr_time_lteq(fr_time_add(treq->last_freed, trunk->conf.req_cleanup_delay), now)) talloc_free(treq);

/*
* If we have idle connections, then close them.
*/
if (fr_time_delta_ispos(trunk->conf.idle_timeout)) {
fr_minmax_heap_iter_t iter;
fr_time_t idle_cutoff = fr_time_sub(now, trunk->conf.idle_timeout);

for (tconn = fr_minmax_heap_iter_init(trunk->active, &iter);
tconn;
tconn = fr_minmax_heap_iter_next(trunk->active, &iter)) {
/*
* The connection has outstanding requests without replies, don't do anything.
*/
if (fr_heap_num_elements(tconn->pending) > 0) continue;

/*
* The connection was last active after the idle cutoff time, don't do anything.
*/
if (fr_time_gt(tconn->pub.last_write_success, idle_cutoff)) continue;

/*
* This connection has been inactive since before the idle timeout. Drain it,
* and free it.
*/
trunk_connection_enter_draining_to_free(tconn);
}
}

/*
* Free any connections which have drained
* and we didn't reactivate during the last
Expand Down Expand Up @@ -4736,6 +4765,16 @@ int trunk_start(trunk_t *trunk)
if (trunk_connection_spawn(trunk, fr_time()) != 0) return -1;
}

/*
* If the idle timeout is set, AND there's no management interval, OR the management interval is
* less than the idle timeout, update the management interval.
*/
if (fr_time_delta_ispos(trunk->conf.idle_timeout) &&
(!fr_time_delta_ispos(trunk->conf.manage_interval) ||
fr_time_delta_gt(trunk->conf.manage_interval, trunk->conf.idle_timeout))) {
trunk->conf.manage_interval = trunk->conf.idle_timeout;
}

if (fr_time_delta_ispos(trunk->conf.manage_interval)) {
/*
* Insert the event timer to manage
Expand Down
2 changes: 2 additions & 0 deletions src/lib/server/trunk.h
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,8 @@ typedef struct {

fr_time_delta_t lifetime; //!< Time between reconnects.

fr_time_delta_t idle_timeout; //!< how long a connection can remain idle for

fr_time_delta_t open_delay; //!< How long we must be above target utilisation
///< to spawn a new connection.

Expand Down

0 comments on commit c966f70

Please sign in to comment.