禁用golang TCP中的截止日期

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

Disabling a deadline in golang TCP

问题

我想在客户端连接上设置一个截止时间,他必须在前10秒内执行某些操作,否则将被断开连接。如果他确实执行了某些操作,我想取消截止时间。

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

但是文档中没有提到如何禁用截止时间。

另外,当满足某个条件时,频繁更改截止时间是否安全?

英文:

I want to set a deadline on client connection, he must do something within the first 10 seconds or else get disconnected, if he does do something, I want to remove the deadline.

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

But the documentation doesn't say anything about disabling the deadline.

Also, is it safe to keep changing the deadline when a certain condition is met?

答案1

得分: 9

要重置截止日期,您可以按照文档所述,调用SetDeadline并将一个“零”值传递给它。可以使用以下代码设置“零”值:

conn.SetDeadline(time.Time{})
英文:

To reset the deadline, you can call SetDeadline with a "zero" value as the docs stay. This the "zero" value can be set with:

conn.SetDeadline(time.Time{})

答案2

得分: 4

它说明:

// SetReadDeadline设置未来读取调用的截止时间。
// t的零值表示读取不会超时。
SetReadDeadline(t time.Time) error

SetReadDeadline的文档中。

所以当客户端发送你期望的内容时,你需要传入零值。

SetDeadline表示它同时设置读取器和写入器,所以请确保你也想设置写入器。

// SetDeadline设置与连接关联的读取和写入截止时间。
// 它等效于同时调用SetReadDeadline和SetWriteDeadline。

英文:

It states:

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

In the documentation for SetReadDeadLine

so you will need to pass in zero when a client sends what you expect.

and the SetDeadLine says it is setting both the reader and the writer, so make sure you also meant to set the writer.

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

huangapple
  • 本文由 发表于 2016年3月11日 03:25:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/35925449.html
匿名

发表评论

匿名网友

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

确定