Skip to content

Commit a188a1e

Browse files
ethstats: stop report ticker in each loop cycle #21070 (#21071)
Co-authored-by: Hao Duan <[email protected]>
1 parent d02301f commit a188a1e

File tree

1 file changed

+13
-10
lines changed

1 file changed

+13
-10
lines changed

ethstats/ethstats.go

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ func (s *Service) loop() {
154154
txSub := txpool.SubscribeNewTxsEvent(txEventCh)
155155
defer txSub.Unsubscribe()
156156

157-
// Start a goroutine that exhausts the subsciptions to avoid events piling up
157+
// Start a goroutine that exhausts the subscriptions to avoid events piling up
158158
var (
159159
quitCh = make(chan struct{})
160160
headCh = make(chan *types.Block, 1)
@@ -194,16 +194,17 @@ func (s *Service) loop() {
194194
}
195195
close(quitCh)
196196
}()
197+
198+
// Resolve the URL, defaulting to TLS, but falling back to none too
199+
path := fmt.Sprintf("%s/api", s.host)
200+
urls := []string{path}
201+
202+
// url.Parse and url.IsAbs is unsuitable (https://github.com/golang/go/issues/19779)
203+
if !strings.Contains(path, "://") {
204+
urls = []string{"wss://" + path, "ws://" + path}
205+
}
197206
// Loop reporting until termination
198207
for {
199-
// Resolve the URL, defaulting to TLS, but falling back to none too
200-
path := fmt.Sprintf("%s/api", s.host)
201-
urls := []string{path}
202-
203-
// url.Parse and url.IsAbs is unsuitable (https://github.com/golang/go/issues/19779)
204-
if !strings.Contains(path, "://") {
205-
urls = []string{"wss://" + path, "ws://" + path}
206-
}
207208
// Establish a websocket connection to the server on any supported URL
208209
var (
209210
conn *websocket.Conn
@@ -240,11 +241,12 @@ func (s *Service) loop() {
240241
}
241242
// Keep sending status updates until the connection breaks
242243
fullReport := time.NewTicker(15 * time.Second)
243-
defer fullReport.Stop()
244244

245245
for err == nil {
246246
select {
247247
case <-quitCh:
248+
fullReport.Stop()
249+
// Make sure the connection is closed
248250
conn.Close()
249251
return
250252

@@ -269,6 +271,7 @@ func (s *Service) loop() {
269271
}
270272
}
271273
}
274+
fullReport.Stop()
272275
// Make sure the connection is closed
273276
conn.Close()
274277
}

0 commit comments

Comments
 (0)