From aaed954d25b6cffff62f0e5a9931a5ba84289797 Mon Sep 17 00:00:00 2001 From: Samuel Bizien Filippi Date: Fri, 12 Oct 2018 12:04:48 +0200 Subject: [PATCH] Using 'units' for body and line sizes for http_listener plugin. --- plugins/inputs/http_listener/bufferpool.go | 4 ++-- plugins/inputs/http_listener/http_listener.go | 22 +++++++++---------- .../http_listener/http_listener_test.go | 9 ++++---- 3 files changed, 18 insertions(+), 17 deletions(-) diff --git a/plugins/inputs/http_listener/bufferpool.go b/plugins/inputs/http_listener/bufferpool.go index 00a93652db2fb..e4cec5bd1f433 100644 --- a/plugins/inputs/http_listener/bufferpool.go +++ b/plugins/inputs/http_listener/bufferpool.go @@ -6,7 +6,7 @@ import ( type pool struct { buffers chan []byte - size int + size int64 created int64 } @@ -14,7 +14,7 @@ type pool struct { // NewPool returns a new pool object. // n is the number of buffers // bufSize is the size (in bytes) of each buffer -func NewPool(n, bufSize int) *pool { +func NewPool(n, bufSize int64) *pool { return &pool{ buffers: make(chan []byte, n), size: bufSize, diff --git a/plugins/inputs/http_listener/http_listener.go b/plugins/inputs/http_listener/http_listener.go index cd82e40c0c963..6b36f80aed043 100644 --- a/plugins/inputs/http_listener/http_listener.go +++ b/plugins/inputs/http_listener/http_listener.go @@ -39,8 +39,8 @@ type HTTPListener struct { ServiceAddress string ReadTimeout internal.Duration WriteTimeout internal.Duration - MaxBodySize int64 - MaxLineSize int + MaxBodySize internal.Size + MaxLineSize internal.Size Port int tlsint.ServerConfig @@ -85,11 +85,11 @@ const sampleConfig = ` ## Maximum allowed http request body size in bytes. ## 0 means to use the default of 536,870,912 bytes (500 mebibytes) - max_body_size = 0 + max_body_size = "500MiB" ## Maximum line size allowed to be sent in bytes. ## 0 means to use the default of 65536 bytes (64 kibibytes) - max_line_size = 0 + max_line_size = "64KiB" ## Set one or more allowed client CA certificate file names to ## enable mutually authenticated TLS connections @@ -139,11 +139,11 @@ func (h *HTTPListener) Start(acc telegraf.Accumulator) error { h.BuffersCreated = selfstat.Register("http_listener", "buffers_created", tags) h.AuthFailures = selfstat.Register("http_listener", "auth_failures", tags) - if h.MaxBodySize == 0 { - h.MaxBodySize = DEFAULT_MAX_BODY_SIZE + if h.MaxBodySize.Size == 0 { + h.MaxBodySize.Size = DEFAULT_MAX_BODY_SIZE } - if h.MaxLineSize == 0 { - h.MaxLineSize = DEFAULT_MAX_LINE_SIZE + if h.MaxLineSize.Size == 0 { + h.MaxLineSize.Size = DEFAULT_MAX_LINE_SIZE } if h.ReadTimeout.Duration < time.Second { @@ -154,7 +154,7 @@ func (h *HTTPListener) Start(acc telegraf.Accumulator) error { } h.acc = acc - h.pool = NewPool(200, h.MaxLineSize) + h.pool = NewPool(200, h.MaxLineSize.Size) tlsConf, err := h.ServerConfig.TLSConfig() if err != nil { @@ -241,7 +241,7 @@ func (h *HTTPListener) ServeHTTP(res http.ResponseWriter, req *http.Request) { func (h *HTTPListener) serveWrite(res http.ResponseWriter, req *http.Request) { // Check that the content length is not too large for us to handle. - if req.ContentLength > h.MaxBodySize { + if req.ContentLength > h.MaxBodySize.Size { tooLarge(res) return } @@ -261,7 +261,7 @@ func (h *HTTPListener) serveWrite(res http.ResponseWriter, req *http.Request) { return } } - body = http.MaxBytesReader(res, body, h.MaxBodySize) + body = http.MaxBytesReader(res, body, h.MaxBodySize.Size) var return400 bool var hangingBytes bool diff --git a/plugins/inputs/http_listener/http_listener_test.go b/plugins/inputs/http_listener/http_listener_test.go index 3277e5344ecbd..9642950613840 100644 --- a/plugins/inputs/http_listener/http_listener_test.go +++ b/plugins/inputs/http_listener/http_listener_test.go @@ -13,6 +13,7 @@ import ( "testing" "time" + "github.com/influxdata/telegraf/internal" "github.com/influxdata/telegraf/testutil" "github.com/stretchr/testify/require" @@ -217,7 +218,7 @@ func TestWriteHTTPNoNewline(t *testing.T) { func TestWriteHTTPMaxLineSizeIncrease(t *testing.T) { listener := &HTTPListener{ ServiceAddress: "localhost:0", - MaxLineSize: 128 * 1000, + MaxLineSize: internal.Size{Size: 128 * 1000}, TimeFunc: time.Now, } @@ -235,7 +236,7 @@ func TestWriteHTTPMaxLineSizeIncrease(t *testing.T) { func TestWriteHTTPVerySmallMaxBody(t *testing.T) { listener := &HTTPListener{ ServiceAddress: "localhost:0", - MaxBodySize: 4096, + MaxBodySize: internal.Size{Size: 4096}, TimeFunc: time.Now, } @@ -252,7 +253,7 @@ func TestWriteHTTPVerySmallMaxBody(t *testing.T) { func TestWriteHTTPVerySmallMaxLineSize(t *testing.T) { listener := &HTTPListener{ ServiceAddress: "localhost:0", - MaxLineSize: 70, + MaxLineSize: internal.Size{Size: 70}, TimeFunc: time.Now, } @@ -279,7 +280,7 @@ func TestWriteHTTPVerySmallMaxLineSize(t *testing.T) { func TestWriteHTTPLargeLinesSkipped(t *testing.T) { listener := &HTTPListener{ ServiceAddress: "localhost:0", - MaxLineSize: 100, + MaxLineSize: internal.Size{Size: 100}, TimeFunc: time.Now, }