Skip to content

Instantly share code, notes, and snippets.

@ggzeng
Last active October 21, 2019 11:33
Show Gist options
  • Save ggzeng/3c5b4100d6a5c9c056516351e5acd3fb to your computer and use it in GitHub Desktop.
Save ggzeng/3c5b4100d6a5c9c056516351e5acd3fb to your computer and use it in GitHub Desktop.
检测TCP/UDP连接超时
m.Conn.SetReadDeadline(time.Now().Add(delay))
for {
n, err := m.Conn.Read(buffer)
if err != nil {
log.Println(err)
}
if n > 0 {
// something was read before the deadline
// let's delay the deadline
m.Conn.SetReadDeadline(time.Now().Add(delay))
}
if nerr, ok := err.(net.Error); ok && nerr.Timeout() {
// Timeout error
fmt.Println("No response")
m.Kill <- true
return
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment