Skip to content

Instantly share code, notes, and snippets.

@eiyaya
Created November 13, 2018 08:17
Show Gist options
  • Save eiyaya/234aff48772bfb1680e0f5501066e83b to your computer and use it in GitHub Desktop.
Save eiyaya/234aff48772bfb1680e0f5501066e83b to your computer and use it in GitHub Desktop.
How to know TCP connection is closed in Golang net package
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))
}
@eiyaya
Copy link
Author

eiyaya commented Nov 13, 2018

For detecting a timeout, it suggests:
if neterr, ok := err.(net.Error); ok && neterr.Timeout() {
...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment