Unverified Commit 39abd92c authored by ucwong's avatar ucwong Committed by GitHub

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

parent 45b75351
...@@ -203,77 +203,86 @@ func (s *Service) loop() { ...@@ -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()
conn.Close() errTimer.Reset(10 * time.Second)
time.Sleep(10 * time.Second) continue
continue }
} go s.readLoop(conn)
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 { // Send the initial stats so our node looks decent from the get go
select { if err = s.report(conn); err != nil {
case <-quitCh: log.Warn("Initial stats report failed", "err", err)
fullReport.Stop()
// Make sure the connection is closed
conn.Close() conn.Close()
return errTimer.Reset(0)
continue
}
// Keep sending status updates until the connection breaks
fullReport := time.NewTicker(15 * time.Second)
case <-fullReport.C: for err == nil {
if err = s.report(conn); err != nil { select {
log.Warn("Full stats report failed", "err", err) case <-quitCh:
} fullReport.Stop()
case list := <-s.histCh: // Make sure the connection is closed
if err = s.reportHistory(conn, list); err != nil { conn.Close()
log.Warn("Requested history report failed", "err", err) return
}
case head := <-headCh: case <-fullReport.C:
if err = s.reportBlock(conn, head); err != nil { if err = s.report(conn); err != nil {
log.Warn("Block stats report failed", "err", err) log.Warn("Full stats report failed", "err", err)
} }
if err = s.reportPending(conn); err != nil { case list := <-s.histCh:
log.Warn("Post-block transaction stats report failed", "err", err) if err = s.reportHistory(conn, list); err != nil {
} log.Warn("Requested history report failed", "err", err)
case <-txCh: }
if err = s.reportPending(conn); err != nil { case head := <-headCh:
log.Warn("Transaction stats report failed", "err", err) 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()
} }
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment