[pgpool-hackers: 405] Re: KeepAlive between pgpool and postgresql

Sergey Logvinov serge.logvinov at gmail.com
Thu Nov 14 18:57:22 JST 2013


pg_syn_count, i use it in connect_inet_domain_socket_by_port.

In linux default setting is 5 (cat /proc/sys/net/ipv4/tcp_syn_retries),
which would lead to an aproximate of 20 seconds delay before the connection
times out. 20 second is very much for web.

new diff pool_connection_pool.c see in attache (use code
src/backend/libpq/pqcomm.c)


2013/11/14 Tatsuo Ishii <ishii at postgresql.org>

> Thanks for the patch!
>
> > Code to allow tcp keepalive connection. It helps to define dead postgres
> > connections.
> >
> > based on ticket #83
> >
> > Recommended params for web server
> >
> > pg_keepalives_idle     = 10     # TCP_KEEPIDLE, in seconds;
> > pg_keepalives_interval = 3     # TCP_KEEPINTVL, in seconds;
> > pg_keepalives_count    = 5     # TCP_KEEPCNT;
>
> You need to check the existence of TCP_KEEPALIVE and
> SIO_KEEPALIVE_VALS in configure.  See PostgreSQL's source code
> (src/backend/libpq/pqcomm.c etc.)
>
> > pg_syn_count           = 3     # TCP_SYNCNT
> >
> >
> > TCP_SYNCNT work on linux?
>
> Don't know. Anyway pg_syn_count is not used anywhere else in your
> patch. Is it intended?
> --
> Tatsuo Ishii
> SRA OSS, Inc. Japan
> English: http://www.sraoss.co.jp/index_en.php
> Japanese: http://www.sraoss.co.jp
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.sraoss.jp/pipermail/pgpool-hackers/attachments/20131114/310bcc8b/attachment.html>
-------------- next part --------------
diff --git a/pool_connection_pool.c b/pool_connection_pool.c
index 47faac9..0f1f7b4 100644
--- a/pool_connection_pool.c
+++ b/pool_connection_pool.c
@@ -532,15 +535,60 @@ int connect_inet_domain_socket_by_port(char *host, int port, bool retry)
 	}
 
 	/* set nodelay */
-	if (setsockopt(fd, IPPROTO_TCP, TCP_NODELAY,
-				   (char *) &on,
-				   sizeof(on)) < 0)
+	if (setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, (char *) &on, sizeof(on)) < 0)
 	{
 		pool_error("connect_inet_domain_socket_by_port: setsockopt() failed: %s", strerror(errno));
 		close(fd);
 		return -1;
 	}
 
+	/* set keepalive */
+   on = 1;
+	if (setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE, (char *) &on, sizeof(on)) < 0)
+	{
+		pool_error("connect_inet_domain_socket_by_port: setsockopt() failed: %s", strerror(errno));
+		close(fd);
+		return -1;
+	}
+	else
+	{
+	    int keepalive_idle     = pool_config->pg_keepalives_idle;
+	    int keepalive_count    = pool_config->pg_keepalives_count;
+	    int keepalive_interval = pool_config->pg_keepalives_interval;
+
+#ifdef TCP_KEEPIDLE
+        if (keepalive_idle && setsockopt(fd, SOL_TCP, TCP_KEEPIDLE, &keepalive_idle, sizeof(int)) < 0)
+            pool_error("connect_inet_domain_socket_by_port: setsockopt(TCP_KEEPIDLE) failed: %s", strerror(errno));
+#else
+        if (keepalive_idle && setsockopt(fd, SOL_TCP, TCP_KEEPALIVE, &keepalive_idle, sizeof(int)) < 0)
+            pool_error("connect_inet_domain_socket_by_port: setsockopt(TCP_KEEPALIVE) failed: %s", strerror(errno));
+#endif
+
+#ifdef TCP_KEEPCNT
+        if (keepalive_count && setsockopt(fd, SOL_TCP, TCP_KEEPCNT, &keepalive_count, sizeof(int)) < 0)
+            pool_error("connect_inet_domain_socket_by_port: setsockopt(TCP_KEEPCNT) failed: %s", strerror(errno));
+#else
+        if (keepalive_count)
+            pool_error("connect_inet_domain_socket_by_port: setsockopt(TCP_KEEPCNT) not supported");
+#endif
+
+#if defined(TCP_KEEPINTVL) || defined (SIO_KEEPALIVE_VALS)
+        if (keepalive_interval && setsockopt(fd, SOL_TCP, TCP_KEEPINTVL, &keepalive_interval, sizeof(int)) < 0)
+            pool_error("connect_inet_domain_socket_by_port: setsockopt(TCP_KEEPINTVL) failed: %s", strerror(errno));
+#else
+        if (keepalive_interval)
+            pool_error("connect_inet_domain_socket_by_port: setsockopt(TCP_KEEPINTVL) not supported");
+#endif
+    }
+
+#ifdef TCP_SYNCNT
+    if (pool_config->pg_syn_count && setsockopt(fd, SOL_TCP, TCP_SYNCNT, &pool_config->pg_syn_count, sizeof(int)) < 0)
+        pool_error("connect_inet_domain_socket_by_port: setsockopt(TCP_SYNCNT) failed: %s", strerror(errno));
+#else
+    if (pool_config->pg_syn_count)
+        pool_error("connect_inet_domain_socket_by_port: setsockopt(TCP_SYNCNT) not supported");
+#endif
+
 	memset((char *) &addr, 0, sizeof(addr));
 	addr.sin_family = AF_INET;
 


More information about the pgpool-hackers mailing list