Unblocking a blocked read or write operation
Sometimes, you need to unblock a read or write operation based on an external event. This recipe shows how you can unblock such an I/O operation.
How to do it...
- If you want to unblock an I/O operation with no intention of reusing the connection again, close the connection asynchronously:
cancel:=make(chan struct{}) done:=make(chan struct{}) // Close the connection if a message is sent to cancel channel go func() { Â Â Â select { Â Â Â Â Â Â case <-cancel: Â Â Â Â Â Â Â Â Â conn.Close() Â Â Â Â Â Â case <-done: Â Â Â } }() go handleConnection(conn) - If you want to unblock an I/O operation but not terminate it, set the deadline to now:
unblock:=make(chan struct{}) // Unblock the connection if a message is sent to unblock channel go func() {   <-unblock   conn.SetDeadline(time.Now()) }() timedout...