[lxc-devel] [lxd/master] add a done signal to Monitor API
tych0 on Github
lxc-bot at linuxcontainers.org
Wed Dec 21 22:48:55 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/9615f40d/attachment.bin>
-------------- next part --------------
From b7ec78337b9b6eddc085408ec7bd99ca3c082d3c 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 | 41 +++++++++++++++++++++++++++++------------
lxc/init.go | 2 +-
lxc/monitor.go | 2 +-
lxd/main_shutdown.go | 2 +-
4 files changed, 32 insertions(+), 15 deletions(-)
diff --git a/client.go b/client.go
index f7d1a6f..6326d59 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.")
}
@@ -1495,20 +1495,37 @@ func (c *Client) Monitor(types []string, handler func(interface{})) error {
}
defer conn.Close()
- for {
- message := make(map[string]interface{})
+ readCh := make(chan []byte)
+ errCh := make(chan error)
- _, data, err := conn.ReadMessage()
- if err != nil {
- return err
+ go func() {
+ for {
+ _, data, err := conn.ReadMessage()
+ if err != nil {
+ errCh <- err
+ return
+ }
+
+ readCh <- data
}
+ }()
- err = json.Unmarshal(data, &message)
- if err != nil {
+ for {
+ select {
+ case <-done:
+ return nil
+ case data := <-readCh:
+ message := make(map[string]interface{})
+
+ err = json.Unmarshal(data, &message)
+ if err != nil {
+ return err
+ }
+
+ handler(message)
+ case err := <-errCh:
return err
}
-
- handler(message)
}
}
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