Skip to content

Commit

Permalink
Implement server lifetime (#129)
Browse files Browse the repository at this point in the history
  • Loading branch information
x4m authored Feb 28, 2020
1 parent 5f7e3ea commit b7bcb86
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 2 deletions.
7 changes: 7 additions & 0 deletions odyssey.conf
Original file line number Diff line number Diff line change
Expand Up @@ -511,6 +511,13 @@ database default {

application_name_add_host yes

#
# Server lifetime - maximum number of seconds for a server connetion to live. Prevents cache bloat.
# Defaults to 3600 (1 hour)
#

server_lifetime 3600

#
# Enable verbose mode for a specific route only.
#
Expand Down
10 changes: 10 additions & 0 deletions sources/config_reader.c
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ enum
OD_LCLIENT_LOGIN_TIMEOUT,
OD_LCLIENT_FWD_ERROR,
OD_LAPPLICATION_NAME_ADD_HOST,
OD_LSERVER_LIFETIME,
OD_LTLS,
OD_LTLS_CA_FILE,
OD_LTLS_KEY_FILE,
Expand Down Expand Up @@ -159,6 +160,7 @@ od_config_keywords[] =
od_keyword("client_login_timeout", OD_LCLIENT_LOGIN_TIMEOUT),
od_keyword("client_fwd_error", OD_LCLIENT_FWD_ERROR),
od_keyword("application_name_add_host", OD_LAPPLICATION_NAME_ADD_HOST),
od_keyword("server_lifetime", OD_LSERVER_LIFETIME),
od_keyword("tls", OD_LTLS),
od_keyword("tls_ca_file", OD_LTLS_CA_FILE),
od_keyword("tls_key_file", OD_LTLS_KEY_FILE),
Expand Down Expand Up @@ -756,6 +758,14 @@ od_config_reader_route(od_config_reader_t *reader, char *db_name, int db_name_le
if (! od_config_reader_yes_no(reader, &route->application_name_add_host))
return -1;
continue;
/* server_lifetime */
case OD_LSERVER_LIFETIME: {
int server_lifetime;
if (!od_config_reader_number(reader, &server_lifetime))
return -1;
route->server_lifetime_us = server_lifetime * 1000000L;
}
continue;
/* pool */
case OD_LPOOL:
if (! od_config_reader_string(reader, &route->pool_sz))
Expand Down
9 changes: 7 additions & 2 deletions sources/router.c
Original file line number Diff line number Diff line change
Expand Up @@ -101,9 +101,13 @@ od_router_expire_server_tick_cb(od_server_t *server, void **argv)
od_route_t *route = server->route;
od_list_t *expire_list = argv[0];
int *count = argv[1];
uint64_t *now_us = argv[2];

uint64_t lifetime = route->rule->server_lifetime_us;
uint64_t server_life = *now_us - server->init_time_us;

/* advance idle time for 1 sec */
if (server->idle_time < route->rule->pool_ttl) {
if (server_life < lifetime && server->idle_time < route->rule->pool_ttl) {
server->idle_time++;
return 0;
}
Expand Down Expand Up @@ -160,7 +164,8 @@ int
od_router_expire(od_router_t *router, od_list_t *expire_list)
{
int count = 0;
void *argv[] = { expire_list, &count };
uint64_t now_us = machine_time_us();
void *argv[] = { expire_list, &count, &now_us };
od_router_foreach(router, od_router_expire_cb, argv);
return count;
}
Expand Down
1 change: 1 addition & 0 deletions sources/rules.c
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,7 @@ od_rules_add(od_rules_t *rules)
rule->refs = 0;
rule->auth_common_name_default = 0;
rule->auth_common_names_count = 0;
rule->server_lifetime_us = 3600 * 1000000L;
od_list_init(&rule->auth_common_names);
od_list_init(&rule->link);
od_list_append(&rules->rules, &rule->link);
Expand Down
1 change: 1 addition & 0 deletions sources/rules.h
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ struct od_rule
int log_debug;
double *quantiles;
int quantiles_count;
uint64_t server_lifetime_us;
od_list_t link;
};

Expand Down
2 changes: 2 additions & 0 deletions sources/server.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ struct od_server
void *client;
void *route;
od_global_t *global;
uint64_t init_time_us;
od_list_t link;
};

Expand All @@ -57,6 +58,7 @@ od_server_init(od_server_t *server)
server->deploy_sync = 0;
server->sync_request = 0;
server->sync_reply = 0;
server->init_time_us = machine_time_us();
server->error_connect = NULL;
od_stat_state_init(&server->stats_state);
od_scram_state_init(&server->scram_state);
Expand Down

0 comments on commit b7bcb86

Please sign in to comment.