diff --git a/lib/tun2/server.go b/lib/tun2/server.go index f9e4c64..45c3eb6 100644 --- a/lib/tun2/server.go +++ b/lib/tun2/server.go @@ -156,52 +156,56 @@ func (s *Server) ListenAndServe() error { go func() { for { time.Sleep(5 * time.Second) - - s.connlock.Lock() - - for _, c := range s.conns { - c.session.SetDeadline(time.Now().Add(time.Second)) - - req, err := http.NewRequest("GET", "http://backend/health", nil) - if err != nil { - panic(err) - } - - stream, err := c.session.OpenStream() - if err != nil { - ln.Error(err, c.F()) - c.cancel() - continue - } - - err = req.Write(stream) - if err != nil { - ln.Error(err, c.F()) - c.cancel() - continue - } - - _, err = stream.Read(make([]byte, 30)) - if err != nil { - ln.Error(err, c.F()) - c.cancel() - continue - } - - stream.Close() - - ln.Log(ln.F{ - "action": "ping_health_is_good", - }, c.F()) - } - - s.connlock.Unlock() + s.pingLoopInner() } }() return nil } +func (s *Server) pingLoopInner() { + s.connlock.Lock() + defer s.connlock.Unlock() + + for _, c := range s.conns { + c.session.SetDeadline(time.Now().Add(time.Second)) + + req, err := http.NewRequest("GET", "http://backend/health", nil) + if err != nil { + panic(err) + } + + stream, err := c.session.OpenStream() + if err != nil { + ln.Error(err, c.F()) + c.cancel() + return + } + + err = req.Write(stream) + if err != nil { + ln.Error(err, c.F()) + c.cancel() + return + } + + _, err = stream.Read(make([]byte, 30)) + if err != nil { + ln.Error(err, c.F()) + c.cancel() + return + } + + stream.Close() + + /* + ln.Log(ln.F{ + "action": "ping_health_is_good", + }, c.F()) + */ + } +} + func (s *Server) HandleConn(c net.Conn, isKCP bool) { ctx, cancel := context.WithCancel(context.Background()) defer cancel() @@ -381,7 +385,7 @@ func (s *Server) RoundTrip(req *http.Request) (*http.Response, error) { Request: req, } - return resp, nil + return resp, errors.New("no backend connected") } }