Created
November 13, 2018 08:17
-
-
Save eiyaya/234aff48772bfb1680e0f5501066e83b to your computer and use it in GitHub Desktop.
How to know TCP connection is closed in Golang net package
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
one := []byte{} | |
c.SetReadDeadline(time.Now()) | |
if _, err := c.Read(one); err == io.EOF { | |
l.Printf(logger.LevelDebug, "%s detected closed LAN connection", id) | |
c.Close() | |
c = nil | |
} else { | |
var zero time.Time | |
c.SetReadDeadline(time.Now().Add(10 * time.Millisecond)) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
For detecting a timeout, it suggests:
if neterr, ok := err.(net.Error); ok && neterr.Timeout() {
...