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