From d4a14af14d9e7c38e19f59d0c4e844326b8c6e68 Mon Sep 17 00:00:00 2001 From: "Marcelo E. Magallon" Date: Wed, 14 Oct 2015 18:48:43 -0600 Subject: [PATCH] Simplify websocket ticker shutdown code "A receive from a closed channel returns the zero value immediately" Close the tickerChan in the calling function, this causes "case <-c" to unblock immediately, ending the goroutine and stopping the ticker. --- middleware/websocket/websocket.go | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/middleware/websocket/websocket.go b/middleware/websocket/websocket.go index f344fe511..76b2bfed8 100644 --- a/middleware/websocket/websocket.go +++ b/middleware/websocket/websocket.go @@ -172,7 +172,7 @@ func reader(conn *websocket.Conn, stdout io.ReadCloser, stdin io.WriteCloser) { conn.SetReadDeadline(time.Now().Add(pongWait)) conn.SetPongHandler(func(string) error { conn.SetReadDeadline(time.Now().Add(pongWait)); return nil }) tickerChan := make(chan bool) - defer func() { tickerChan <- true }() // make sure to close the ticker when we are done. + defer close(tickerChan) // make sure to close the ticker when we are done. go ticker(conn, tickerChan) for { @@ -213,10 +213,7 @@ func reader(conn *websocket.Conn, stdout io.ReadCloser, stdin io.WriteCloser) { // between the server and client to keep it alive with ping messages. func ticker(conn *websocket.Conn, c chan bool) { ticker := time.NewTicker(pingPeriod) - defer func() { - ticker.Stop() - close(c) - }() + defer ticker.Stop() for { // blocking loop with select to wait for stimulation. select {