From 847343d801381be358d6129afcfb17d05689eb50 Mon Sep 17 00:00:00 2001
From: Armon Dadgar <armon.dadgar@gmail.com>
Date: Fri, 27 Nov 2015 17:20:57 -0800
Subject: [PATCH] consul: shrink yamux recv buffer on idle streams

---
 consul/pool.go | 6 ++++++
 1 file changed, 6 insertions(+)

diff --git a/consul/pool.go b/consul/pool.go
index 7254ded151..4abecbf4bf 100644
--- a/consul/pool.go
+++ b/consul/pool.go
@@ -103,6 +103,12 @@ func (c *Conn) returnClient(client *StreamClient) {
 	if c.clients.Len() < c.pool.maxStreams && atomic.LoadInt32(&c.shouldClose) == 0 {
 		c.clients.PushFront(client)
 		didSave = true
+
+		// If this is a Yamux stream, shrink the internal buffers so that
+		// we can GC the idle memory
+		if ys, ok := client.stream.(*yamux.Stream); ok {
+			ys.Shrink()
+		}
 	}
 	c.clientLock.Unlock()
 	if !didSave {