likexian / whois

Whois client for domain and ip whois information query in Go(Golang).
Apache License 2.0
405 stars 76 forks source link

关于 Clinet Timeout 设置无效的问题 #43

Open yb opened 6 months ago

yb commented 6 months ago

Hi @likexian,

我遇到了和 #37 相同的问题,经过测试和检查 whois.go 的源代码,我猜测:

之所以会得出这个猜测,是因为今晚我所在的网络无法连接到 whois.nic.work 这个服务器,以下为 ping 表现:

PING whois.nic.work(2600:1f13:dde:c501::1:5 (2600:1f13:dde:c501::1:5)) 56 data bytes
^C
--- whois.nic.work ping statistics ---
2 packets transmitted, 0 received, 100% packet loss, time 1016ms

然后看了一下 net 包中关于 Dialer.Timeout 的解释,好像确实与连接时的超时有关:

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.
    //
    // When using TCP and dialing a host name with multiple IP
    // addresses, the timeout may be divided between them.
    //
    // With or without a timeout, the operating system may impose
    // its own earlier timeout. For instance, TCP timeouts are
    // often around 3 minutes.
    Timeout [time](https://pkg.go.dev/time).[Duration](https://pkg.go.dev/time#Duration)

......

EDIT: 补充一个复现方法:

whois.DefaultClient.SetTimeout(time.Second * 5)
result, err := whois.Whois("example.com", "www.google.com")

如此,即可复现 5 秒超时的设置并没起作用,连接 www.google.com:43 要等到 30 秒后才超时。


由于这是第一次接触 go 语言,如果猜测错误,请见谅 感谢

likexian commented 6 months ago

Hello @yb

感谢反馈,你是专业的! 我看看这个问题怎么处理,感觉不太好搞,应该是得引入 ctx 了。


If you think this repository is helpful, please share it with friends, thanks.

yb commented 6 months ago

Hi @likexian

我现在使用 connect, err := net.DialTimeout("tcp", address, timeout) 这个方式重写了 whois func,可以解决连接时的 timeout 问题,但我不确定能否与你设计的 c.dialer 合并,仅供参考

jafayer commented 3 months ago

Hello @likexian, I have opened a pull request to address this issue. It is not the cleanest way to address the problem, but might help provide a quick and dirty way forward. As suggested in the PR, we may need to make additional changes to SetTimout to make this a reliable change - but those changes might cause backwards compatibility issues with some uses of the whois package. I welcome feedback and understand if this is not the direction you would like to go in!