@@ -154,17 +154,10 @@ func (c *Client) Unsubscribe(topic Topic, tickers ...string) error {
154154 return nil
155155}
156156
157- // Output returns the next message in the output queue. If no messages are available, it returns nil.
158- func (c * Client ) Output () any {
159- select {
160- case out , ok := <- c .output :
161- if ok {
162- return out
163- }
164- return nil
165- default :
166- return nil
167- }
157+ // Output returns the output queue. If the channel is closed by the user (not
158+ // recommended), the client connection will close as well.
159+ func (c * Client ) Output () chan any {
160+ return c .output
168161}
169162
170163// Close attempt to gracefully close the connection to the server.
@@ -254,6 +247,17 @@ func (c *Client) reconnect() {
254247 }
255248}
256249
250+ func (c * Client ) closeOutput () {
251+ defer func () {
252+ if r := recover (); r != nil {
253+ c .log .Debugf ("output channel was closed by user" )
254+ } else {
255+ c .log .Debugf ("output channel closed" )
256+ }
257+ }()
258+ close (c .output )
259+ }
260+
257261func (c * Client ) close (reconnect bool ) {
258262 if c .conn == nil {
259263 return
@@ -270,7 +274,7 @@ func (c *Client) close(reconnect bool) {
270274 c .log .Errorf ("process thread closed: %v" , err )
271275 }
272276 c .shouldClose = true
273- close ( c . output )
277+ c . closeOutput ( )
274278 }
275279
276280 if c .conn != nil {
@@ -329,16 +333,15 @@ func (c *Client) write() error {
329333 if err := c .conn .WriteMessage (websocket .TextMessage , msg ); err != nil {
330334 return fmt .Errorf ("failed to send message: %w" , err )
331335 }
332- default :
333- continue
334336 }
335337 }
336338}
337339
338340func (c * Client ) process () (err error ) {
339341 defer func () {
340342 c .log .Debugf ("process thread closed" )
341- if err != nil {
343+ r := recover ()
344+ if r != nil || err != nil {
342345 go c .Close () // this client should close if it hits a fatal error (e.g. auth failed)
343346 }
344347 }()
@@ -356,8 +359,6 @@ func (c *Client) process() (err error) {
356359 if err := c .route (msgs ); err != nil {
357360 return err
358361 }
359- default :
360- continue
361362 }
362363 }
363364}
0 commit comments