whois icon indicating copy to clipboard operation
whois copied to clipboard

关于 Clinet Timeout 设置无效的问题

Open yb opened this issue 1 year ago • 3 comments

Hi @likexian,

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

  • 因为 SetTimeout() 只设置了 c.timeout,而 c.dialer.Timeout 没有被更新,它在 NewClient() 时已经被固定为 30 秒;
  • 所以如果超时发生在 c.dialer.Dial() 则 timeout 无效;

之所以会得出这个猜测,是因为今晚我所在的网络无法连接到 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 语言,如果猜测错误,请见谅 感谢

yb avatar May 13 '24 17:05 yb

Hello @yb

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


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

likexian avatar May 24 '24 13:05 likexian

Hi @likexian

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

yb avatar May 25 '24 16:05 yb

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!

jafayer avatar Aug 29 '24 21:08 jafayer