You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@trafficserver.apache.org by jp...@apache.org on 2015/11/16 23:01:42 UTC

[2/2] trafficserver git commit: TS-2523: automatically max out the listen backlog

TS-2523: automatically max out the listen backlog

Automatically detect and set the listen backlog to the system
maximum, falling back to the previous default for compatibility.


Project: http://git-wip-us.apache.org/repos/asf/trafficserver/repo
Commit: http://git-wip-us.apache.org/repos/asf/trafficserver/commit/b2c697be
Tree: http://git-wip-us.apache.org/repos/asf/trafficserver/tree/b2c697be
Diff: http://git-wip-us.apache.org/repos/asf/trafficserver/diff/b2c697be

Branch: refs/heads/master
Commit: b2c697be5f351ee1bdd77c76165ed2d71a24343e
Parents: 10574ba
Author: James Peach <jp...@apache.org>
Authored: Thu Oct 15 21:03:25 2015 -0700
Committer: James Peach <jp...@apache.org>
Committed: Mon Nov 16 14:00:55 2015 -0800

----------------------------------------------------------------------
 configure.ac                                |  2 +-
 doc/admin-guide/files/records.config.en.rst |  7 +++++
 iocore/net/Connection.cc                    |  4 +--
 lib/ts/ink_inet.cc                          | 33 ++++++++++++++++++++++++
 lib/ts/ink_inet.h                           |  3 +++
 lib/ts/ink_platform.h                       |  3 +--
 mgmt/LocalManager.cc                        |  8 +++---
 mgmt/RecordsConfig.cc                       |  2 +-
 8 files changed, 51 insertions(+), 11 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/trafficserver/blob/b2c697be/configure.ac
----------------------------------------------------------------------
diff --git a/configure.ac b/configure.ac
index f3d7283..317f962 100644
--- a/configure.ac
+++ b/configure.ac
@@ -1149,7 +1149,7 @@ if test "x${enable_xml}" != "xyes"; then
 fi
 
 AC_CHECK_FUNCS([clock_gettime kqueue epoll_ctl posix_memalign posix_fadvise posix_madvise posix_fallocate inotify_init])
-AC_CHECK_FUNCS([lrand48_r srand48_r port_create strlcpy strlcat sysconf getpagesize])
+AC_CHECK_FUNCS([lrand48_r srand48_r port_create strlcpy strlcat sysconf sysctlbyname getpagesize])
 AC_CHECK_FUNCS([getreuid getresuid getresgid setreuid setresuid getpeereid getpeerucred])
 AC_CHECK_FUNCS([strsignal psignal psiginfo])
 

http://git-wip-us.apache.org/repos/asf/trafficserver/blob/b2c697be/doc/admin-guide/files/records.config.en.rst
----------------------------------------------------------------------
diff --git a/doc/admin-guide/files/records.config.en.rst b/doc/admin-guide/files/records.config.en.rst
index f59658a..d313b78 100644
--- a/doc/admin-guide/files/records.config.en.rst
+++ b/doc/admin-guide/files/records.config.en.rst
@@ -2850,6 +2850,13 @@ Sockets
    This directive enables operating system specific optimizations for a listening socket. ``defer_accept`` holds a call to ``accept(2)``
    back until data has arrived. In Linux' special case this is up to a maximum of 45 seconds.
 
+.. ts:cv:: CONFIG proxy.config.net.listen_backlog INT -1
+   :reloadable:
+
+  This directive sets the maximum number of pending connections.
+  If it is set to -1, Traffic Server will automatically set this
+  to a platform-specific maximum.
+
 .. ts:cv:: CONFIG proxy.config.net.sock_send_buffer_size_in INT 0
 
    Sets the send buffer size for connections from the client to Traffic Server.

http://git-wip-us.apache.org/repos/asf/trafficserver/blob/b2c697be/iocore/net/Connection.cc
----------------------------------------------------------------------
diff --git a/iocore/net/Connection.cc b/iocore/net/Connection.cc
index 53e6d55..ebdafd5 100644
--- a/iocore/net/Connection.cc
+++ b/iocore/net/Connection.cc
@@ -46,10 +46,10 @@
 int
 get_listen_backlog(void)
 {
-  int listen_backlog = 1024;
+  int listen_backlog;
 
   REC_ReadConfigInteger(listen_backlog, "proxy.config.net.listen_backlog");
-  return listen_backlog;
+  return listen_backlog >= 0 ? listen_backlog : ats_tcp_somaxconn();
 }
 
 

http://git-wip-us.apache.org/repos/asf/trafficserver/blob/b2c697be/lib/ts/ink_inet.cc
----------------------------------------------------------------------
diff --git a/lib/ts/ink_inet.cc b/lib/ts/ink_inet.cc
index 850afd1..7856de4 100644
--- a/lib/ts/ink_inet.cc
+++ b/lib/ts/ink_inet.cc
@@ -28,6 +28,7 @@
 #include "ts/ink_code.h"
 #include "ts/ink_assert.h"
 #include "ts/TestBox.h"
+#include "ts/TextBuffer.h"
 
 #if defined(darwin)
 extern "C" {
@@ -634,3 +635,35 @@ REGRESSION_TEST(Ink_Inet)(RegressionTest *t, int /* atype */, int *pstatus)
     }
   }
 }
+
+int
+ats_tcp_somaxconn()
+{
+  int fd;
+  int value;
+
+/* Darwin version ... */
+#if HAVE_SYSCTLBYNAME
+  if (sysctlbyname("kern.ipc.somaxconn", NULL, NULL, &value, sizeof(value)) == 0) {
+    return value;
+  }
+#endif
+
+  fd = open("/proc/sys/net/ipv4/tcp_max_syn_backlog", O_RDONLY);
+  if (fd != -1) {
+    textBuffer text(0);
+    text.slurp(fd);
+    if (!text.empty()) {
+      value = strtoul(text.bufPtr(), NULL, 10);
+    }
+    close(fd);
+  }
+
+  // Default to the compatible value we used before detection. SOMAXCONN is the right
+  // macro to use, but most systems set this to 128, which is just too small.
+  if (value <= 0) {
+    return 1024;
+  }
+
+  return value;
+}

http://git-wip-us.apache.org/repos/asf/trafficserver/blob/b2c697be/lib/ts/ink_inet.h
----------------------------------------------------------------------
diff --git a/lib/ts/ink_inet.h b/lib/ts/ink_inet.h
index b149f9e..1754bd6 100644
--- a/lib/ts/ink_inet.h
+++ b/lib/ts/ink_inet.h
@@ -149,6 +149,9 @@ struct hostent *ink_gethostbyname_r(char *hostname, ink_gethostbyname_r_data *da
 */
 struct hostent *ink_gethostbyaddr_r(char *ip, int len, int type, ink_gethostbyaddr_r_data *data);
 
+/** Return the detected maximum listen(2) backlog for TCP. */
+int ats_tcp_somaxconn();
+
 /** Parse a string for pieces of an IP address.
 
     This doesn't parse the actual IP address, but picks it out from @a

http://git-wip-us.apache.org/repos/asf/trafficserver/blob/b2c697be/lib/ts/ink_platform.h
----------------------------------------------------------------------
diff --git a/lib/ts/ink_platform.h b/lib/ts/ink_platform.h
index 75d129a..90105c3 100644
--- a/lib/ts/ink_platform.h
+++ b/lib/ts/ink_platform.h
@@ -177,11 +177,10 @@ typedef unsigned int in_addr_t;
 #include <sys/sysinfo.h>
 #endif
 
-#if !defined(darwin)
 #ifdef HAVE_SYS_SYSCTL_H
 #include <sys/sysctl.h>
 #endif
-#endif
+
 #ifdef HAVE_SYS_SYSTEMINFO_H
 #include <sys/systeminfo.h>
 #endif

http://git-wip-us.apache.org/repos/asf/trafficserver/blob/b2c697be/mgmt/LocalManager.cc
----------------------------------------------------------------------
diff --git a/mgmt/LocalManager.cc b/mgmt/LocalManager.cc
index 1775f22..6fc1391 100644
--- a/mgmt/LocalManager.cc
+++ b/mgmt/LocalManager.cc
@@ -984,12 +984,10 @@ LocalManager::listenForProxy()
     }
 
     // read backlong configuration value and overwrite the default value if found
-    int backlog = 1024;
     bool found;
-    RecInt config_backlog = REC_readInteger("proxy.config.net.listen_backlog", &found);
-    if (found) {
-      backlog = config_backlog;
-    }
+    RecInt backlog = REC_readInteger("proxy.config.net.listen_backlog", &found);
+    backlog = (found && backlog >= 0) ? backlog : ats_tcp_somaxconn();
+
     if ((listen(p.m_fd, backlog)) < 0) {
       mgmt_fatal(stderr, errno, "[LocalManager::listenForProxy] Unable to listen on port: %d (%s)\n", p.m_port,
                  ats_ip_family_name(p.m_family));

http://git-wip-us.apache.org/repos/asf/trafficserver/blob/b2c697be/mgmt/RecordsConfig.cc
----------------------------------------------------------------------
diff --git a/mgmt/RecordsConfig.cc b/mgmt/RecordsConfig.cc
index dc60715..7064df0 100644
--- a/mgmt/RecordsConfig.cc
+++ b/mgmt/RecordsConfig.cc
@@ -805,7 +805,7 @@ static const RecordElement RecordsConfig[] =
   //##############################################################################
   {RECT_CONFIG, "proxy.config.net.connections_throttle", RECD_INT, "30000", RECU_RESTART_TS, RR_REQUIRED, RECC_STR, "^[0-9]+$", RECA_NULL}
   ,
-  {RECT_CONFIG, "proxy.config.net.listen_backlog", RECD_INT, "1024", RECU_NULL, RR_NULL, RECC_NULL, NULL, RECA_NULL}
+  {RECT_CONFIG, "proxy.config.net.listen_backlog", RECD_INT, "-1", RECU_NULL, RR_NULL, RECC_NULL, NULL, RECA_NULL}
   ,
   // This option takes different defaults depending on features / platform. TODO: This should use the
   // autoconf stuff probably ?