JDK-8203194 : Add support for per Socket configuration of TCP keepalive
  • Type: CSR
  • Component: core-libs
  • Sub-Component: java.net
  • Priority: P3
  • Status: Closed
  • Resolution: Approved
  • Fix Versions: 11
  • Submitted: 2018-05-15
  • Updated: 2018-05-26
  • Resolved: 2018-05-26
Related Reports
CSR :  
Description
Summary
-------

TCP_KEEPIDLE, TCP_KEEPCOUNT, and TCP_KEEPINTERVAL are non-standard socket options supported on several platforms to provide fine control over the TCP/IP keep alive mechanism. It should be possible to set these socket options via the setOption method defined by java.net.Socket and java.nio.channels.SocketChannel.

Problem
-------

When the SO_KEEPALIVE option is enabled, TCP probes a connection that has been idle for some amount of time.  The default value for this idle period is 2 hours which is too long for most applications. The TCP_KEEPIDLE, TCP_KEEPCOUNT, TCP_KEEPINTERVAL option can be used to affect this value for a given socket.

Solution
-------

Add a JDK-specific socket option that supports setting TCP_KEEPIDLE, TCP_KEEPCOUNT, TCP_KEEPINTERVAL, on platforms that support it. The option can be set/get through the existing set/getOption methods on Socket and NetworkChannel.

Specification
-------

To jdk.net.ExtendedSocketOptions, in the jdk.net module, add the following new socket options:


    /**
     * Keep-Alive idle time.
     *
     * <p>
     * The value of this socket option is an {@code Integer} that is the number
     * of seconds of idle time before keep-alive initiates a probe. The socket
     * option is specific to stream-oriented sockets using the TCP/IP protocol.
     * The exact semantics of this socket option are system dependent.
     *
     * <p>
     * When the {@link java.net.StandardSocketOptions#SO_KEEPALIVE
     * SO_KEEPALIVE} option is enabled, TCP probes a connection that has been
     * idle for some amount of time. The default value for this idle period is
     * system dependent, but is typically 2 hours. The {@code TCP_KEEPIDLE}
     * option can be used to affect this value for a given socket.
     *
     * @since 11
     */
    public static final SocketOption<Integer> TCP_KEEPIDLE

    /**
     * Keep-Alive retransmission interval time.
     *
     * <p>
     * The value of this socket option is an {@code Integer} that is the number
     * of seconds to wait before retransmitting a keep-alive probe. The socket
     * option is specific to stream-oriented sockets using the TCP/IP protocol.
     * The exact semantics of this socket option are system dependent.
     *
     * <p>
     * When the {@link java.net.StandardSocketOptions#SO_KEEPALIVE
     * SO_KEEPALIVE} option is enabled, TCP probes a connection that has been
     * idle for some amount of time. If the remote system does not respond to a
     * keep-alive probe, TCP retransmits the probe after some amount of time.
     * The default value for this retransmission interval is system dependent,
     * but is typically 75 seconds. The {@code TCP_KEEPINTERVAL} option can be
     * used to affect this value for a given socket.
     *
     * @since 11
     */
    public static final SocketOption<Integer> TCP_KEEPINTERVAL

    /**
     * Keep-Alive retransmission maximum limit.
     *
     * <p>
     * The value of this socket option is an {@code Integer} that is the maximum
     * number of keep-alive probes to be sent. The socket option is specific to
     * stream-oriented sockets using the TCP/IP protocol. The exact semantics of
     * this socket option are system dependent.
     *
     * <p>
     * When the {@link java.net.StandardSocketOptions#SO_KEEPALIVE
     * SO_KEEPALIVE} option is enabled, TCP probes a connection that has been
     * idle for some amount of time. If the remote system does not respond to a
     * keep-alive probe, TCP retransmits the probe a certain number of times
     * before a connection is considered to be broken. The default value for
     * this keep-alive probe retransmit limit is system dependent, but is
     * typically 8. The {@code TCP_KEEPCOUNT} option can be used to affect this
     * value for a given socket.
     *
     * @since 11
     */
    public static final SocketOption<Integer> TCP_KEEPCOUNT

Comments
The SocketOptions, even the standard ones, aren't very explicit about how out-of-range values are handled. I'd generally prefer to see that more explicitly documented here, what should happen for zero and negative values, etc., but I'm voting to approve as it is locally consistent with other parts of the related API.
26-05-2018