[prev in list] [next in list] [prev in thread] [next in thread] 

List:       lxc-devel
Subject:    [lxc-devel] [lxd/master] add a done signal to Monitor API
From:       tych0 on Github <lxc-bot () linuxcontainers ! org>
Date:       2016-12-21 22:48:56
Message-ID: 20161221224856.E30F639AAD () mailman01 ! srv ! dcmtl ! stgraber ! net
[Download RAW message or body]

[Attachment #2 (text/x-mailbox)]

The following pull request was submitted through Github.
It can be accessed and reviewed at: https://github.com/lxc/lxd/pull/2754

This e-mail was sent by the LXC bot, direct replies will not reach the author
unless they happen to be subscribed to this list.

=== Description (from pull-request) ===
Otherwise there is no way to close these connections.

Signed-off-by: Tycho Andersen <tycho.andersen@canonical.com>

[Attachment #3 (text/plain)]

From b7ec78337b9b6eddc085408ec7bd99ca3c082d3c Mon Sep 17 00:00:00 2001
From: Tycho Andersen <tycho.andersen@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@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 {


[Attachment #4 (text/plain)]

_______________________________________________
lxc-devel mailing list
lxc-devel@lists.linuxcontainers.org
http://lists.linuxcontainers.org/listinfo/lxc-devel


[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic