tun2: move pingloop into a function

This commit is contained in:
Cadey Ratio 2017-03-26 22:19:43 -07:00
parent 1c221cdf6d
commit 03f9570151
1 changed files with 45 additions and 41 deletions

View File

@ -156,8 +156,16 @@ func (s *Server) ListenAndServe() error {
go func() { go func() {
for { for {
time.Sleep(5 * time.Second) time.Sleep(5 * time.Second)
s.pingLoopInner()
}
}()
return nil
}
func (s *Server) pingLoopInner() {
s.connlock.Lock() s.connlock.Lock()
defer s.connlock.Unlock()
for _, c := range s.conns { for _, c := range s.conns {
c.session.SetDeadline(time.Now().Add(time.Second)) c.session.SetDeadline(time.Now().Add(time.Second))
@ -171,35 +179,31 @@ func (s *Server) ListenAndServe() error {
if err != nil { if err != nil {
ln.Error(err, c.F()) ln.Error(err, c.F())
c.cancel() c.cancel()
continue return
} }
err = req.Write(stream) err = req.Write(stream)
if err != nil { if err != nil {
ln.Error(err, c.F()) ln.Error(err, c.F())
c.cancel() c.cancel()
continue return
} }
_, err = stream.Read(make([]byte, 30)) _, err = stream.Read(make([]byte, 30))
if err != nil { if err != nil {
ln.Error(err, c.F()) ln.Error(err, c.F())
c.cancel() c.cancel()
continue return
} }
stream.Close() stream.Close()
/*
ln.Log(ln.F{ ln.Log(ln.F{
"action": "ping_health_is_good", "action": "ping_health_is_good",
}, c.F()) }, c.F())
*/
} }
s.connlock.Unlock()
}
}()
return nil
} }
func (s *Server) HandleConn(c net.Conn, isKCP bool) { func (s *Server) HandleConn(c net.Conn, isKCP bool) {
@ -381,7 +385,7 @@ func (s *Server) RoundTrip(req *http.Request) (*http.Response, error) {
Request: req, Request: req,
} }
return resp, nil return resp, errors.New("no backend connected")
} }
} }