[lxc-devel] [lxd/master] add a done signal to Monitor API

tych0 on Github lxc-bot at linuxcontainers.org
Wed Dec 21 22:07:07 UTC 2016


A non-text attachment was scrubbed...
Name: not available
Type: text/x-mailbox
Size: 418 bytes
Desc: not available
URL: <http://lists.linuxcontainers.org/pipermail/lxc-devel/attachments/20161221/e334beca/attachment.bin>
-------------- next part --------------
From a74b479f445143e4aaa6fae2356d1179b30d6dca Mon Sep 17 00:00:00 2001
From: Tycho Andersen <tycho.andersen at canonical.com>
Date: Wed, 21 Dec 2016 14:58:59 -0700
Subject: [PATCH] add a done signal to Monitor API

Otherwise there is no way to close these connections.

Signed-off-by: Tycho Andersen <tycho.andersen at canonical.com>
---
 client.go            | 15 ++++++++++++---
 lxc/init.go          |  2 +-
 lxc/monitor.go       |  2 +-
 lxd/main_shutdown.go |  2 +-
 4 files changed, 15 insertions(+), 6 deletions(-)

diff --git a/client.go b/client.go
index f7d1a6f..9e6bc03 100644
--- a/client.go
+++ b/client.go
@@ -705,7 +705,7 @@ func (c *Client) CopyImage(image string, dest *Client, copy_aliases bool, aliase
 	}
 
 	if progressHandler != nil {
-		go dest.Monitor([]string{"operation"}, handler)
+		go dest.Monitor([]string{"operation"}, handler, nil)
 	}
 
 	fingerprint := info.Fingerprint
@@ -937,7 +937,7 @@ func (c *Client) PostImageURL(imageFile string, properties []string, public bool
 	}
 
 	if progressHandler != nil {
-		go c.Monitor([]string{"operation"}, handler)
+		go c.Monitor([]string{"operation"}, handler, nil)
 	}
 
 	resp, err := c.post("images", body, Async)
@@ -1479,7 +1479,7 @@ func (c *Client) LocalCopy(source string, name string, config map[string]string,
 	return c.post("containers", body, Async)
 }
 
-func (c *Client) Monitor(types []string, handler func(interface{})) error {
+func (c *Client) Monitor(types []string, handler func(interface{}), done chan bool) error {
 	if c.Remote.Public {
 		return fmt.Errorf("This function isn't supported by public remotes.")
 	}
@@ -1496,6 +1496,15 @@ func (c *Client) Monitor(types []string, handler func(interface{})) error {
 	defer conn.Close()
 
 	for {
+		if done != nil {
+			select {
+			case <-done:
+				return nil
+			default:
+				break
+			}
+		}
+
 		message := make(map[string]interface{})
 
 		_, data, err := conn.ReadMessage()
diff --git a/lxc/init.go b/lxc/init.go
index fec1ef0..95f0507 100644
--- a/lxc/init.go
+++ b/lxc/init.go
@@ -270,7 +270,7 @@ func (c *initCmd) initProgressTracker(d *lxd.Client, progress *ProgressRenderer,
 			progress.Update(opMd["download_progress"].(string))
 		}
 	}
-	go d.Monitor([]string{"operation"}, handler)
+	go d.Monitor([]string{"operation"}, handler, nil)
 }
 
 func (c *initCmd) guessImage(config *lxd.Config, d *lxd.Client, remote string, iremote string, image string) (string, string) {
diff --git a/lxc/monitor.go b/lxc/monitor.go
index 877a1f9..74e3306 100644
--- a/lxc/monitor.go
+++ b/lxc/monitor.go
@@ -84,5 +84,5 @@ func (c *monitorCmd) run(config *lxd.Config, args []string) error {
 		fmt.Printf("%s\n\n", render)
 	}
 
-	return d.Monitor(c.typeArgs, handler)
+	return d.Monitor(c.typeArgs, handler, nil)
 }
diff --git a/lxd/main_shutdown.go b/lxd/main_shutdown.go
index 9110ed5..74c380f 100644
--- a/lxd/main_shutdown.go
+++ b/lxd/main_shutdown.go
@@ -34,7 +34,7 @@ func cmdShutdown() error {
 
 	monitor := make(chan error, 1)
 	go func() {
-		monitor <- c.Monitor(nil, func(m interface{}) {})
+		monitor <- c.Monitor(nil, func(m interface{}) {}, nil)
 	}()
 
 	select {


More information about the lxc-devel mailing list