Try to improve handling of timeouts

This commit is contained in:
Neil Alexander 2019-07-17 11:13:53 +01:00
parent 2532cd77e4
commit 747b50bb7c
No known key found for this signature in database
GPG key ID: A02A2019A2BB0944
2 changed files with 21 additions and 13 deletions

View file

@ -52,26 +52,29 @@ func (s *tunConn) reader() error {
default:
}
s.tun.log.Debugln("Starting conn reader for", s)
defer s.tun.log.Debugln("Stopping conn reader for", s)
var n int
var err error
read := make(chan bool)
b := make([]byte, 65535)
go func() {
s.tun.log.Debugln("Starting conn reader helper for", s)
defer s.tun.log.Debugln("Stopping conn reader helper for", s)
for {
s.conn.SetReadDeadline(time.Now().Add(tunConnTimeout))
if n, err = s.conn.Read(b); err != nil {
s.tun.log.Errorln(s.conn.String(), "TUN/TAP conn read error:", err)
if e, eok := err.(yggdrasil.ConnError); eok {
s.tun.log.Debugln("Conn reader helper", s, "error:", e)
switch {
case e.Temporary():
fallthrough
case e.Timeout():
read <- false
continue
case e.Timeout():
s.tun.log.Debugln("Conn reader for helper", s, "timed out")
case e.Closed():
fallthrough
default:
s.tun.log.Debugln("Stopping conn reader helper for", s)
s.close()
return
}
@ -94,7 +97,6 @@ func (s *tunConn) reader() error {
}
s.stillAlive() // TODO? Only stay alive if we read >0 bytes?
case <-s.stop:
s.tun.log.Debugln("Stopping conn reader for", s)
return nil
}
}
@ -109,10 +111,10 @@ func (s *tunConn) writer() error {
default:
}
s.tun.log.Debugln("Starting conn writer for", s)
defer s.tun.log.Debugln("Stopping conn writer for", s)
for {
select {
case <-s.stop:
s.tun.log.Debugln("Stopping conn writer for", s)
return nil
case b, ok := <-s.send:
if !ok {