Skip to content

Commit facb720

Browse files
committed
Fix connection_timeout to use time() and handle timeout == 1.
Code cleanup.
1 parent 8a96c50 commit facb720

File tree

3 files changed

+22
-35
lines changed

3 files changed

+22
-35
lines changed

src/interfaces/libpq/fe-connect.c

Lines changed: 12 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
*
99
*
1010
* IDENTIFICATION
11-
* $Header: /cvsroot/pgsql/src/interfaces/libpq/fe-connect.c,v 1.210 2002/10/15 01:48:25 tgl Exp $
11+
* $Header: /cvsroot/pgsql/src/interfaces/libpq/fe-connect.c,v 1.211 2002/10/16 02:55:30 momjian Exp $
1212
*
1313
*-------------------------------------------------------------------------
1414
*/
@@ -1052,10 +1052,7 @@ connectDBComplete(PGconn *conn)
10521052
{
10531053
PostgresPollingStatusType flag = PGRES_POLLING_WRITING;
10541054

1055-
time_t finish_time = 0,
1056-
current_time;
1057-
struct timeval remains,
1058-
*rp = NULL;
1055+
time_t finish_time = -1;
10591056

10601057
if (conn == NULL || conn->status == CONNECTION_BAD)
10611058
return 0;
@@ -1065,20 +1062,21 @@ connectDBComplete(PGconn *conn)
10651062
*/
10661063
if (conn->connect_timeout != NULL)
10671064
{
1068-
remains.tv_sec = atoi(conn->connect_timeout);
1069-
if (!remains.tv_sec)
1065+
int timeout = atoi(conn->connect_timeout);
1066+
1067+
if (timeout == 0)
10701068
{
10711069
conn->status = CONNECTION_BAD;
10721070
return 0;
10731071
}
1074-
remains.tv_usec = 0; /* We don't use subsecond timing */
1075-
rp = &remains;
1076-
1072+
/* Rounding could cause connection to fail;we need at least 2 secs */
1073+
if (timeout == 1)
1074+
timeout++;
10771075
/* calculate the finish time based on start + timeout */
1078-
finish_time = time((time_t *) NULL) + remains.tv_sec;
1076+
finish_time = time(NULL) + timeout;
10791077
}
10801078

1081-
while (rp == NULL || remains.tv_sec > 0)
1079+
while (finish_time == -1 || time(NULL) >= finish_time)
10821080
{
10831081
/*
10841082
* Wait, if necessary. Note that the initial state (just after
@@ -1094,15 +1092,15 @@ connectDBComplete(PGconn *conn)
10941092
return 1; /* success! */
10951093

10961094
case PGRES_POLLING_READING:
1097-
if (pqWaitTimed(1, 0, conn, rp))
1095+
if (pqWaitTimed(1, 0, conn, finish_time))
10981096
{
10991097
conn->status = CONNECTION_BAD;
11001098
return 0;
11011099
}
11021100
break;
11031101

11041102
case PGRES_POLLING_WRITING:
1105-
if (pqWaitTimed(0, 1, conn, rp))
1103+
if (pqWaitTimed(0, 1, conn, finish_time))
11061104
{
11071105
conn->status = CONNECTION_BAD;
11081106
return 0;
@@ -1119,20 +1117,6 @@ connectDBComplete(PGconn *conn)
11191117
* Now try to advance the state machine.
11201118
*/
11211119
flag = PQconnectPoll(conn);
1122-
1123-
/*
1124-
* If connecting timeout is set, calculate remaining time.
1125-
*/
1126-
if (rp != NULL)
1127-
{
1128-
if (time(&current_time) == -1)
1129-
{
1130-
conn->status = CONNECTION_BAD;
1131-
return 0;
1132-
}
1133-
1134-
remains.tv_sec = finish_time - current_time;
1135-
}
11361120
}
11371121
conn->status = CONNECTION_BAD;
11381122
return 0;

src/interfaces/libpq/fe-misc.c

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
*
2626
*
2727
* IDENTIFICATION
28-
* $Header: /cvsroot/pgsql/src/interfaces/libpq/fe-misc.c,v 1.83 2002/10/14 18:11:17 momjian Exp $
28+
* $Header: /cvsroot/pgsql/src/interfaces/libpq/fe-misc.c,v 1.84 2002/10/16 02:55:30 momjian Exp $
2929
*
3030
*-------------------------------------------------------------------------
3131
*/
@@ -779,11 +779,11 @@ pqFlush(PGconn *conn)
779779
int
780780
pqWait(int forRead, int forWrite, PGconn *conn)
781781
{
782-
return pqWaitTimed(forRead, forWrite, conn, (const struct timeval *) NULL);
782+
return pqWaitTimed(forRead, forWrite, conn, -1);
783783
}
784784

785785
int
786-
pqWaitTimed(int forRead, int forWrite, PGconn *conn, const struct timeval *timeout)
786+
pqWaitTimed(int forRead, int forWrite, PGconn *conn, time_t finish_time)
787787
{
788788
fd_set input_mask;
789789
fd_set output_mask;
@@ -820,7 +820,7 @@ pqWaitTimed(int forRead, int forWrite, PGconn *conn, const struct timeval *timeo
820820
FD_SET(conn->sock, &output_mask);
821821
FD_SET(conn->sock, &except_mask);
822822

823-
if (NULL != timeout)
823+
if (finish_time != -1)
824824
{
825825
/*
826826
* select() may modify timeout argument on some platforms so
@@ -831,7 +831,9 @@ pqWaitTimed(int forRead, int forWrite, PGconn *conn, const struct timeval *timeo
831831
* incorrect timings when select() is interrupted.
832832
* bjm 2002-10-14
833833
*/
834-
tmp_timeout = *timeout;
834+
if ((tmp_timeout.tv_sec = finish_time - time(NULL)) < 0)
835+
tmp_timeout.tv_sec = 0; /* possible? */
836+
tmp_timeout.tv_usec = 0;
835837
ptmp_timeout = &tmp_timeout;
836838
}
837839
if (select(conn->sock + 1, &input_mask, &output_mask,

src/interfaces/libpq/libpq-int.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
* Portions Copyright (c) 1996-2002, PostgreSQL Global Development Group
1313
* Portions Copyright (c) 1994, Regents of the University of California
1414
*
15-
* $Id: libpq-int.h,v 1.59 2002/10/14 17:15:11 momjian Exp $
15+
* $Id: libpq-int.h,v 1.60 2002/10/16 02:55:30 momjian Exp $
1616
*
1717
*-------------------------------------------------------------------------
1818
*/
@@ -340,7 +340,8 @@ extern int pqReadData(PGconn *conn);
340340
extern int pqFlush(PGconn *conn);
341341
extern int pqSendSome(PGconn *conn);
342342
extern int pqWait(int forRead, int forWrite, PGconn *conn);
343-
extern int pqWaitTimed(int forRead, int forWrite, PGconn *conn, const struct timeval *timeout);
343+
extern int pqWaitTimed(int forRead, int forWrite, PGconn *conn,
344+
time_t finish_time);
344345
extern int pqReadReady(PGconn *conn);
345346
extern int pqWriteReady(PGconn *conn);
346347

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy