一种Go使用tcp检测超时的方式

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))
}
For detecting a timeout, it suggests:

if neterr, ok := err.(net.Error); ok && neterr.Timeout() {
  //......
}

Source Page: http://*.com/a/12741495

 
上一篇:李洪强iOS经典面试题141-报错警告调试


下一篇:bzoj 2286 [Sdoi2011]消耗战(虚树+树上DP)