Trying to be smarter about closing channels: Ask the application not to close the Send channel, but let us do that from Close() instead.
authorChris Jones <chris@cjones.org>
Wed, 16 Oct 2013 20:52:05 -0600
changeset 170 d496de556c9a
parent 169 f59b79d032da
child 171 2f6ae54a2bd1
Trying to be smarter about closing channels: Ask the application not to close the Send channel, but let us do that from Close() instead.
xmpp/xmpp.go
--- a/xmpp/xmpp.go	Wed Oct 16 20:41:14 2013 -0600
+++ b/xmpp/xmpp.go	Wed Oct 16 20:52:05 2013 -0600
@@ -67,7 +67,8 @@
 	// set up the XMPP stream will not appear here.
 	Recv <-chan Stanza
 	// Outgoing XMPP stanzas to the server should be sent to this
-	// channel.
+	// channel. The application should not close this channel;
+	// rather, call Close().
 	Send    chan<- Stanza
 	sendRaw chan<- interface{}
 	statmgr *statmgr
@@ -232,11 +233,7 @@
 	cl.setStatus(StatusShutdown)
 
 	// Shuts down the senders:
-	select {
-	case cl.Send <- &Iq{}:
-		close(cl.Send)
-	default:
-	}
+	cl.shutdownOnce.Do(func() { close(cl.Send) })
 }
 
 // If there's a buffered error in the channel, return it. Otherwise,
@@ -256,11 +253,8 @@
 // there's already an error in the channel, discard the newer one in
 // favor of the older.
 func (cl *Client) setError(err error) {
-	shutdown := func() {
-		cl.setStatus(StatusError)
-		cl.Close()
-	}
-	defer cl.shutdownOnce.Do(shutdown)
+	defer cl.Close()
+	defer cl.setStatus(StatusError)
 
 	if len(cl.error) > 0 {
 		return