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,77 +203,86 @@ 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 {
// Establish a websocket connection to the server on any supported URL select {
var ( case <-quitCh:
conn *websocket.Conn return
err error case <-errTimer.C:
) // Establish a websocket connection to the server on any supported URL
dialer := websocket.Dialer{HandshakeTimeout: 5 * time.Second} var (
header := make(http.Header) conn *websocket.Conn
header.Set("origin", "http://localhost") err error
for _, url := range urls { )
conn, _, err = dialer.Dial(url, header) dialer := websocket.Dialer{HandshakeTimeout: 5 * time.Second}
if err == nil { header := make(http.Header)
break header.Set("origin", "http://localhost")
for _, url := range urls {
conn, _, err = dialer.Dial(url, header)
if err == nil {
break
}
} }
} if err != nil {
if err != nil { log.Warn("Stats server unreachable", "err", err)
log.Warn("Stats server unreachable", "err", err) errTimer.Reset(10 * time.Second)
time.Sleep(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()
time.Sleep(10 * time.Second)
continue
}
go s.readLoop(conn)
// Send the initial stats so our node looks decent from the get go
if err = s.report(conn); err != nil {
log.Warn("Initial stats report failed", "err", err)
conn.Close()
continue
}
// Keep sending status updates until the connection breaks
fullReport := time.NewTicker(15 * time.Second)
for err == nil {
select {
case <-quitCh:
fullReport.Stop()
// Make sure the connection is closed
conn.Close() conn.Close()
return errTimer.Reset(10 * time.Second)
continue
}
go s.readLoop(conn)
case <-fullReport.C: // Send the initial stats so our node looks decent from the get go
if err = s.report(conn); err != nil { if err = s.report(conn); err != nil {
log.Warn("Full stats report failed", "err", err) log.Warn("Initial stats report failed", "err", err)
} conn.Close()
case list := <-s.histCh: errTimer.Reset(0)
if err = s.reportHistory(conn, list); err != nil { continue
log.Warn("Requested history report failed", "err", err) }
} // Keep sending status updates until the connection breaks
case head := <-headCh: fullReport := time.NewTicker(15 * time.Second)
if err = s.reportBlock(conn, head); err != nil {
log.Warn("Block stats report failed", "err", err) for err == nil {
} select {
if err = s.reportPending(conn); err != nil { case <-quitCh:
log.Warn("Post-block transaction stats report failed", "err", err) fullReport.Stop()
} // Make sure the connection is closed
case <-txCh: conn.Close()
if err = s.reportPending(conn); err != nil { return
log.Warn("Transaction stats report failed", "err", err)
case <-fullReport.C:
if err = s.report(conn); err != nil {
log.Warn("Full stats report failed", "err", err)
}
case list := <-s.histCh:
if err = s.reportHistory(conn, list); err != nil {
log.Warn("Requested history report failed", "err", err)
}
case head := <-headCh:
if err = s.reportBlock(conn, head); err != nil {
log.Warn("Block stats report failed", "err", err)
}
if err = s.reportPending(conn); err != nil {
log.Warn("Post-block transaction stats report failed", "err", err)
}
case <-txCh:
if err = s.reportPending(conn); err != nil {
log.Warn("Transaction stats report failed", "err", err)
}
} }
} }
fullReport.Stop()
// Make sure the connection is closed
conn.Close()
} }
fullReport.Stop()
// Make sure the connection is closed
conn.Close()
} }
} }