Golang Conn.setDeadline仅用于下一个操作还是永远?
在下面的代码中:
conn.SetDeadline(time.Now().Add(time.Minute))
//read/write operation, only once
conn.SetDeadline(time.Time{}) //cancel deadline
“取消”操作是否需要?即,如果我不取消它,连接<strong>将在一分钟内超时,无论操作是否成功/失败?
或者,setDeadline命令仅适用于立即操作的立即操作,并且该操作成功后,截止日期自动消失了吗?
In the code below:
conn.SetDeadline(time.Now().Add(time.Minute))
//read/write operation, only once
conn.SetDeadline(time.Time{}) //cancel deadline
Is the "cancel" operation necessary? i.e. If I do not cancel it, the connection will timeout in one minute, no matter if following operation succeeds/fails?
Or, the SetDeadline command only applys to the immediate following operation, and after that operation succeeds, the deadline automatically vanishes?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
请参阅
conn
接口如前所述,截止日期适用于“所有未来” I/O。如果您在截止日期之后使用连接,您将获得“包装
os.errdeadlineExceeded
>的错误”。如果您希望再次使用连接,则需要将截止日期设置为未来或零值(即conn.setDeadline(time.time {})
)。重要的是要注意,超出截止日期不会关闭连接。收到
OS.ERRDEADLINEEXCEEDED
后,您仍然可以设置新的截止日期(或清除截止日期)并继续发送/接收。您不应该假设接收错误意味着连接已关闭(如果您想要的话,请确保您调用collect()
)。See the comments for the
Conn
interface
As stated the deadline applies to "all future" I/O. If you use the connection after the deadline has passed you will get "an error that wraps
os.ErrDeadlineExceeded
". If you wish to use the connection again you will need to set the deadline to a future or zero value (i.e.conn.SetDeadline(time.Time{})
).It is important to note that exceeding the deadline does not close the connection. After receiving an
os.ErrDeadlineExceeded
you can still set a new deadline (or clear the deadline) and continue to send/receive. You should not assume that receiving an error means the connection is closed (if that is what you want ensure you callclose()
).