Golang Conn.setDeadline仅用于下一个操作还是永远?

发布于 2025-02-12 14:49:44 字数 339 浏览 1 评论 0原文

在下面的代码中:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

如果没有 2025-02-19 14:49:44

请参阅 conn 接口

//截止日期是绝对的时间,之后i/o操作
//失败而不是阻止。截止日期适用于所有未来
//和待定的I/O,不仅是在致电
之后的立即
//读或写。超出截止日期后,
//可以通过在将来设置截止日期来刷新连接。

如前所述,截止日期适用于“所有未来” I/O。如果您在截止日期之后使用连接,您将获得“包装os.errdeadlineExceeded>的错误”。如果您希望再次使用连接,则需要将截止日期设置为未来或零值(即conn.setDeadline(time.time {}))。

重要的是要注意,超出截止日期不会关闭连接。收到OS.ERRDEADLINEEXCEEDED后,您仍然可以设置新的截止日期(或清除截止日期)并继续发送/接收。您不应该假设接收错误意味着连接已关闭(如果您想要的话,请确保您调用collect())。

See the comments for the Conn interface

// A deadline is an absolute time after which I/O operations
// fail instead of blocking. The deadline applies to all future
// and pending I/O, not just the immediately following call to
// Read or Write. After a deadline has been exceeded, the
// connection can be refreshed by setting a deadline in the future.

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 call close()).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文