majidarif
majidarif

Reputation: 20075

Disabling a deadline in golang TCP

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?

Upvotes: 1

Views: 5586

Answers (2)

Derick
Derick

Reputation: 36794

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{})

Upvotes: 11

animal
animal

Reputation: 238

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.

Upvotes: 4

Related Questions