Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(common.socket): Make sure the scanner buffer matches the read-buffer size #16111

Merged
merged 1 commit into from
Oct 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions plugins/common/socket/stream.go
Original file line number Diff line number Diff line change
Expand Up @@ -335,6 +335,9 @@ func (l *streamListener) read(conn net.Conn, onData CallbackData) error {
timeout := time.Duration(l.ReadTimeout)

scanner := bufio.NewScanner(decoder)
if l.ReadBufferSize > bufio.MaxScanTokenSize {
scanner.Buffer(make([]byte, l.ReadBufferSize), l.ReadBufferSize)
}
scanner.Split(l.Splitter)
for {
// Set the read deadline, if any, then start reading. The read
Expand Down
67 changes: 67 additions & 0 deletions plugins/inputs/socket_listener/socket_listener_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package socket_listener

import (
"bytes"
"crypto/tls"
"encoding/json"
"errors"
Expand All @@ -25,6 +26,7 @@ import (
"github.com/influxdata/telegraf/plugins/inputs"
_ "github.com/influxdata/telegraf/plugins/parsers/all"
"github.com/influxdata/telegraf/plugins/parsers/influx"
"github.com/influxdata/telegraf/plugins/parsers/value"
"github.com/influxdata/telegraf/testutil"
)

Expand Down Expand Up @@ -195,6 +197,71 @@ func TestSocketListener(t *testing.T) {
}
}

func TestLargeReadBuffer(t *testing.T) {
// Construct a buffer-size setting of 100KiB
var bufsize config.Size
require.NoError(t, bufsize.UnmarshalText([]byte("1000KiB")))

// Setup plugin with a sufficient read buffer
plugin := &SocketListener{
ServiceAddress: "tcp://127.0.0.1:0",
Config: socket.Config{
ReadBufferSize: bufsize,
},
SplitConfig: socket.SplitConfig{
SplittingStrategy: "newline",
},
Log: &testutil.Logger{},
}
parser := &value.Parser{
MetricName: "test",
DataType: "string",
}
require.NoError(t, parser.Init())
plugin.SetParser(parser)

// Create a large message with the readbuffer size
message := bytes.Repeat([]byte{'a'}, int(bufsize)-2)
expected := []telegraf.Metric{
metric.New(
"test",
map[string]string{},
map[string]interface{}{"value": string(message)},
time.Unix(0, 0),
),
}

// Start the plugin
var acc testutil.Accumulator
require.NoError(t, plugin.Init())
require.NoError(t, plugin.Start(&acc))
defer plugin.Stop()

addr := plugin.socket.Address()

// Setup the client for submitting data
client, err := createClient(plugin.ServiceAddress, addr, nil)
require.NoError(t, err)
defer client.Close()

_, err = client.Write(append(message, '\n'))
require.NoError(t, err)
client.Close()

getError := func() error {
acc.Lock()
defer acc.Unlock()
return acc.FirstError()
}

// Test the resulting metrics and compare against expected results
require.Eventuallyf(t, func() bool {
return acc.NMetrics() >= uint64(len(expected))
}, time.Second, 100*time.Millisecond, "did not receive metrics (%d): %v", acc.NMetrics(), getError())
actual := acc.GetTelegrafMetrics()
testutil.RequireMetricsEqual(t, expected, actual, testutil.IgnoreTime())
}

func TestCases(t *testing.T) {
// Get all directories in testdata
folders, err := os.ReadDir("testcases")
Expand Down
Loading