From ddad4cbd32da4f2bc0b06aefd1ec7b1ded18d909 Mon Sep 17 00:00:00 2001 From: Steve Singer Date: Thu, 6 Jan 2011 15:24:12 -0500 Subject: [PATCH 3/3] Bug 126. This change turns TCP_KEEP alive on by default. It also provides configuration in the slon.conf file for keep alive, keep alive interval, keep alive count and the keep alive idle interval. Other than turning keep alive on/off this change has not yet been implemen on Win32 --- doc/adminguide/slonconf.sgml | 51 +++++++++++++++++++++++++++++++++ src/slon/confoptions.c | 64 ++++++++++++++++++++++++++++++++++++++++- src/slon/confoptions.h | 4 ++ src/slon/dbutils.c | 44 ++++++++++++++++++++++++++++ 4 files changed, 161 insertions(+), 2 deletions(-) diff --git a/doc/adminguide/slonconf.sgml b/doc/adminguide/slonconf.sgml index abd6464..ed8d61c 100644 --- a/doc/adminguide/slonconf.sgml +++ b/doc/adminguide/slonconf.sgml @@ -209,6 +209,57 @@ + + tcp_keepalive (bool) + tcp_keepalive + + + Enables sending of TCP KEEP alive requests between slon and the + PostgreSQL backends. Defaults to true. + + + + + tcp_keepalive_idle (integer) + tcp_keepalive_idle + + + The number of seconds of idle activity after which a TCP KEEPALIVE + will be sent across the network. The tcp_keepalive parameter + must be enabled for this to take effect. The default value is 0 + which means use the operating systems default. Setting this parameter + has no effect on Win32 systems. + + + + + + tcp_keepalive_count ( integer) + tcp_keepalive_count + + + The number of keep alive requets to the server that need to be lost + before the connection is declared dead. tcp_keep_alive must be turned + on for this parameter to take effect. The default value is 0 + which means use the operating systems default. Setting this parameter + has no effect on Win32 systems. + + + + + + tcp_keepalive_interval (integer) + tcp_keepalive_interval + + + The number of seconds between TCP keep alive requests. tcp_keepalive + must be enabled for this parameter to take effect. The default + value is 0 which means use the operating systems default. + Setting this parameter has no effect on Win32 systems. + + + + diff --git a/src/slon/confoptions.c b/src/slon/confoptions.c index 34cf738..a18c18f 100644 --- a/src/slon/confoptions.c +++ b/src/slon/confoptions.c @@ -17,9 +17,7 @@ static int conf_name_compare(const char *namea, const char *nameb); bool set_config_option(const char *name, const char *value); void *get_config_option(const char *name); -static bool bool_placeholder; static double real_placeholder; -static char *string_placeholder; void dump_configuration(void); @@ -724,6 +722,54 @@ static struct config_int ConfigureNamesInt[] = }, + { + { + (const char*) "tcp_keepalive_idle", + gettext_noop("The number of seconds after which a TCP keep alive " + "is sent across an idle connection. tcp_keepalive " + "must be enabled for this to take effect. Default " + "of 0 means use operating system default" + "use default" ), + NULL, + SLON_C_INT, + }, + &keep_alive_idle, + 0, /*default val */ + 0, /* min val */ + 1073741824 /*max val*/ + }, + { + { + (const char*) "tcp_keepalive_interval", + gettext_noop("The number of seconds in between TCP keep alive " + "requests. tcp_keepalive " + "must be enabled. Default value of 0 means use " + "operating system defaut"), + NULL, + SLON_C_INT, + }, + &keep_alive_interval, + 0, + 0, /* min val */ + 1073741824 /*max val*/ + }, + { + { + (const char*) "tcp_keepalive_count", + gettext_noop("The number of keep alive requests to the server " + "that can be lost before the connection is declared " + "dead. tcp_keep_alive must be on. Default value " + "of 0 means use operating system default"), + NULL, + SLON_C_INT, + }, + &keep_alive_count, + 0, + 0, /* min val */ + 1073741824 /*max val*/ + }, + + {{0}} }; @@ -750,6 +796,18 @@ static struct config_bool ConfigureNamesBool[] = true }, + { + { + (const char*) "tcp_keepalive", + gettext_noop("Enables sending of TCP KEEP alive between slon " + "and the PostgreSQL backends. "), + NULL, + SLON_C_BOOL, + }, + &keep_alive, + true + }, + {{0}} }; @@ -901,6 +959,8 @@ static struct config_string ConfigureNamesString[] = &cleanup_interval, "10 minutes" }, + + {{0}} }; diff --git a/src/slon/confoptions.h b/src/slon/confoptions.h index 76e1d48..3affdaf 100644 --- a/src/slon/confoptions.h +++ b/src/slon/confoptions.h @@ -27,6 +27,10 @@ extern int desired_sync_time; extern int quit_sync_provider; extern int quit_sync_finalsync; +extern bool keep_alive; +extern int keep_alive_idle; +extern int keep_alive_interval; +extern int keep_alive_count; /* * ---------- diff --git a/src/slon/dbutils.c b/src/slon/dbutils.c index 1c171c4..21a6509 100644 --- a/src/slon/dbutils.c +++ b/src/slon/dbutils.c @@ -22,9 +22,15 @@ #include #include #include +#include +#include #include "slon.h" +bool keep_alive; +int keep_alive_idle; +int keep_alive_count; +int keep_alive_interval; static int slon_appendquery_int(SlonDString *dsp, char *fmt, va_list ap); static int db_get_version(PGconn *conn); @@ -81,6 +87,44 @@ slon_connectdb(char *conninfo, char *symname) return NULL; } + setsockopt(PQsocket(dbconn),SOL_SOCKET,SO_KEEPALIVE,&keep_alive, + sizeof(int)); +#ifndef WIN32 + if(keep_alive) + { + + if(keep_alive_idle > 0) + setsockopt(PQsocket(dbconn),IPPROTO_TCP,TCP_KEEPIDLE, + &keep_alive_idle,sizeof(keep_alive_idle)); + if(keep_alive_interval > 0) + setsockopt(PQsocket(dbconn),IPPROTO_TCP,TCP_KEEPINTVL, + &keep_alive_interval,sizeof(keep_alive_interval)); + if(keep_alive_count > 0) + setsockopt(PQsocket(dbconn),IPPROTO_TCP,TCP_KEEPCNT, + &keep_alive_count,sizeof(keep_alive_count)); + + } +#else + /** + * Win32 does not support the setsockopt calls for setting keep alive + * parameters. On Win32 this can be adjusted via the registry. + * libpq 9.0 and above provide functions for doing this. + * If we ever require libpq9.0 or above we could start to use them. + * Alternativly someone could re-implement that functionality inside + * of slony. + */ + if(keep_alive) + { + if(keep_alive_idle > 0 ) + slon_log(SLON_WARN,"keep_alive_idle is not supported by Slony on Win32"); + if(keep_alive_interval > 0) + slon_slog(SLON_WARN,"keep_alive_interval is not supported by Slony on Win32"); + if(keep_alive_count > 0) + slon_log(SLON_WARN,"keep_alive_count is not supported by Slony Win32"); + + } +#endif + dstring_init(&query); if (sql_on_connection != NULL) -- 1.7.0.4