aboutsummaryrefslogtreecommitdiff
path: root/src/interfaces/libpq/fe-connect.c
diff options
context:
space:
mode:
authorTom Lane <tgl@sss.pgh.pa.us>2002-10-24 23:35:55 +0000
committerTom Lane <tgl@sss.pgh.pa.us>2002-10-24 23:35:55 +0000
commit2908a838ac2cf8cdccaa115249f8399eef8a731e (patch)
tree9e13a675ebed1067b03210497142542ca8a378af /src/interfaces/libpq/fe-connect.c
parentbd19e8f60488c1861b43f20931e64fe0468d309a (diff)
downloadpostgresql-2908a838ac2cf8cdccaa115249f8399eef8a731e.tar.gz
postgresql-2908a838ac2cf8cdccaa115249f8399eef8a731e.zip
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.
Diffstat (limited to 'src/interfaces/libpq/fe-connect.c')
-rw-r--r--src/interfaces/libpq/fe-connect.c28
1 files changed, 13 insertions, 15 deletions
diff --git a/src/interfaces/libpq/fe-connect.c b/src/interfaces/libpq/fe-connect.c
index 65775de9d64..7cdd2466624 100644
--- a/src/interfaces/libpq/fe-connect.c
+++ b/src/interfaces/libpq/fe-connect.c
@@ -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;
}
/* ----------------