From d0bdb1912ec24f261e7f40bd5fb341ecb9729c3b Mon Sep 17 00:00:00 2001 From: Yoshiyuki Asaba Date: Tue, 6 Feb 2007 02:11:07 +0000 Subject: [PATCH] Add checking socket state when connection pool is reused. If socket is closed, pgpool try to create new connection pool. --- pool_connection_pool.c | 50 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) diff --git a/pool_connection_pool.c b/pool_connection_pool.c index d63a37f..3abac3a 100644 --- a/pool_connection_pool.c +++ b/pool_connection_pool.c @@ -44,6 +44,7 @@ POOL_CONNECTION_POOL *pool_connection_pool; /* connection pool */ static POOL_CONNECTION_POOL_SLOT *create_cp(POOL_CONNECTION_POOL_SLOT *cp, int secondary_backend); static POOL_CONNECTION_POOL *new_connection(POOL_CONNECTION_POOL *p); +static int check_socket_status(int fd); /* * initialize connection pools. this should be called once at the startup. @@ -95,6 +96,24 @@ POOL_CONNECTION_POOL *pool_get_cp(char *user, char *database, int protoMajor) /* mark this connection is under use */ MASTER_CONNECTION(p)->closetime = 0; POOL_SETMASK(&oldmask); + + if (check_socket_status(MASTER(p)->fd) < 0 || + (DUAL_MODE && check_socket_status(MASTER(p)->fd) < 0)) + { + pool_log("connection closed. retry to create new connection pool."); + pool_free_startup_packet(MASTER_CONNECTION(p)->sp); + pool_close(MASTER_CONNECTION(p)->con); + + if (DUAL_MODE) + { + /* do not free memory! we did not allocate them */ + pool_close(SECONDARY_CONNECTION(p)->con); + } + + memset(p, 0, sizeof(POOL_CONNECTION_POOL)); + return NULL; + } + return p; } p++; @@ -462,3 +481,34 @@ static POOL_CONNECTION_POOL *new_connection(POOL_CONNECTION_POOL *p) return p; } + +/* check_socket_status() + * RETURN: 0 => OK + * -1 => broken socket. + */ +static int check_socket_status(int fd) +{ + fd_set rfds; + int result; + struct timeval t; + + for (;;) + { + FD_ZERO(&rfds); + FD_SET(fd, &rfds); + + t.tv_sec = t.tv_usec = 0; + + result = select(fd+1, &rfds, NULL, NULL, &t); + if (result < 0 && errno == EINTR) + { + continue; + } + else + { + return (result == 0 ? 0 : -1); + } + } + + return -1; +} -- 2.39.5