Skip to content

Commit 39abd92

Browse files
authored
ethstats: use timer instead of time.sleep (#20924)
1 parent 45b7535 commit 39abd92

1 file changed

Lines changed: 71 additions & 62 deletions

File tree

‎ethstats/ethstats.go‎

Lines changed: 71 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -203,77 +203,86 @@ func (s *Service) loop() {
203203
if !strings.Contains(path, "://") {
204204
urls = []string{"wss://" + path, "ws://" + path}
205205
}
206+
207+
errTimer := time.NewTimer(0)
208+
defer errTimer.Stop()
206209
// Loop reporting until termination
207210
for {
208-
// Establish a websocket connection to the server on any supported URL
209-
var (
210-
conn *websocket.Conn
211-
err error
212-
)
213-
dialer := websocket.Dialer{HandshakeTimeout: 5 * time.Second}
214-
header := make(http.Header)
215-
header.Set("origin", "http://localhost")
216-
for _, url := range urls {
217-
conn, _, err = dialer.Dial(url, header)
218-
if err == nil {
219-
break
211+
select {
212+
case <-quitCh:
213+
return
214+
case <-errTimer.C:
215+
// Establish a websocket connection to the server on any supported URL
216+
var (
217+
conn *websocket.Conn
218+
err error
219+
)
220+
dialer := websocket.Dialer{HandshakeTimeout: 5 * time.Second}
221+
header := make(http.Header)
222+
header.Set("origin", "http://localhost")
223+
for _, url := range urls {
224+
conn, _, err = dialer.Dial(url, header)
225+
if err == nil {
226+
break
227+
}
220228
}
221-
}
222-
if err != nil {
223-
log.Warn("Stats server unreachable", "err", err)
224-
time.Sleep(10 * time.Second)
225-
continue
226-
}
227-
// Authenticate the client with the server
228-
if err = s.login(conn); err != nil {
229-
log.Warn("Stats login failed", "err", err)
230-
conn.Close()
231-
time.Sleep(10 * time.Second)
232-
continue
233-
}
234-
go s.readLoop(conn)
235-
236-
// Send the initial stats so our node looks decent from the get go
237-
if err = s.report(conn); err != nil {
238-
log.Warn("Initial stats report failed", "err", err)
239-
conn.Close()
240-
continue
241-
}
242-
// Keep sending status updates until the connection breaks
243-
fullReport := time.NewTicker(15 * time.Second)
229+
if err != nil {
230+
log.Warn("Stats server unreachable", "err", err)
231+
errTimer.Reset(10 * time.Second)
232+
continue
233+
}
234+
// Authenticate the client with the server
235+
if err = s.login(conn); err != nil {
236+
log.Warn("Stats login failed", "err", err)
237+
conn.Close()
238+
errTimer.Reset(10 * time.Second)
239+
continue
240+
}
241+
go s.readLoop(conn)
244242

245-
for err == nil {
246-
select {
247-
case <-quitCh:
248-
fullReport.Stop()
249-
// Make sure the connection is closed
243+
// Send the initial stats so our node looks decent from the get go
244+
if err = s.report(conn); err != nil {
245+
log.Warn("Initial stats report failed", "err", err)
250246
conn.Close()
251-
return
247+
errTimer.Reset(0)
248+
continue
249+
}
250+
// Keep sending status updates until the connection breaks
251+
fullReport := time.NewTicker(15 * time.Second)
252252

253-
case <-fullReport.C:
254-
if err = s.report(conn); err != nil {
255-
log.Warn("Full stats report failed", "err", err)
256-
}
257-
case list := <-s.histCh:
258-
if err = s.reportHistory(conn, list); err != nil {
259-
log.Warn("Requested history report failed", "err", err)
260-
}
261-
case head := <-headCh:
262-
if err = s.reportBlock(conn, head); err != nil {
263-
log.Warn("Block stats report failed", "err", err)
264-
}
265-
if err = s.reportPending(conn); err != nil {
266-
log.Warn("Post-block transaction stats report failed", "err", err)
267-
}
268-
case <-txCh:
269-
if err = s.reportPending(conn); err != nil {
270-
log.Warn("Transaction stats report failed", "err", err)
253+
for err == nil {
254+
select {
255+
case <-quitCh:
256+
fullReport.Stop()
257+
// Make sure the connection is closed
258+
conn.Close()
259+
return
260+
261+
case <-fullReport.C:
262+
if err = s.report(conn); err != nil {
263+
log.Warn("Full stats report failed", "err", err)
264+
}
265+
case list := <-s.histCh:
266+
if err = s.reportHistory(conn, list); err != nil {
267+
log.Warn("Requested history report failed", "err", err)
268+
}
269+
case head := <-headCh:
270+
if err = s.reportBlock(conn, head); err != nil {
271+
log.Warn("Block stats report failed", "err", err)
272+
}
273+
if err = s.reportPending(conn); err != nil {
274+
log.Warn("Post-block transaction stats report failed", "err", err)
275+
}
276+
case <-txCh:
277+
if err = s.reportPending(conn); err != nil {
278+
log.Warn("Transaction stats report failed", "err", err)
279+
}
271280
}
272281
}
282+
fullReport.Stop()
283+
// Make sure the connection is closed
284+
conn.Close()
273285
}
274-
fullReport.Stop()
275-
// Make sure the connection is closed
276-
conn.Close()
277286
}
278287
}
279288

0 commit comments

Comments
 (0)