From 0f16c0f4cf58b8a7ebb096e26d541c5d896269e8 Mon Sep 17 00:00:00 2001
From: Cameron Sparr <cameronsparr@gmail.com>
Date: Tue, 5 Apr 2016 14:35:14 -0600
Subject: [PATCH] Reduce TCP listener allocations

---
 CHANGELOG.md                                |  1 +
 plugins/inputs/tcp_listener/tcp_listener.go | 10 +++++-----
 2 files changed, 6 insertions(+), 5 deletions(-)

diff --git a/CHANGELOG.md b/CHANGELOG.md
index 9c7d0f50..5a455269 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -2,6 +2,7 @@
 
 ### Features
 - [#976](https://github.com/influxdata/telegraf/pull/976): Reduce allocations in the UDP and statsd inputs.
+- [#979](https://github.com/influxdata/telegraf/pull/979): Reduce allocations in the TCP listener.
 
 ### Bugfixes
 - [#968](https://github.com/influxdata/telegraf/issues/968): Processes plugin gets unknown state when spaces are in (command name)
diff --git a/plugins/inputs/tcp_listener/tcp_listener.go b/plugins/inputs/tcp_listener/tcp_listener.go
index a1b99105..4559a3bf 100644
--- a/plugins/inputs/tcp_listener/tcp_listener.go
+++ b/plugins/inputs/tcp_listener/tcp_listener.go
@@ -39,7 +39,7 @@ type TcpListener struct {
 	acc    telegraf.Accumulator
 }
 
-var dropwarn = "ERROR: Message queue full. Discarding line [%s] " +
+var dropwarn = "ERROR: Message queue full. Discarding metric. " +
 	"You may want to increase allowed_pending_messages in the config\n"
 
 const sampleConfig = `
@@ -202,11 +202,10 @@ func (t *TcpListener) handler(conn *net.TCPConn, id string) {
 			if !scanner.Scan() {
 				return
 			}
-			buf := scanner.Bytes()
 			select {
-			case t.in <- buf:
+			case t.in <- scanner.Bytes():
 			default:
-				log.Printf(dropwarn, string(buf))
+				log.Printf(dropwarn)
 			}
 		}
 	}
@@ -215,11 +214,12 @@ func (t *TcpListener) handler(conn *net.TCPConn, id string) {
 // tcpParser parses the incoming tcp byte packets
 func (t *TcpListener) tcpParser() error {
 	defer t.wg.Done()
+	var packet []byte
 	for {
 		select {
 		case <-t.done:
 			return nil
-		case packet := <-t.in:
+		case packet = <-t.in:
 			if len(packet) == 0 {
 				continue
 			}
-- 
GitLab