在golang TCP中禁用截止日期

时间:2016-03-10 19:25:07

标签: time go timeout

我想在客户端连接上设置截止日期,他必须在前10秒内做一些事情或者断开连接,如果他确实做了什么,我想删除截止日期。

// meConn = *TCPConn
c.meConn.SetDeadline(time.Now().Add(10 * time.Second))

但是文档没有说明禁用截止日期的事情。

此外,在满足特定条件时,保持更改截止日期是否安全?

2 个答案:

答案 0 :(得分:4)

它声明:

// SetReadDeadline sets the deadline for future Read calls.
// A zero value for t means Read will not time out.
SetReadDeadline(t time.Time) error

SetReadDeadLine

的文档中

因此,当客户端发送您期望的内容时,您需要传入零。

并且SetDeadLine表示它正在设置读者和作者,因此请确保您还要设置作者。

   // SetDeadline sets the read and write deadlines associated
   // with the connection. It is equivalent to calling both
   // SetReadDeadline and SetWriteDeadline.

答案 1 :(得分:1)

要重置截止日期,您可以在文档停留期间以“零”值调用SetDeadline。可以通过以下方式设置“零”值:

conn.SetDeadline(time.Time{})