禁用 golang TCP 中的截止日期



我想在客户端连接上设置一个截止日期,他必须在前 10 秒内做某事,否则就会断开连接,如果他确实做了某事,我想删除截止日期。

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

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

另外,当满足某个条件时,继续更改截止日期是否安全?

要重置截止时间,您可以在文档停留时使用"零"值调用SetDeadline。这个"零"值可以设置为:

conn.SetDeadline(time.Time{})

它指出:

// 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.

最新更新