gpt4 book ai didi

go - go http.Client 的 DialContext KeepAlive 和 Transport IdleTimeout 的区别

转载 作者:行者123 更新时间:2023-12-01 19:20:23 29 4
gpt4 key购买 nike

client := &http.Client{
Transport: &http.Transport{
DialContext: (&net.Dialer{
KeepAlive: 5 * time.Second,
}).DialContext,
IdleConnTimeout: 3 * time.Second,
},
}

KeepAlive 和 IdleConnTimeout 有什么区别?

他们中的哪一个究竟是做什么的?

最佳答案

从文档:
https://github.com/golang/go/blob/go1.13.8/src/net/dial.go#L72

KeepAlive specifies the interval between keep-alive probes for an active network connection. If zero, keep-alive probes are sent with a default value (currently 15 seconds), 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-alive probes are disabled.


所以,如果你设置了 KeepAlive大于 0,它将表示从下一次探测开始所耗时,以便了解网络连接是否仍处于事件状态。
另一边, IdleConnTimeout :
https://github.com/golang/go/blob/go1.13.8/src/net/dial.go#L72

IdleConnTimeout is the maximum amount of time an idle (keep-alive) connection will remain idle before closing itself. Zero means no limit.


所以,如果你设置了 IdleConnTimeout大于0,表示连接仍然​​打开的时间。

关于go - go http.Client 的 DialContext KeepAlive 和 Transport IdleTimeout 的区别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46686360/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com