net.Dialer#KeepAlive 和 http.Transport#IdleTimeout 有什么区别?

What is the difference between net.Dialer#KeepAlive and http.Transport#IdleTimeout?

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
}

我认为keep-alive是tcp连接应该保持的时间。但是 IdleConnTimeout 似乎是同一回事。那么它们之间有什么区别,如果我都设置这些变量,tcp连接可以保持多长时间?

keep-alive 一词在两种上下文中的含义不同。

net/http 传输文档使用术语来指代 persistent connections。保持活动或持久连接是一种可用于多个 HTTP 事务的连接。

Transport.IdleConnTimeout 字段指定传输在关闭连接之前在池中保留未使用的连接的时间。

网络拨号程序文档使用保持活动术语来指代 TCP feature for probing the health of a connection

Dialer.KeepAlive 字段指定向对等方发送 TCP 保持活动探测的频率。

这两个设置在堆栈的不同层做不同的事情。