Re: Keep alive in libpq

From: Andrew Chernow <ac(at)esilo(dot)com>
To: Pavel Golub <pavel(at)gf(dot)microolap(dot)com>
Cc: PostgreSQL-development <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: Keep alive in libpq
Date: 2009-05-27 13:27:44
Message-ID: 4A1D3FD0.6080305@esilo.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

Pavel Golub wrote:
> Hello, postgresmen.
>
> I found solution how to implement keep alive through sockets in
> archive: http://archives.postgresql.org/pgsql-interfaces/2006-11/msg00014.php
>
> However, it is dated 2006 year and I am wonder if this is for real?

setsockopt has been around since at least the early 90s. It is for real.

> At least in Windows environment?
>
> If not are there any solutions?
>

Use WSAIoctl(SIO_KEEPALIVE_VALS). SIO_KEEPALIVE_VALS is supported on
Windows 2000 and later.

http://msdn.microsoft.com/en-us/library/ms741621(VS.85).aspx
Search the page for "SIO_KEEPALIVE_VALS".

Make sure to test the below because I didn't :)

#include <Winsock2.h>
#include <Mstcpip.h> /* struct tcp_keepalive */

int r;
DWORD dw;
struct tcp_keepalive ka;

/* enable or disable (same as SO_KEEPALIVE) */
ka.onoff = 1;

/* milliseconds (same as TCP_KEEPIDLE) */
ka.keepalivetime = 60000;

/* milliseconds (same as TCP_KEEPINTVL) */
ka.keepaliveinterval = 3000;

/* configure keep-alives for 'conn' */
r = WSAIoctl((SOCKET) PQsocket(conn), SIO_KEEPALIVE_VALS,
&ka, (DWORD) sizeof(ka), NULL, 0, &dw, NULL, NULL);

if (r == SOCKET_ERROR)
{
// failed, check WSAGetLastError()
}

Apparently, you can also enable keep-alives using the standard
setsockopt(...SO_KEEPALIVE...); Although, the other knobs are only
exposed through WSAIoctl. When using SO_KEEPALIVE, the default
keep-alive settings are used ... keep-alive timeout of 2 hours followed
by a 1 second probe. They can still be adjusted via WSAIoctl.

--
Andrew Chernow
eSilo, LLC
every bit counts
http://www.esilo.com/

In response to

Browse pgsql-hackers by date

  From Date Subject
Next Message Peter Eisentraut 2009-05-27 13:33:28 Re: PostgreSQL Developer meeting minutes up
Previous Message Bruce Momjian 2009-05-27 13:16:18 Re: A couple of gripes about the gettext plurals patch