-2
type Dialer struct {
    ......
    // KeepAlive specifies the keep-alive period for an active
    // network connection.
    // If zero, keep-alives are enabled if supported by the protocol
    // and operating system. Network protocols or operating systems
    // that do not support keep-alives ignore this field.
    // If negative, keep-alives are disabled.
    KeepAlive time.Duration
}
type Transport struct {
    ......
// IdleConnTimeout is the maximum amount of time an idle
    // (keep-alive) connection will remain idle before closing
    // itself.
    // Zero means no limit.
    IdleConnTimeout time.Duration
}

I think keep-alive is a time that the tcp connection shoud keep. But IdleConnTimeout seems like the same thing. So what the difference between them and if I both set up these varible, the tcp connection can keep how long?

Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189
kongshuo
  • 25
  • 6

1 Answers1

5

The term keep-alive means different things in the two contexts.

The net/http Transport documentation uses the term to refer to persistent connections. A keep-alive or persistent connection is a connection that can be used for more than one HTTP transaction.

The Transport.IdleConnTimeout field specifies how long the transport keeps an unused connection in the pool before closing the connection.

The net Dialer documentation uses the keep-alive term to refer the TCP feature for probing the health of a connection.

Dialer.KeepAlive field specifies how frequently TCP keep-alive probes are sent to the peer.

The two settings do different things at different layers in the stack.

Charlie Tumahai
  • 113,709
  • 12
  • 249
  • 242
  • Thanks! So Dialer.KeepAlive is uesd to probe tcp connection health period, and Transport.IdleConnTimeout is how long can I reuse the tcp connection. The first one is work TCP and the second one is work on HTTP. Is it right? – kongshuo Jan 09 '20 at 02:47