1
0
mirror of https://github.com/postgres/postgres.git synced 2025-08-05 07:41:25 +03:00

Code review for connection timeout patch. Avoid unportable assumption

that tv_sec is signed; return a useful error message on timeout failure;
honor PGCONNECT_TIMEOUT environment variable in PQsetdbLogin; make code
obey documentation statement that timeout=0 means no timeout.
This commit is contained in:
Tom Lane
2002-10-24 23:35:55 +00:00
parent bd19e8f604
commit 2908a838ac
2 changed files with 45 additions and 30 deletions

View File

@@ -8,7 +8,7 @@
*
*
* IDENTIFICATION
* $Header: /cvsroot/pgsql/src/interfaces/libpq/fe-connect.c,v 1.212 2002/10/16 04:38:00 momjian Exp $
* $Header: /cvsroot/pgsql/src/interfaces/libpq/fe-connect.c,v 1.213 2002/10/24 23:35:55 tgl Exp $
*
*-------------------------------------------------------------------------
*/
@@ -507,6 +507,9 @@ PQsetdbLogin(const char *pghost, const char *pgport, const char *pgoptions,
else
conn->pgpass = strdup(DefaultPassword);
if ((tmp = getenv("PGCONNECT_TIMEOUT")) != NULL)
conn->connect_timeout = strdup(tmp);
#ifdef USE_SSL
if ((tmp = getenv("PGREQUIRESSL")) != NULL)
conn->require_ssl = (tmp[0] == '1') ? true : false;
@@ -1051,32 +1054,29 @@ static int
connectDBComplete(PGconn *conn)
{
PostgresPollingStatusType flag = PGRES_POLLING_WRITING;
time_t finish_time = -1;
time_t finish_time = ((time_t) -1);
if (conn == NULL || conn->status == CONNECTION_BAD)
return 0;
/*
* Prepare to time calculations, if connect_timeout isn't zero.
* Set up a time limit, if connect_timeout isn't zero.
*/
if (conn->connect_timeout != NULL)
{
int timeout = atoi(conn->connect_timeout);
if (timeout == 0)
if (timeout > 0)
{
conn->status = CONNECTION_BAD;
return 0;
/* Rounding could cause connection to fail; need at least 2 secs */
if (timeout < 2)
timeout = 2;
/* calculate the finish time based on start + timeout */
finish_time = time(NULL) + timeout;
}
/* Rounding could cause connection to fail;we need at least 2 secs */
if (timeout == 1)
timeout++;
/* calculate the finish time based on start + timeout */
finish_time = time(NULL) + timeout;
}
while (finish_time == -1 || time(NULL) < finish_time)
for (;;)
{
/*
* Wait, if necessary. Note that the initial state (just after
@@ -1118,8 +1118,6 @@ connectDBComplete(PGconn *conn)
*/
flag = PQconnectPoll(conn);
}
conn->status = CONNECTION_BAD;
return 0;
}
/* ----------------