Skip to content

Commit

Permalink
Using 'units' for body and line sizes for http_listener plugin.
Browse files Browse the repository at this point in the history
  • Loading branch information
Samuel Bizien Filippi committed Oct 12, 2018
1 parent 4d38de3 commit aaed954
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 17 deletions.
4 changes: 2 additions & 2 deletions plugins/inputs/http_listener/bufferpool.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@ import (

type pool struct {
buffers chan []byte
size int
size int64

created int64
}

// 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,
Expand Down
22 changes: 11 additions & 11 deletions plugins/inputs/http_listener/http_listener.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand All @@ -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 {
Expand Down Expand Up @@ -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
}
Expand All @@ -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
Expand Down
9 changes: 5 additions & 4 deletions plugins/inputs/http_listener/http_listener_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"testing"
"time"

"github.com/influxdata/telegraf/internal"
"github.com/influxdata/telegraf/testutil"

"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -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,
}

Expand All @@ -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,
}

Expand All @@ -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,
}

Expand All @@ -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,
}

Expand Down

0 comments on commit aaed954

Please sign in to comment.