在golang中,默认的HTTP拨号超时值是多少?

huangapple go评论73阅读模式
英文:

Default HTTP dial timeout value in golang

问题

我正在运行一个用于压力测试服务器的 Golang HTTP 客户端。有时候我会遇到 "dial tcp 161.170.xx.xxx:80: operation timed out" 错误。

我认为这是一个 HTTP 客户端超时错误。我打算根据 https://stackoverflow.com/a/16895878/198497 中的建议增加超时时间,但我想先找出 Golang 中默认的超时时间是多少。如果这个超时时间取决于操作系统而不是语言,我该如何在 Mac OS 中检查这个值呢?

英文:

I am running a golang http client to stress test a server. Sometimes I got error "dial tcp 161.170.xx.xxx:80: operation timed out" error.

I think this is a HTTP client timeout. I am thinking about increase the timeout value based on https://stackoverflow.com/a/16895878/198497, but I would like to find out what's the default timeout value in golang first. If it is depends on os instead of language, how can I check this value in Mac OS?

答案1

得分: 10

根据http://golang.org/pkg/net/#Dialer:

Dialer类型有一个Timeout字段,它表示连接完成的最长等待时间。如果还设置了Deadline字段,可能会在此之前失败。

默认情况下,没有设置超时时间。

无论是否设置超时时间,操作系统可能会施加自己的较早超时限制。例如,TCP超时通常约为3分钟。

可以使用SetDeadline方法设置超时时间。

默认的OSX超时时间(我认为)可以通过sysctl net.inet.tcp来检查。

英文:

According to http://golang.org/pkg/net/#Dialer :

type Dialer struct {
        // Timeout is the maximum amount of time a dial will wait for
        // a connect to complete. If Deadline is also set, it may fail
        // earlier.
        //
        // The default is no timeout.
        //
        // With or without a timeout, the operating system may impose
        // its own earlier timeout. For instance, TCP timeouts are
        // often around 3 minutes.

So the default timeout, without taking into account OS imposed limits is none.

The timeout can be set with SetDeadline.

The default OSX timeout can (I think) be checked with sysctl net.inet.tcp.

huangapple
  • 本文由 发表于 2013年10月31日 08:39:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/19696448.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定