Skip to content

Commit

Permalink
pubsub/explorer: leave the HubRelay channels opened
Browse files Browse the repository at this point in the history
There are multiple goroutines that send to HubRelay.  The WebsocketHub
is the receiver, but there are other senders.
Closing the channel is not necessary to terminate the run loops.

Also, to quit the run loops, Stop() now closes quitWSHandler instead of
sending on it.  There are NO senders, only a receiver.
  • Loading branch information
chappjc committed Jun 5, 2019
1 parent da5b141 commit 5fa10b5
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 25 deletions.
14 changes: 4 additions & 10 deletions explorer/websocket.go
Original file line number Diff line number Diff line change
Expand Up @@ -193,10 +193,9 @@ func (wsh *WebsocketHub) pingClients() chan<- struct{} {
// Stop kills the run() loop and unregisters all clients (connections).
func (wsh *WebsocketHub) Stop() {
// End the run() loop, allowing in-progress operations to complete.
wsh.quitWSHandler <- struct{}{}
// Lastly close the hub relay channel sine the quitWSHandler signal is
// handled in the Run loop.
close(wsh.HubRelay)
close(wsh.quitWSHandler)
// Do not close HubRelay since there are multiple senders; run() is the
// receiver.
}

func (wsh *WebsocketHub) run() {
Expand Down Expand Up @@ -274,12 +273,7 @@ func (wsh *WebsocketHub) run() {
case c := <-wsh.Unregister:
wsh.unregisterClient(c)

case _, ok := <-wsh.quitWSHandler:
if !ok {
log.Error("close channel already closed. This should not happen.")
return
}
close(wsh.quitWSHandler)
case <-wsh.quitWSHandler:

// End the buffer interval send loop.
wsh.bufferTickerChan <- tickerSigStop
Expand Down
20 changes: 5 additions & 15 deletions pubsub/websocket.go
Original file line number Diff line number Diff line change
Expand Up @@ -283,10 +283,7 @@ func (wsh *WebsocketHub) pingClients() chan<- struct{} {
select {
case <-ticker.C:
wsh.HubRelay <- pstypes.HubMessage{Signal: sigPingAndUserCount}
case _, ok := <-stopPing:
if ok {
log.Errorf("Do not send on stopPing channel, only close it.")
}
case <-stopPing:
return
}
}
Expand All @@ -298,10 +295,9 @@ func (wsh *WebsocketHub) pingClients() chan<- struct{} {
// Stop kills the run() loop and unregisters all clients (connections).
func (wsh *WebsocketHub) Stop() {
// End the run() loop, allowing in progress operations to complete.
wsh.quitWSHandler <- struct{}{}
// Lastly close the hub relay channel sine the quitWSHandler signal is
// handled in the Run loop.
close(wsh.HubRelay)
close(wsh.quitWSHandler)
// Do not close HubRelay since there are multiple senders; Run() is the
// receiver.
}

// Run starts the main event loop, which handles the following: 1. receiving
Expand Down Expand Up @@ -429,13 +425,7 @@ func (wsh *WebsocketHub) Run() {
case c := <-wsh.Unregister:
wsh.unregisterClient(c)

case _, ok := <-wsh.quitWSHandler:
if !ok {
log.Error("close channel already closed. This should not happen.")
return
}
close(wsh.quitWSHandler)

case <-wsh.quitWSHandler:
// End the buffer interval send loop,
wsh.bufferTickerChan <- tickerSigStop

Expand Down

0 comments on commit 5fa10b5

Please sign in to comment.