判断 net.Listener 是否已关闭

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

Tell whether net.Listener is dead

问题

在Go语言中,调用net.Listener类型的Accept方法会返回一个错误。然而,有没有办法区分临时错误(例如,连接设置失败)和永久错误(例如,监听器已经关闭,比如强制删除的Unix域套接字文件)?如果我无法区分这两种错误,就有可能陷入无限循环,并且每次调用Accept都会立即返回一个错误,导致错误不断输出。

英文:

In Go, a call to the net.Listener type's Accept method returns an error. However, is there a way to tell the difference between a transient error (ie, this connection failed to set up) vs a permanent error (ie, the listener is dead, such as a Unix domain socket file that was forcibly removed)? If I can't tell the difference, I run the risk of infinite looping and spitting out errors as fast as I can since each Accept call will immediately return an error.

答案1

得分: 2

我明白了。net 包返回的错误可能是 net.Error 类型,该类型定义了 Temporary() bool 方法,用于判断错误是否是临时的。

英文:

Figured it out. Errors returned by the net package may be of the net.Error type, which defines the Temporary() bool method which reports whether the error is temporary.

huangapple
  • 本文由 发表于 2015年8月21日 02:44:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/32125980.html
匿名

发表评论

匿名网友

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

确定