Skip to content

Commit

Permalink
ethstats: stop report ticker in each loop cycle #21070 (#21071)
Browse files Browse the repository at this point in the history
Co-authored-by: Hao Duan <[email protected]>
  • Loading branch information
duanhao0814 and duanhaohyperchain authored May 13, 2020
1 parent d02301f commit a188a1e
Showing 1 changed file with 13 additions and 10 deletions.
23 changes: 13 additions & 10 deletions ethstats/ethstats.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ func (s *Service) loop() {
txSub := txpool.SubscribeNewTxsEvent(txEventCh)
defer txSub.Unsubscribe()

// Start a goroutine that exhausts the subsciptions to avoid events piling up
// Start a goroutine that exhausts the subscriptions to avoid events piling up
var (
quitCh = make(chan struct{})
headCh = make(chan *types.Block, 1)
Expand Down Expand Up @@ -194,16 +194,17 @@ func (s *Service) loop() {
}
close(quitCh)
}()

// Resolve the URL, defaulting to TLS, but falling back to none too
path := fmt.Sprintf("%s/api", s.host)
urls := []string{path}

// url.Parse and url.IsAbs is unsuitable (https://github.com/golang/go/issues/19779)
if !strings.Contains(path, "://") {
urls = []string{"wss://" + path, "ws://" + path}
}
// Loop reporting until termination
for {
// Resolve the URL, defaulting to TLS, but falling back to none too
path := fmt.Sprintf("%s/api", s.host)
urls := []string{path}

// url.Parse and url.IsAbs is unsuitable (https://github.com/golang/go/issues/19779)
if !strings.Contains(path, "://") {
urls = []string{"wss://" + path, "ws://" + path}
}
// Establish a websocket connection to the server on any supported URL
var (
conn *websocket.Conn
Expand Down Expand Up @@ -240,11 +241,12 @@ func (s *Service) loop() {
}
// Keep sending status updates until the connection breaks
fullReport := time.NewTicker(15 * time.Second)
defer fullReport.Stop()

for err == nil {
select {
case <-quitCh:
fullReport.Stop()
// Make sure the connection is closed
conn.Close()
return

Expand All @@ -269,6 +271,7 @@ func (s *Service) loop() {
}
}
}
fullReport.Stop()
// Make sure the connection is closed
conn.Close()
}
Expand Down

0 comments on commit a188a1e

Please sign in to comment.