Re: droped out precise time calculations in src/interfaces/libpq/fe-connect.c

Поиск
Список
Период
Сортировка
Искать

Re: droped out precise time calculations in src/interfaces/libpq/fe-connect.c

От:
Tom Lane <tgl@sss.pgh.pa.us>
Дата:

Re: droped out precise time calculations in src/interfaces/libpq/fe-connect.c

От:
Tom Lane <tgl@sss.pgh.pa.us>
Дата:

Re: droped out precise time calculations in src/interfaces/libpq/fe-connect.c

От:
Tom Lane <tgl@sss.pgh.pa.us>
Дата:

Re: droped out precise time calculations in src/interfaces/libpq/fe-connect.c

От:
Tom Lane <tgl@sss.pgh.pa.us>
Дата:

Re: droped out precise time calculations in src/interfaces/libpq/fe-connect.c

От:
Tom Lane <tgl@sss.pgh.pa.us>
Дата:

Re: droped out precise time calculations in src/interfaces/libpq/fe-connect.c

От:
Tom Lane <tgl@sss.pgh.pa.us>
Дата:

Re: droped out precise time calculations in src/interfaces/libpq/fe-connect.c

От:
Tom Lane <tgl@sss.pgh.pa.us>
Дата:

droped out precise time calculations in src/interfaces/libpq/fe-connect.c

От:
Denis A Ustimenko <denis@oldham.ru>
Дата:

Re: droped out precise time calculations in src/interfaces/libpq/fe-connect.c

От:
Denis A Ustimenko <denis@oldham.ru>
Дата:

Re: droped out precise time calculations in src/interfaces/libpq/fe-connect.c

От:
Bruce Momjian <pgman@candle.pha.pa.us>
Дата:

Re: droped out precise time calculations in src/interfaces/libpq/fe-connect.c

От:
Bruce Momjian <pgman@candle.pha.pa.us>
Дата:

Re: droped out precise time calculations in src/interfaces/libpq/fe-connect.c

От:
Denis A Ustimenko <denis@oldham.ru>
Дата:

Re: droped out precise time calculations in src/interfaces/libpq/fe-connect.c

От:
Bruce Momjian <pgman@candle.pha.pa.us>
Дата:

Re: droped out precise time calculations in src/interfaces/libpq/fe-connect.c

От:
Bruce Momjian <pgman@candle.pha.pa.us>
Дата:

Re: droped out precise time calculations in src/interfaces/libpq/fe-connect.c

От:
Bruce Momjian <pgman@candle.pha.pa.us>
Дата:
Tom Lane wrote:
> Bruce Momjian  writes:
> > That whole remains structure should be a time_t variable, and then we
> > _know_ we can't assume it is signed.  The use of timeval should
> > happen only in pqWaitTimed because it has to use select().
> 
> I think it's fine to use struct timeval as the parameter type for
> pqWaitTimed.  This particular caller of pqWaitTimed has no need for
> sub-second wait precision, but that doesn't mean we might not want it
> for other purposes later.

That was a question:  whether pqWaitTimed() was something exported by
libpq and therefore something that has an API that shouldn't change.  I
see it in libpq-int.h, which I think means it isn't exported, but yes,
there could be later cases where we need subsecond stuff.

I have applied the following patch to get us a little closer to sanity.

-- 
  Bruce Momjian                        |  http://candle.pha.pa.us
  pgman@candle.pha.pa.us               |  (610) 359-1001
  +  If your life is a hard drive,     |  13 Roberts Road
  +  Christ can be your backup.        |  Newtown Square, Pennsylvania 19073
Index: src/interfaces/libpq/fe-connect.c
===================================================================
RCS file: /cvsroot/pgsql-server/src/interfaces/libpq/fe-connect.c,v
retrieving revision 1.208
diff -c -c -r1.208 fe-connect.c
*** src/interfaces/libpq/fe-connect.c	11 Oct 2002 04:41:59 -0000	1.208
--- src/interfaces/libpq/fe-connect.c	14 Oct 2002 17:10:19 -0000
***************
*** 1071,1085 ****
  			conn->status = CONNECTION_BAD;
  			return 0;
  		}
! 		remains.tv_usec = 0;
  		rp = &remains;
  
  		/* calculate the finish time based on start + timeout */
  		finish_time = time((time_t *) NULL) + remains.tv_sec;
  	}
  
! 	while (rp == NULL || remains.tv_sec > 0 ||
! 		   (remains.tv_sec == 0 && remains.tv_usec > 0))
  	{
  		/*
  		 * Wait, if necessary.	Note that the initial state (just after
--- 1071,1084 ----
  			conn->status = CONNECTION_BAD;
  			return 0;
  		}
! 		remains.tv_usec = 0;	/* We don't use subsecond timing */
  		rp = &remains;
  
  		/* calculate the finish time based on start + timeout */
  		finish_time = time((time_t *) NULL) + remains.tv_sec;
  	}
  
! 	while (rp == NULL || remains.tv_sec > 0)
  	{
  		/*
  		 * Wait, if necessary.	Note that the initial state (just after
***************
*** 1133,1139 ****
  			}
  
  			remains.tv_sec = finish_time - current_time;
- 			remains.tv_usec = 0;
  		}
  	}
  	conn->status = CONNECTION_BAD;
--- 1132,1137 ----
Index: src/interfaces/libpq/fe-misc.c
===================================================================
RCS file: /cvsroot/pgsql-server/src/interfaces/libpq/fe-misc.c,v
retrieving revision 1.80
diff -c -c -r1.80 fe-misc.c
*** src/interfaces/libpq/fe-misc.c	3 Oct 2002 17:09:42 -0000	1.80
--- src/interfaces/libpq/fe-misc.c	14 Oct 2002 17:10:22 -0000
***************
*** 783,796 ****
  }
  
  int
! pqWaitTimed(int forRead, int forWrite, PGconn *conn, const struct timeval * timeout)
  {
  	fd_set		input_mask;
  	fd_set		output_mask;
  	fd_set		except_mask;
  
  	struct timeval tmp_timeout;
- 	struct timeval *ptmp_timeout = NULL;
  
  	if (conn->sock < 0)
  	{
--- 783,795 ----
  }
  
  int
! pqWaitTimed(int forRead, int forWrite, PGconn *conn, const struct timeval *timeout)
  {
  	fd_set		input_mask;
  	fd_set		output_mask;
  	fd_set		except_mask;
  
  	struct timeval tmp_timeout;
  
  	if (conn->sock < 0)
  	{
***************
*** 823,836 ****
  		if (NULL != timeout)
  		{
  			/*
! 			 * select may modify timeout argument on some platforms use
! 			 * copy
  			 */
  			tmp_timeout = *timeout;
- 			ptmp_timeout = &tmp_timeout;
  		}
  		if (select(conn->sock + 1, &input_mask, &output_mask,
! 				   &except_mask, ptmp_timeout) < 0)
  		{
  			if (SOCK_ERRNO == EINTR)
  				goto retry5;
--- 822,834 ----
  		if (NULL != timeout)
  		{
  			/*
! 			 * 	select() may modify timeout argument on some platforms so
! 			 *	use copy
  			 */
  			tmp_timeout = *timeout;
  		}
  		if (select(conn->sock + 1, &input_mask, &output_mask,
! 				   &except_mask, &tmp_timeout) < 0)
  		{
  			if (SOCK_ERRNO == EINTR)
  				goto retry5;
Index: src/interfaces/libpq/libpq-int.h
===================================================================
RCS file: /cvsroot/pgsql-server/src/interfaces/libpq/libpq-int.h,v
retrieving revision 1.58
diff -c -c -r1.58 libpq-int.h
*** src/interfaces/libpq/libpq-int.h	3 Oct 2002 17:09:42 -0000	1.58
--- src/interfaces/libpq/libpq-int.h	14 Oct 2002 17:10:24 -0000
***************
*** 340,346 ****
  extern int	pqFlush(PGconn *conn);
  extern int	pqSendSome(PGconn *conn);
  extern int	pqWait(int forRead, int forWrite, PGconn *conn);
! extern int	pqWaitTimed(int forRead, int forWrite, PGconn *conn, const struct timeval * timeout);
  extern int	pqReadReady(PGconn *conn);
  extern int	pqWriteReady(PGconn *conn);
  
--- 340,346 ----
  extern int	pqFlush(PGconn *conn);
  extern int	pqSendSome(PGconn *conn);
  extern int	pqWait(int forRead, int forWrite, PGconn *conn);
! extern int	pqWaitTimed(int forRead, int forWrite, PGconn *conn, const struct timeval *timeout);
  extern int	pqReadReady(PGconn *conn);
  extern int	pqWriteReady(PGconn *conn);
  

Re: droped out precise time calculations in src/interfaces/libpq/fe-connect.c

От:
Bruce Momjian <pgman@candle.pha.pa.us>
Дата:

Re: droped out precise time calculations in src/interfaces/libpq/fe-connect.c

От:
Bruce Momjian <pgman@candle.pha.pa.us>
Дата:

I have applied the following comment patch.  The current code resets the
timer when select() is interruped.  On OS's that modify timeout to show
the remaining time, we should be using that value instead of resetting
the timer to its original value on select retry.

---------------------------------------------------------------------------

pgman wrote:
> 
> Oops, overoptimized a little. ptmp_timeout is needed in case no time is
> passed;  ptmp_timeout restored.
> 
> ---------------------------------------------------------------------------
> 
> pgman wrote:
> > Tom Lane wrote:
> > > Bruce Momjian  writes:
> > > > That whole remains structure should be a time_t variable, and then we
> > > > _know_ we can't assume it is signed.  The use of timeval should
> > > > happen only in pqWaitTimed because it has to use select().
> > > 
> > > I think it's fine to use struct timeval as the parameter type for
> > > pqWaitTimed.  This particular caller of pqWaitTimed has no need for
> > > sub-second wait precision, but that doesn't mean we might not want it
> > > for other purposes later.
> > 
> > That was a question:  whether pqWaitTimed() was something exported by
> > libpq and therefore something that has an API that shouldn't change.  I
> > see it in libpq-int.h, which I think means it isn't exported, but yes,
> > there could be later cases where we need subsecond stuff.
> > 
> > I have applied the following patch to get us a little closer to sanity.
> 
> -- 
>   Bruce Momjian                        |  http://candle.pha.pa.us
>   pgman@candle.pha.pa.us               |  (610) 359-1001
>   +  If your life is a hard drive,     |  13 Roberts Road
>   +  Christ can be your backup.        |  Newtown Square, Pennsylvania 19073

-- 
  Bruce Momjian                        |  http://candle.pha.pa.us
  pgman@candle.pha.pa.us               |  (610) 359-1001
  +  If your life is a hard drive,     |  13 Roberts Road
  +  Christ can be your backup.        |  Newtown Square, Pennsylvania 19073
Index: src/interfaces/libpq/fe-misc.c
===================================================================
RCS file: /cvsroot/pgsql-server/src/interfaces/libpq/fe-misc.c,v
retrieving revision 1.82
diff -c -c -r1.82 fe-misc.c
*** src/interfaces/libpq/fe-misc.c	14 Oct 2002 17:33:08 -0000	1.82
--- src/interfaces/libpq/fe-misc.c	14 Oct 2002 18:08:14 -0000
***************
*** 824,830 ****
  		{
  			/*
  			 * 	select() may modify timeout argument on some platforms so
! 			 *	use copy
  			 */
  			tmp_timeout = *timeout;
  			ptmp_timeout = &tmp_timeout;
--- 824,835 ----
  		{
  			/*
  			 * 	select() may modify timeout argument on some platforms so
! 			 *	use copy.
! 			 *	XXX Do we really want to do that?  If select() returns
! 			 *	the number of seconds remaining, we are resetting
! 			 *	the timeout to its original value.  This will yeild
! 			 *	incorrect timings when select() is interrupted.
! 			 *	bjm 2002-10-14
  			 */
  			tmp_timeout = *timeout;
  			ptmp_timeout = &tmp_timeout;

Re: droped out precise time calculations in src/interfaces/libpq/fe-connect.c

От:
Bruce Momjian <pgman@candle.pha.pa.us>
Дата:

Re: droped out precise time calculations in src/interfaces/libpq/fe-connect.c

От:
Denis A Ustimenko <denis@oldham.ru>
Дата:

Re: droped out precise time calculations in src/interfaces/libpq/fe-connect.c

От:
Bruce Momjian <pgman@candle.pha.pa.us>
Дата:

Re: droped out precise time calculations in src/interfaces/libpq/fe-connect.c

От:
Bruce Momjian <pgman@candle.pha.pa.us>
Дата:

Re: droped out precise time calculations in src/interfaces/libpq/fe-connect.c

От:
Bruce Momjian <pgman@candle.pha.pa.us>
Дата:

Re: droped out precise time calculations in src/interfaces/libpq/fe-connect.c

От:
Bruce Momjian <pgman@candle.pha.pa.us>
Дата:

Re: droped out precise time calculations in src/interfaces/libpq/fe-connect.c

От:
Bruce Momjian <pgman@candle.pha.pa.us>
Дата:
Tom Lane wrote:
> Joe Conway  writes:
> > [ some convincing test cases that timeout=1 is not good ]
> 
> >   		remains.tv_sec = atoi(conn->connect_timeout);
> > + 		if (remains.tv_sec == 1)
> > + 			remains.tv_sec += 1;
> >   		if (!remains.tv_sec)
> >   		{
> >   			conn->status = CONNECTION_BAD;
> 
> On pure-paranoia grounds, I'd suggest the logic
> 
> +		/* force a sane minimum delay */
> + 		if (remains.tv_sec < 2)
> + 			remains.tv_sec = 2;
> 
> whereupon you could remove the failure check just below.

I think we should fail if they set the timeout to zero, rather than
cover it up by setting the delay to two.

Attached is a patch that implements most of what we have discussed:

	use time()
	get rid of timeval where not needed
	allow restart of select() to properly compute remaining time
	add 1 to timeout == 1
	pass finish time to pqWaitTimed

Patch applied.  I am applying it so it is in CVS and everyone can see
it.  I will keep modifying it until everyone likes it.  It is just
easier to do it that way when multiple people are reviewing it.  They
can jump in and make changes too.

-- 
  Bruce Momjian                        |  http://candle.pha.pa.us
  pgman@candle.pha.pa.us               |  (610) 359-1001
  +  If your life is a hard drive,     |  13 Roberts Road
  +  Christ can be your backup.        |  Newtown Square, Pennsylvania 19073
Index: src/interfaces/libpq/fe-connect.c
===================================================================
RCS file: /cvsroot/pgsql-server/src/interfaces/libpq/fe-connect.c,v
retrieving revision 1.210
diff -c -c -r1.210 fe-connect.c
*** src/interfaces/libpq/fe-connect.c	15 Oct 2002 01:48:25 -0000	1.210
--- src/interfaces/libpq/fe-connect.c	16 Oct 2002 02:48:07 -0000
***************
*** 1052,1061 ****
  {
  	PostgresPollingStatusType flag = PGRES_POLLING_WRITING;
  
! 	time_t			finish_time = 0,
! 					current_time;
! 	struct timeval	remains,
! 				   *rp = NULL;
  
  	if (conn == NULL || conn->status == CONNECTION_BAD)
  		return 0;
--- 1052,1058 ----
  {
  	PostgresPollingStatusType flag = PGRES_POLLING_WRITING;
  
! 	time_t			finish_time = -1;
  
  	if (conn == NULL || conn->status == CONNECTION_BAD)
  		return 0;
***************
*** 1065,1084 ****
  	 */
  	if (conn->connect_timeout != NULL)
  	{
! 		remains.tv_sec = atoi(conn->connect_timeout);
! 		if (!remains.tv_sec)
  		{
  			conn->status = CONNECTION_BAD;
  			return 0;
  		}
! 		remains.tv_usec = 0;	/* We don't use subsecond timing */
! 		rp = &remains;
! 
  		/* calculate the finish time based on start + timeout */
! 		finish_time = time((time_t *) NULL) + remains.tv_sec;
  	}
  
! 	while (rp == NULL || remains.tv_sec > 0)
  	{
  		/*
  		 * Wait, if necessary.	Note that the initial state (just after
--- 1062,1082 ----
  	 */
  	if (conn->connect_timeout != NULL)
  	{
! 		int timeout = atoi(conn->connect_timeout);
! 
! 		if (timeout == 0)
  		{
  			conn->status = CONNECTION_BAD;
  			return 0;
  		}
! 		/* 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)
  	{
  		/*
  		 * Wait, if necessary.	Note that the initial state (just after
***************
*** 1094,1100 ****
  				return 1;		/* success! */
  
  			case PGRES_POLLING_READING:
! 				if (pqWaitTimed(1, 0, conn, rp))
  				{
  					conn->status = CONNECTION_BAD;
  					return 0;
--- 1092,1098 ----
  				return 1;		/* success! */
  
  			case PGRES_POLLING_READING:
! 				if (pqWaitTimed(1, 0, conn, finish_time))
  				{
  					conn->status = CONNECTION_BAD;
  					return 0;
***************
*** 1102,1108 ****
  				break;
  
  			case PGRES_POLLING_WRITING:
! 				if (pqWaitTimed(0, 1, conn, rp))
  				{
  					conn->status = CONNECTION_BAD;
  					return 0;
--- 1100,1106 ----
  				break;
  
  			case PGRES_POLLING_WRITING:
! 				if (pqWaitTimed(0, 1, conn, finish_time))
  				{
  					conn->status = CONNECTION_BAD;
  					return 0;
***************
*** 1119,1138 ****
  		 * Now try to advance the state machine.
  		 */
  		flag = PQconnectPoll(conn);
- 
- 		/*
- 		 * If connecting timeout is set, calculate remaining time.
- 		 */
- 		if (rp != NULL)
- 		{
- 			if (time(¤t_time) == -1)
- 			{
- 				conn->status = CONNECTION_BAD;
- 				return 0;
- 			}
- 
- 			remains.tv_sec = finish_time - current_time;
- 		}
  	}
  	conn->status = CONNECTION_BAD;
  	return 0;
--- 1117,1122 ----
Index: src/interfaces/libpq/fe-misc.c
===================================================================
RCS file: /cvsroot/pgsql-server/src/interfaces/libpq/fe-misc.c,v
retrieving revision 1.83
diff -c -c -r1.83 fe-misc.c
*** src/interfaces/libpq/fe-misc.c	14 Oct 2002 18:11:17 -0000	1.83
--- src/interfaces/libpq/fe-misc.c	16 Oct 2002 02:48:10 -0000
***************
*** 779,789 ****
  int
  pqWait(int forRead, int forWrite, PGconn *conn)
  {
! 	return pqWaitTimed(forRead, forWrite, conn, (const struct timeval *) NULL);
  }
  
  int
! pqWaitTimed(int forRead, int forWrite, PGconn *conn, const struct timeval *timeout)
  {
  	fd_set		input_mask;
  	fd_set		output_mask;
--- 779,789 ----
  int
  pqWait(int forRead, int forWrite, PGconn *conn)
  {
! 	return pqWaitTimed(forRead, forWrite, conn, -1);
  }
  
  int
! pqWaitTimed(int forRead, int forWrite, PGconn *conn, time_t finish_time)
  {
  	fd_set		input_mask;
  	fd_set		output_mask;
***************
*** 820,826 ****
  			FD_SET(conn->sock, &output_mask);
  		FD_SET(conn->sock, &except_mask);
  
! 		if (NULL != timeout)
  		{
  			/*
  			 * 	select() may modify timeout argument on some platforms so
--- 820,826 ----
  			FD_SET(conn->sock, &output_mask);
  		FD_SET(conn->sock, &except_mask);
  
! 		if (finish_time != -1)
  		{
  			/*
  			 * 	select() may modify timeout argument on some platforms so
***************
*** 831,837 ****
  			 *	incorrect timings when select() is interrupted.
  			 *	bjm 2002-10-14
  			 */
! 			tmp_timeout = *timeout;
  			ptmp_timeout = &tmp_timeout;
  		}
  		if (select(conn->sock + 1, &input_mask, &output_mask,
--- 831,839 ----
  			 *	incorrect timings when select() is interrupted.
  			 *	bjm 2002-10-14
  			 */
! 			if ((tmp_timeout.tv_sec = finish_time - time(NULL)) < 0)
! 				tmp_timeout.tv_sec = 0;  /* possible? */
! 			tmp_timeout.tv_usec = 0;
  			ptmp_timeout = &tmp_timeout;
  		}
  		if (select(conn->sock + 1, &input_mask, &output_mask,
Index: src/interfaces/libpq/libpq-int.h
===================================================================
RCS file: /cvsroot/pgsql-server/src/interfaces/libpq/libpq-int.h,v
retrieving revision 1.59
diff -c -c -r1.59 libpq-int.h
*** src/interfaces/libpq/libpq-int.h	14 Oct 2002 17:15:11 -0000	1.59
--- src/interfaces/libpq/libpq-int.h	16 Oct 2002 02:48:12 -0000
***************
*** 340,346 ****
  extern int	pqFlush(PGconn *conn);
  extern int	pqSendSome(PGconn *conn);
  extern int	pqWait(int forRead, int forWrite, PGconn *conn);
! extern int	pqWaitTimed(int forRead, int forWrite, PGconn *conn, const struct timeval *timeout);
  extern int	pqReadReady(PGconn *conn);
  extern int	pqWriteReady(PGconn *conn);
  
--- 340,347 ----
  extern int	pqFlush(PGconn *conn);
  extern int	pqSendSome(PGconn *conn);
  extern int	pqWait(int forRead, int forWrite, PGconn *conn);
! extern int	pqWaitTimed(int forRead, int forWrite, PGconn *conn, 
! 						time_t finish_time);
  extern int	pqReadReady(PGconn *conn);
  extern int	pqWriteReady(PGconn *conn);
  

Re: droped out precise time calculations in src/interfaces/libpq/fe-connect.c

От:
Bruce Momjian <pgman@candle.pha.pa.us>
Дата:

Re: droped out precise time calculations in src/interfaces/libpq/fe-connect.c

От:
Bruce Momjian <pgman@candle.pha.pa.us>
Дата:

Re: droped out precise time calculations in src/interfaces/libpq/fe-connect.c

От:
Denis A Ustimenko <denis@oldham.ru>
Дата:

Re: droped out precise time calculations in src/interfaces/libpq/fe-connect.c

От:
Bruce Momjian <pgman@candle.pha.pa.us>
Дата:

Re: droped out precise time calculations in src/interfaces/libpq/fe-connect.c

От:
Bruce Momjian <pgman@candle.pha.pa.us>
Дата:

Re: droped out precise time calculations in src/interfaces/libpq/fe-connect.c

От:
Bruce Momjian <pgman@candle.pha.pa.us>
Дата:

Re: droped out precise time calculations in src/interfaces/libpq/fe-connect.c

От:
Bruce Momjian <pgman@candle.pha.pa.us>
Дата:

Re: droped out precise time calculations in src/interfaces/libpq/fe-connect.c

От:
Tom Lane <tgl@sss.pgh.pa.us>
Дата:

Re: droped out precise time calculations in src/interfaces/libpq/fe-connect.c

От:
Tom Lane <tgl@sss.pgh.pa.us>
Дата:

Re: droped out precise time calculations in src/interfaces/libpq/fe-connect.c

От:
Tom Lane <tgl@sss.pgh.pa.us>
Дата:

Re: droped out precise time calculations in src/interfaces/libpq/fe-connect.c

От:
Joe Conway <mail@joeconway.com>
Дата:

Re: droped out precise time calculations in src/interfaces/libpq/fe-connect.c

От:
Joe Conway <mail@joeconway.com>
Дата:

Re: droped out precise time calculations in src/interfaces/libpq/fe-connect.c

От:
Joe Conway <mail@joeconway.com>
Дата:

Re: droped out precise time calculations in src/interfaces/libpq/fe-connect.c

От:
Joe Conway <mail@joeconway.com>
Дата:

Re: droped out precise time calculations in src/interfaces/libpq/fe-connect.c

От:
Joe Conway <mail@joeconway.com>
Дата:
Tom Lane wrote:
 > Joe Conway  writes:
 >
 >> The thing was that with the extra +1, I was repeatedly getting a
 >> wall-clock time of 2 seconds with a timeout set to 1 second. It seemed
 >> odd to have my 1 second timeout automatically turned into 2 seconds every
 >> time.
 >
 > That is odd; seems like you should get between 1 and 2 seconds.  How were
 > you measuring the delay, exactly?

OK. I got a little more scientific about my testing. I used a php script, 
running on the same machine, to connect/disconnect in a tight loop and timed 
successful and unsuccessful connection attempts using microtime().

Here are the results. First with current cvs code:

current cvs libpq code
-----------------------
good connect info, using unix socket, timeout = 1 second:
=========================================================
unsuccessful 69 times: sum 0.41736388206482: avg 0.0060487519139829
successful 9931 times: sum 68.798981308937: avg 0.0069276992557584

good connect info, using hostaddr, timeout = 1 second
=====================================================
unsuccessful 72 times: sum 0.37020063400269: avg 0.0051416754722595
successful 9928 times: sum 75.047878861427: avg 0.0075592142285886

current cvs libpq code - bad hostaadr, using hostaddr, timeout = 1 second
=========================================================================
unsuccessful 100 times: sum 99.975758910179: avg 0.99975758910179
successful 0 times: sum 0: avg n/a


Clearly not good. The timeout code is causing connection failures about 0.7% 
of the time. Next are the results using the attached patch. Per Bruce's 
suggestion, it only adds 1 if the timeout is set to 1.


with patch libpq code
---------------------
good connect info, using unix socket, timeout = 1 second
========================================================
unsuccessful 0 times: sum 0: avg n/a
successful 10000 times: sum 68.95981669426: avg 0.006895981669426

with patch libpq code - good connect info, using hostaddr, timeout = 1 second
=============================================================================
unsuccessful 0 times: sum 0: avg n/a
successful 10000 times: sum 73.500863552094: avg 0.0073500863552094

with patch libpq code - good connect info, using hostaddr, timeout = 2 seconds
==============================================================================
unsuccessful 0 times: sum 0: avg n/a
successful 10000 times: sum 73.354710936546: avg 0.0073354710936546

with patch libpq code - bad hostaadr, using hostaddr, timeout = 1 second
========================================================================
unsuccessful 100 times: sum 149.98181843758: avg 1.4998181843758
successful 0 times: sum 0: avg n/a

with patch libpq code - bad hostaadr, using hostaddr, timeout = 2 seconds
=========================================================================
unsuccessful 100 times: sum 149.98445630074: avg 1.4998445630074
successful 0 times: sum 0: avg n/a

with patch libpq code - bad hostaadr, using hostaddr, timeout = 3 seconds
=========================================================================
unsuccessful 20 times: sum 59.842629671097: avg 2.9921314835548
successful 0 times: sum 0: avg n/a


With the patch there were 0 failures on 30000 attempts using good connect 
information.

If there are no objections, please apply the attached. Otherwise let me know 
if you'd like different tests or would like to try other approaches.

Thanks,

Joe


Index: src/interfaces/libpq/fe-connect.c
===================================================================
RCS file: /opt/src/cvs/pgsql-server/src/interfaces/libpq/fe-connect.c,v
retrieving revision 1.210
diff -c -r1.210 fe-connect.c
*** src/interfaces/libpq/fe-connect.c	15 Oct 2002 01:48:25 -0000	1.210
--- src/interfaces/libpq/fe-connect.c	15 Oct 2002 22:36:53 -0000
***************
*** 1066,1071 ****
--- 1066,1073 ----
  	if (conn->connect_timeout != NULL)
  	{
  		remains.tv_sec = atoi(conn->connect_timeout);
+ 		if (remains.tv_sec == 1)
+ 			remains.tv_sec += 1;
  		if (!remains.tv_sec)
  		{
  			conn->status = CONNECTION_BAD;

Re: droped out precise time calculations in src/interfaces/libpq/fe-connect.c

От:
Joe Conway <mail@joeconway.com>
Дата:

Re: droped out precise time calculations in src/interfaces/libpq/fe-connect.c

От:
Joe Conway <mail@joeconway.com>
Дата:

Re: droped out precise time calculations in src/interfaces/libpq/fe-connect.c

От:
Joe Conway <mail@joeconway.com>
Дата:
FAQ