From: Leandro Lucarella <llucarella@integratech.com.ar>
To: pgsql-interfaces@postgresql.org
Subject: Keep-alive support
Date: Wed, 29 Nov 2006 16:49:07 -0300
Message-ID: <456DE433.8000407@integratech.com.ar> (raw)
Is there any keep alive support in libpq? I'm not really using libpq
directly, I'm using libpqxx and there is no keep-alive support there, so
I'm trying to use TCP's own keep-alive support, but I have a problem:
libpq seems to reconnect the socket when the connection is lost.
What I do is this:
/* Connect libpq as in the Example 1 of the manual. */
/* Before any query is sent (linux 2.6.18) */
int sd = PQsocket(conn);
// Use TCP keep-alive feature
setsockopt(sd, SOL_SOCKET, SO_KEEPALIVE, 1);
// Maximum keep-alive probes before asuming the connection is lost
setsockopt(sd, IPPROTO_TCP, TCP_KEEPCNT, 5);
// Interval (in seconds) between keep-alive probes
setsockopt(sd, IPPROTO_TCP, TCP_KEEPINTVL, 2);
// Maximum idle time (in seconds) before start sending keep-alive probes
setsockopt(sd, IPPROTO_TCP, TCP_KEEPIDLE, 10);
(see set_sock_opt() above, but is just a simple setsockopt wrapper)
then I so a sleep(10) and continue with the Example 1 of the manual
(which makes a simple transaction query). In the sleep time I unplug the
network cable and monitor the TCP connection using netstat -pano, and
found all the TCP keep-alive timers times out perfectly, closing the
connection, but inmediatly I see a new connection (and without the
keep-alive parameters, so it take forever to timeout again). So I guess
libpq is re-opening the socket. This is making my life a nightmare =)
Is there any way to avoid this behavior? Please tell me it is =)
PS: This thread was originated in libpqxx's mailing list, but I'm moving
it here because it looks like a libpq issue, if you want take a look to
the original thread, you can find it here:
http://gborg.postgresql.org/pipermail/libpqxx-general/2006-November/001511.html
TIA
--------8<--------8<--------8<--------8<--------8<--------8<--------
void set_sock_opt(int sd, int level, int name, int val)
{
if (setsockopt(sd, level, name, &val, sizeof(val)) == -1)
{
perror("setsockopt");
abort();
}
}
-------->8-------->8-------->8-------->8-------->8-------->8--------
--
Leandro Lucarella
Integratech S.A.
4571-5252
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Reply to all the recipients using the --to and --cc options:
reply via email
To: pgsql-interfaces@postgresql.org
Cc: llucarella@integratech.com.ar
Subject: Re: Keep-alive support
In-Reply-To: <456DE433.8000407@integratech.com.ar>
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox