Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
yb opened this issue May 13, 2024 · 2 comments
Open

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

yb opened this issue May 13, 2024 · 2 comments

Comments

@yb
Copy link

yb commented May 13, 2024

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 语言,如果猜测错误,请见谅
感谢

@likexian
Copy link
Owner

likexian commented May 24, 2024

Hello @yb

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


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

@yb
Copy link
Author

yb commented May 25, 2024

Hi @likexian

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants