[lxc-devel] [lxd/master] disable keepalives in http.Transports

tych0 on Github lxc-bot at linuxcontainers.org
Tue Oct 11 15:03:25 UTC 2016


A non-text attachment was scrubbed...
Name: not available
Type: text/x-mailbox
Size: 1116 bytes
Desc: not available
URL: <http://lists.linuxcontainers.org/pipermail/lxc-devel/attachments/20161011/c4e17429/attachment.bin>
-------------- next part --------------
From 87c0b9df6c52259586b392c41cdeeffbcbabd286 Mon Sep 17 00:00:00 2001
From: Tycho Andersen <tycho.andersen at canonical.com>
Date: Tue, 11 Oct 2016 09:00:19 -0600
Subject: [PATCH] disable keepalives in http.Transports

http/transport.go says:

// By default, Transport caches connections for future re-use.
// This may leave many open connections when accessing many hosts.
// This behavior can be managed using Transport's CloseIdleConnections
// method
// and the MaxIdleConnsPerHost and DisableKeepAlives fields.

In particular,

babbageclunk | Yeah - we end up leaking 10 goroutines everytime we destroy a model.

juju uses the client from a long running daemon, which creates a new
http.Transport for each call to the API, which creates a new
http.Transport, which leaks these goroutines.

Perhaps we can fix this more better with the client rewrite, but for now
let's just not do the keepalive, since that's how we're using
http.Transport anyway.

Signed-off-by: Tycho Andersen <tycho.andersen at canonical.com>
---
 client.go     | 12 ++++++++----
 lxd/daemon.go | 14 ++++++++------
 2 files changed, 16 insertions(+), 10 deletions(-)

diff --git a/client.go b/client.go
index 2b08baa..4309cb6 100644
--- a/client.go
+++ b/client.go
@@ -272,7 +272,10 @@ func connectViaUnix(c *Client, remote *RemoteConfig) error {
 		}
 		return net.DialUnix("unix", nil, raddr)
 	}
-	c.Http.Transport = &http.Transport{Dial: uDial}
+	c.Http.Transport = &http.Transport{
+		Dial:              uDial,
+		DisableKeepAlives: true,
+	}
 	c.websocketDialer.NetDial = uDial
 	c.Remote = remote
 
@@ -291,9 +294,10 @@ func connectViaHttp(c *Client, remote *RemoteConfig, clientCert, clientKey, clie
 	}
 
 	tr := &http.Transport{
-		TLSClientConfig: tlsconfig,
-		Dial:            shared.RFC3493Dialer,
-		Proxy:           shared.ProxyFromEnvironment,
+		TLSClientConfig:   tlsconfig,
+		Dial:              shared.RFC3493Dialer,
+		Proxy:             shared.ProxyFromEnvironment,
+		DisableKeepAlives: true,
 	}
 
 	c.websocketDialer.NetDial = shared.RFC3493Dialer
diff --git a/lxd/daemon.go b/lxd/daemon.go
index c60ad6c..949d7dc 100644
--- a/lxd/daemon.go
+++ b/lxd/daemon.go
@@ -129,9 +129,10 @@ func (d *Daemon) httpGetSync(url string, certificate string) (*lxd.Response, err
 	}
 
 	tr := &http.Transport{
-		TLSClientConfig: tlsConfig,
-		Dial:            shared.RFC3493Dialer,
-		Proxy:           d.proxy,
+		TLSClientConfig:   tlsConfig,
+		Dial:              shared.RFC3493Dialer,
+		Proxy:             d.proxy,
+		DisableKeepAlives: true,
 	}
 
 	myhttp := http.Client{
@@ -184,9 +185,10 @@ func (d *Daemon) httpGetFile(url string, certificate string) (*http.Response, er
 	}
 
 	tr := &http.Transport{
-		TLSClientConfig: tlsConfig,
-		Dial:            shared.RFC3493Dialer,
-		Proxy:           d.proxy,
+		TLSClientConfig:   tlsConfig,
+		Dial:              shared.RFC3493Dialer,
+		Proxy:             d.proxy,
+		DisableKeepAlives: true,
 	}
 	myhttp := http.Client{
 		Transport: tr,


More information about the lxc-devel mailing list