net.keepalive和http.idletimeout在golang中有什么区别?

问题描述 投票:-1回答:1
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连接可以保持多长时间?

http go network-programming
1个回答
3
投票

术语“保持活动”在两种情况下意味着不同的事物。

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

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

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

[Dialer.KeepAlive字段指定将TCP保持活动状态探测发送到对等体的频率。

这两个设置在堆栈的不同层上执行不同的操作。

© www.soinside.com 2019 - 2024. All rights reserved.