Thread: Keep alive in libpq

Keep alive in libpq

From
Pavel Golub
Date:
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? At
least in Windows environment?

If not are there any solutions?

-- 
With best wishes,Pavel                          mailto:pavel@gf.microolap.com



Re: Keep alive in libpq

From
Andrew Chernow
Date:
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/