TS-3119: add SO_LINGER socket option support The timeout should be 0 if we turn on SO_LINGER option as we are using non-blocking socket. I have tried a non-zero timeout, but the setsockopt failed.
If we add a new sock option static unit32_t const SOCK_OPT_LINGER_ON = 4; Then we could configure proxy.config.net.sock_option_flag_in proxy.config.net.sock_option_flag_out to make client connection or server connection LINGER for 0 ms if needed. Project: http://git-wip-us.apache.org/repos/asf/trafficserver/repo Commit: http://git-wip-us.apache.org/repos/asf/trafficserver/commit/e5069f1b Tree: http://git-wip-us.apache.org/repos/asf/trafficserver/tree/e5069f1b Diff: http://git-wip-us.apache.org/repos/asf/trafficserver/diff/e5069f1b Branch: refs/heads/master Commit: e5069f1b6249bcd62f5bf1a425ed16926ac2bbba Parents: 9475268 Author: Kang Li <[email protected]> Authored: Wed Oct 29 17:04:03 2014 -0700 Committer: James Peach <[email protected]> Committed: Wed Oct 29 17:07:41 2014 -0700 ---------------------------------------------------------------------- CHANGES | 3 +++ doc/reference/configuration/records.config.en.rst | 18 +++++++++++++----- iocore/net/I_NetVConnection.h | 2 ++ iocore/net/UnixConnection.cc | 7 +++++++ 4 files changed, 25 insertions(+), 5 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/trafficserver/blob/e5069f1b/CHANGES ---------------------------------------------------------------------- diff --git a/CHANGES b/CHANGES index a1ff377..a4144bc 100644 --- a/CHANGES +++ b/CHANGES @@ -1,6 +1,9 @@ -*- coding: utf-8 -*- Changes with Apache Traffic Server 5.2.0 + *) [TS-3119] Add SO_LINGER socket option support. + Author: Kang Li <[email protected]> + *) [TS-3157] Standardize --help and --version arguments across tools. *) [TS-3143] Create new Regex class that uses PCRE JIT. http://git-wip-us.apache.org/repos/asf/trafficserver/blob/e5069f1b/doc/reference/configuration/records.config.en.rst ---------------------------------------------------------------------- diff --git a/doc/reference/configuration/records.config.en.rst b/doc/reference/configuration/records.config.en.rst index 03d55e6..3560b7f 100644 --- a/doc/reference/configuration/records.config.en.rst +++ b/doc/reference/configuration/records.config.en.rst @@ -2410,8 +2410,9 @@ Sockets Turns different options "on" for the socket handling client connections::: - TCP_NODELAY (1) + TCP_NODELAY (1) SO_KEEPALIVE (2) + SO_LINGER (4) .. note:: @@ -2432,14 +2433,21 @@ Sockets Turns different options "on" for the origin server socket::: - TCP_NODELAY (1) + TCP_NODELAY (1) SO_KEEPALIVE (2) + SO_LINGER (4) .. note:: - This is a flag and you look at the bits set. Therefore, - you must set the value to ``3`` if you want to enable both options - above. + This is a flag and you look at the bits set. Therefore, you + must set the value to ``3`` if you want to enable both + options above. + + When SO_LINGER is enabled, the linger timeout time is set + to 0. This is useful when ATS and origin server were installed + This is useful when Traffic Server and the origin server + are co-located and large numbers of sockets are retained + in the TIME_WAIT state. .. ts:cv:: CONFIG proxy.config.net.sock_mss_in INT 0 http://git-wip-us.apache.org/repos/asf/trafficserver/blob/e5069f1b/iocore/net/I_NetVConnection.h ---------------------------------------------------------------------- diff --git a/iocore/net/I_NetVConnection.h b/iocore/net/I_NetVConnection.h index 0927144..71cd1fc 100644 --- a/iocore/net/I_NetVConnection.h +++ b/iocore/net/I_NetVConnection.h @@ -153,6 +153,8 @@ struct NetVCOptions { static uint32_t const SOCK_OPT_NO_DELAY = 1; /// Value for keep alive for @c sockopt_flags. static uint32_t const SOCK_OPT_KEEP_ALIVE = 2; + /// Value for linger on for @c sockopt_flags + static uint32_t const SOCK_OPT_LINGER_ON = 4; uint32_t packet_mark; uint32_t packet_tos; http://git-wip-us.apache.org/repos/asf/trafficserver/blob/e5069f1b/iocore/net/UnixConnection.cc ---------------------------------------------------------------------- diff --git a/iocore/net/UnixConnection.cc b/iocore/net/UnixConnection.cc index ae1f5e7..d738c5e 100644 --- a/iocore/net/UnixConnection.cc +++ b/iocore/net/UnixConnection.cc @@ -370,6 +370,13 @@ Connection::apply_options(NetVCOptions const& opt) safe_setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE, SOCKOPT_ON, sizeof(int)); Debug("socket", "::open: setsockopt() SO_KEEPALIVE on socket"); } + if (opt.sockopt_flags & NetVCOptions::SOCK_OPT_LINGER_ON) { + struct linger l; + l.l_onoff = 1; + l.l_linger = 0; + safe_setsockopt(fd, SOL_SOCKET, SO_LINGER, (char *)&l, sizeof(l)); + Debug("socket", "::open:: setsockopt() turn on SO_LINGER on socket"); + } } #if TS_HAS_SO_MARK
