From: Cody Cutrer Date: Wed, 28 Aug 2013 00:27:55 +0000 (-0600) Subject: simplify oldest connection logic X-Git-Tag: pgbouncer_1_6_rc1~20^2~1 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=387e83489532a368474273b941d4e51cf4cdfd80;p=pgbouncer simplify oldest connection logic --- diff --git a/include/objects.h b/include/objects.h index 509749d..b18148b 100644 --- a/include/objects.h +++ b/include/objects.h @@ -32,7 +32,7 @@ extern struct Slab *iobuf_cache; PgDatabase *find_database(const char *name); PgUser *find_user(const char *name); PgPool *get_pool(PgDatabase *, PgUser *); -void check_oldest_connection(PgSocket **oldest_connection, PgSocket *connection); +PgSocket *compare_connections_by_time(PgSocket *lhs, PgSocket *rhs); bool evict_connection(PgDatabase *db) _MUSTCHECK; bool find_server(PgSocket *client) _MUSTCHECK; bool release_server(PgSocket *server) /* _MUSTCHECK */; diff --git a/src/objects.c b/src/objects.c index 6550f40..904d8f8 100644 --- a/src/objects.c +++ b/src/objects.c @@ -976,17 +976,13 @@ static void dns_connect(struct PgSocket *server) connect_server(server, sa, sa_len); } -void check_oldest_connection(PgSocket **oldest_connection, PgSocket *connection) +PgSocket *compare_connections_by_time(PgSocket *lhs, PgSocket *rhs) { - if (!connection) - return; - if (!*oldest_connection) { - *oldest_connection = connection; - return; - } - if (connection->request_time < (*oldest_connection)->request_time) { - *oldest_connection = connection; - } + if (!lhs) + return rhs; + if (!rhs) + return lhs; + return lhs->request_time < rhs->request_time ? lhs : rhs; } /* evict the single most idle connection from among all pools to make room in the db */ @@ -1000,11 +996,11 @@ bool evict_connection(PgDatabase *db) pool = container_of(item, PgPool, head); if (pool->db != db) continue; - check_oldest_connection(&oldest_connection, last_socket(&pool->idle_server_list)); + oldest_connection = compare_connections_by_time(oldest_connection, last_socket(&pool->idle_server_list)); // only evict testing connections if nobody's waiting if (statlist_empty(&pool->waiting_client_list)) { - check_oldest_connection(&oldest_connection, last_socket(&pool->used_server_list)); - check_oldest_connection(&oldest_connection, last_socket(&pool->tested_server_list)); + oldest_connection = compare_connections_by_time(oldest_connection, last_socket(&pool->used_server_list)); + oldest_connection = compare_connections_by_time(oldest_connection, last_socket(&pool->tested_server_list)); } }