[lxc-devel] [lxd/master] Move imagesDownloading out of the daemon struct

stgraber on Github lxc-bot at linuxcontainers.org
Mon Feb 13 23:14:19 UTC 2017


A non-text attachment was scrubbed...
Name: not available
Type: text/x-mailbox
Size: 354 bytes
Desc: not available
URL: <http://lists.linuxcontainers.org/pipermail/lxc-devel/attachments/20170213/8d220023/attachment.bin>
-------------- next part --------------
From f5d3f2fd128842d99dbf1387842a2c1afa6b5925 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?St=C3=A9phane=20Graber?= <stgraber at ubuntu.com>
Date: Mon, 13 Feb 2017 18:14:01 -0500
Subject: [PATCH] Move imagesDownloading out of the daemon struct
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

Signed-off-by: Stéphane Graber <stgraber at ubuntu.com>
---
 lxd/daemon.go                |  5 -----
 lxd/daemon_images.go         | 21 ++++++++++++---------
 lxd/main_activateifneeded.go |  6 +-----
 lxd/main_test.go             |  5 +----
 4 files changed, 14 insertions(+), 23 deletions(-)

diff --git a/lxd/daemon.go b/lxd/daemon.go
index e0fc291..9a9bba8 100644
--- a/lxd/daemon.go
+++ b/lxd/daemon.go
@@ -90,9 +90,6 @@ type Daemon struct {
 	MockMode  bool
 	SetupMode bool
 
-	imagesDownloading     map[string]chan bool
-	imagesDownloadingLock sync.Mutex
-
 	tlsConfig *tls.Config
 
 	proxy func(req *http.Request) (*url.URL, error)
@@ -545,8 +542,6 @@ func haveMacAdmin() bool {
 
 func (d *Daemon) Init() error {
 	/* Initialize some variables */
-	d.imagesDownloading = map[string]chan bool{}
-
 	d.readyChan = make(chan bool)
 	d.shutdownChan = make(chan bool)
 
diff --git a/lxd/daemon_images.go b/lxd/daemon_images.go
index 97739ab..15cc0e1 100644
--- a/lxd/daemon_images.go
+++ b/lxd/daemon_images.go
@@ -34,6 +34,9 @@ type imageStreamCacheEntry struct {
 var imageStreamCache = map[string]*imageStreamCacheEntry{}
 var imageStreamCacheLock sync.Mutex
 
+var imagesDownloading = map[string]chan bool{}
+var imagesDownloadingLock sync.Mutex
+
 func imageSaveStreamCache() error {
 	data, err := yaml.Marshal(&imageStreamCache)
 	if err != nil {
@@ -188,10 +191,10 @@ func (d *Daemon) ImageDownload(op *operation, server string, protocol string, ce
 	}
 
 	// Now check if we already downloading the image
-	d.imagesDownloadingLock.Lock()
-	if waitChannel, ok := d.imagesDownloading[fp]; ok {
+	imagesDownloadingLock.Lock()
+	if waitChannel, ok := imagesDownloading[fp]; ok {
 		// We already download the image
-		d.imagesDownloadingLock.Unlock()
+		imagesDownloadingLock.Unlock()
 
 		shared.LogDebug(
 			"Already downloading the image, waiting for it to succeed",
@@ -218,17 +221,17 @@ func (d *Daemon) ImageDownload(op *operation, server string, protocol string, ce
 	}
 
 	// Add the download to the queue
-	d.imagesDownloading[fp] = make(chan bool)
-	d.imagesDownloadingLock.Unlock()
+	imagesDownloading[fp] = make(chan bool)
+	imagesDownloadingLock.Unlock()
 
 	// Unlock once this func ends.
 	defer func() {
-		d.imagesDownloadingLock.Lock()
-		if waitChannel, ok := d.imagesDownloading[fp]; ok {
+		imagesDownloadingLock.Lock()
+		if waitChannel, ok := imagesDownloading[fp]; ok {
 			close(waitChannel)
-			delete(d.imagesDownloading, fp)
+			delete(imagesDownloading, fp)
 		}
-		d.imagesDownloadingLock.Unlock()
+		imagesDownloadingLock.Unlock()
 	}()
 
 	// Begin downloading
diff --git a/lxd/main_activateifneeded.go b/lxd/main_activateifneeded.go
index b0d66d2..f91ff6b 100644
--- a/lxd/main_activateifneeded.go
+++ b/lxd/main_activateifneeded.go
@@ -1,8 +1,6 @@
 package main
 
 import (
-	"sync"
-
 	"github.com/lxc/lxd"
 	"github.com/lxc/lxd/shared"
 )
@@ -10,9 +8,7 @@ import (
 func cmdActivateIfNeeded() error {
 	// Don't start a full daemon, we just need DB access
 	d := &Daemon{
-		imagesDownloading:     map[string]chan bool{},
-		imagesDownloadingLock: sync.Mutex{},
-		lxcpath:               shared.VarPath("containers"),
+		lxcpath: shared.VarPath("containers"),
 	}
 
 	if !shared.PathExists(shared.VarPath("lxd.db")) {
diff --git a/lxd/main_test.go b/lxd/main_test.go
index e3af4e1..40a639e 100644
--- a/lxd/main_test.go
+++ b/lxd/main_test.go
@@ -3,7 +3,6 @@ package main
 import (
 	"io/ioutil"
 	"os"
-	"sync"
 	"testing"
 
 	"github.com/stretchr/testify/require"
@@ -14,9 +13,7 @@ import (
 
 func mockStartDaemon() (*Daemon, error) {
 	d := &Daemon{
-		MockMode:              true,
-		imagesDownloading:     map[string]chan bool{},
-		imagesDownloadingLock: sync.Mutex{},
+		MockMode: true,
 	}
 
 	if err := d.Init(); err != nil {


More information about the lxc-devel mailing list