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,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")
}
}