ethstats: use timer instead of time.sleep (#20924)

This commit is contained in:
ucwong 2020-06-08 19:27:08 +08:00 committed by GitHub
parent 45b7535137
commit 39abd92ca8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -203,8 +203,15 @@ func (s *Service) loop() {
if !strings.Contains(path, "://") {
urls = []string{"wss://" + path, "ws://" + path}
}
errTimer := time.NewTimer(0)
defer errTimer.Stop()
// Loop reporting until termination
for {
select {
case <-quitCh:
return
case <-errTimer.C:
// Establish a websocket connection to the server on any supported URL
var (
conn *websocket.Conn
@ -221,14 +228,14 @@ func (s *Service) loop() {
}
if err != nil {
log.Warn("Stats server unreachable", "err", err)
time.Sleep(10 * time.Second)
errTimer.Reset(10 * time.Second)
continue
}
// Authenticate the client with the server
if err = s.login(conn); err != nil {
log.Warn("Stats login failed", "err", err)
conn.Close()
time.Sleep(10 * time.Second)
errTimer.Reset(10 * time.Second)
continue
}
go s.readLoop(conn)
@ -237,6 +244,7 @@ func (s *Service) loop() {
if err = s.report(conn); err != nil {
log.Warn("Initial stats report failed", "err", err)
conn.Close()
errTimer.Reset(0)
continue
}
// Keep sending status updates until the connection breaks
@ -275,6 +283,7 @@ func (s *Service) loop() {
// Make sure the connection is closed
conn.Close()
}
}
}
// readLoop loops as long as the connection is alive and retrieves data packets