Added a Close() function and fixed a couple of shutdown bugs.
authorChris Jones <chris@cjones.org>
Mon, 30 Sep 2013 18:59:37 -0600
changeset 162 7b5586a5e109
parent 161 6570a6a9683b
child 163 3f891f7fe817
Added a Close() function and fixed a couple of shutdown bugs.
TODO.txt
example/interact.go
xmpp/layer3.go
xmpp/roster.go
xmpp/xmpp.go
--- a/TODO.txt	Sun Sep 29 21:56:19 2013 -0600
+++ b/TODO.txt	Mon Sep 30 18:59:37 2013 -0600
@@ -5,9 +5,6 @@
 
 Eliminate as many uses of Generic as possible.
 
-Think about how to gracefully shutdown. Probably have a Close()
-function.
-
 Get rid of logging. We're providing status updates. Include an error()
 receiver function that lets internals report their errors for return
 from NewClient().
--- a/example/interact.go	Sun Sep 29 21:56:19 2013 -0600
+++ b/example/interact.go	Mon Sep 30 18:59:37 2013 -0600
@@ -52,7 +52,7 @@
 	if err != nil {
 		log.Fatalf("NewClient(%v): %v", jid, err)
 	}
-	defer close(c.Send)
+	defer c.Close()
 
 	go func(ch <-chan xmpp.Stanza) {
 		for obj := range ch {
--- a/xmpp/layer3.go	Sun Sep 29 21:56:19 2013 -0600
+++ b/xmpp/layer3.go	Mon Sep 30 18:59:37 2013 -0600
@@ -26,7 +26,10 @@
 	var input <-chan Stanza
 	for {
 		select {
-		case stat := <-status:
+		case stat, ok := <-status:
+			if !ok {
+				return
+			}
 			switch stat {
 			default:
 				input = nil
--- a/xmpp/roster.go	Sun Sep 29 21:56:19 2013 -0600
+++ b/xmpp/roster.go	Mon Sep 30 18:59:37 2013 -0600
@@ -79,6 +79,7 @@
 	go r.rosterMgr(rosterUpdate)
 	recv := func(in <-chan Stanza, out chan<- Stanza) {
 		defer close(out)
+		defer close(rosterUpdate)
 		for stan := range in {
 			rosterUpdate <- stan
 			out <- stan
--- a/xmpp/xmpp.go	Sun Sep 29 21:56:19 2013 -0600
+++ b/xmpp/xmpp.go	Mon Sep 30 18:59:37 2013 -0600
@@ -288,3 +288,10 @@
 		Debug.Log(buf)
 	}
 }
+
+func (cl *Client) Close() {
+	// Shuts down the receivers:
+	cl.setStatus(StatusShutdown)
+	// Shuts down the senders:
+	close(cl.Send)
+}