Add read timeout to socket_listener

This commit is contained in:
soldierkam 2017-03-25 00:12:57 +01:00 committed by Daniel Nelson
parent 1d416a4213
commit f5a8415c78
No known key found for this signature in database
GPG Key ID: CAAD59C9444F6155
3 changed files with 24 additions and 2 deletions

View File

@ -40,6 +40,7 @@
- [#2973](https://github.com/influxdata/telegraf/pull/2973): Change default prometheus_client port. - [#2973](https://github.com/influxdata/telegraf/pull/2973): Change default prometheus_client port.
- [#2661](https://github.com/influxdata/telegraf/pull/2661): Add fluentd input plugin. - [#2661](https://github.com/influxdata/telegraf/pull/2661): Add fluentd input plugin.
- [#2990](https://github.com/influxdata/telegraf/pull/2990): Add result_type field to net_response input plugin. - [#2990](https://github.com/influxdata/telegraf/pull/2990): Add result_type field to net_response input plugin.
- [#2571](https://github.com/influxdata/telegraf/pull/2571): Add read timeout to socket_listener
### Bugfixes ### Bugfixes

View File

@ -30,6 +30,11 @@ This is a sample configuration for the plugin.
## 0 (default) is unlimited. ## 0 (default) is unlimited.
# max_connections = 1024 # max_connections = 1024
## Read timeout.
## Only applies to stream sockets (e.g. TCP).
## 0 (default) is unlimited.
# read_timeout = "30s"
## Maximum socket buffer size in bytes. ## Maximum socket buffer size in bytes.
## For stream sockets, once the buffer fills up, the sender will start backing up. ## For stream sockets, once the buffer fills up, the sender will start backing up.
## For datagram sockets, once the buffer fills up, metrics will start dropping. ## For datagram sockets, once the buffer fills up, metrics will start dropping.

View File

@ -10,6 +10,8 @@ import (
"strings" "strings"
"sync" "sync"
"time"
"github.com/influxdata/telegraf" "github.com/influxdata/telegraf"
"github.com/influxdata/telegraf/internal" "github.com/influxdata/telegraf/internal"
"github.com/influxdata/telegraf/plugins/inputs" "github.com/influxdata/telegraf/plugins/inputs"
@ -91,7 +93,13 @@ func (ssl *streamSocketListener) read(c net.Conn) {
defer c.Close() defer c.Close()
scnr := bufio.NewScanner(c) scnr := bufio.NewScanner(c)
for scnr.Scan() { for {
if ssl.ReadTimeout != nil && ssl.ReadTimeout.Duration > 0 {
c.SetReadDeadline(time.Now().Add(ssl.ReadTimeout.Duration))
}
if !scnr.Scan() {
break
}
metrics, err := ssl.Parse(scnr.Bytes()) metrics, err := ssl.Parse(scnr.Bytes())
if err != nil { if err != nil {
ssl.AddError(fmt.Errorf("unable to parse incoming line: %s", err)) ssl.AddError(fmt.Errorf("unable to parse incoming line: %s", err))
@ -104,7 +112,9 @@ func (ssl *streamSocketListener) read(c net.Conn) {
} }
if err := scnr.Err(); err != nil { if err := scnr.Err(); err != nil {
if !strings.HasSuffix(err.Error(), ": use of closed network connection") { if err, ok := err.(net.Error); ok && err.Timeout() {
log.Printf("D! Timeout in plugin [input.socket_listener]: %s", err)
} else if !strings.HasSuffix(err.Error(), ": use of closed network connection") {
ssl.AddError(err) ssl.AddError(err)
} }
} }
@ -142,6 +152,7 @@ type SocketListener struct {
ServiceAddress string ServiceAddress string
MaxConnections int MaxConnections int
ReadBufferSize int ReadBufferSize int
ReadTimeout *internal.Duration
KeepAlivePeriod *internal.Duration KeepAlivePeriod *internal.Duration
parsers.Parser parsers.Parser
@ -172,6 +183,11 @@ func (sl *SocketListener) SampleConfig() string {
## 0 (default) is unlimited. ## 0 (default) is unlimited.
# max_connections = 1024 # max_connections = 1024
## Read timeout.
## Only applies to stream sockets (e.g. TCP).
## 0 (default) is unlimited.
# read_timeout = "30s"
## Maximum socket buffer size in bytes. ## Maximum socket buffer size in bytes.
## For stream sockets, once the buffer fills up, the sender will start backing up. ## For stream sockets, once the buffer fills up, the sender will start backing up.
## For datagram sockets, once the buffer fills up, metrics will start dropping. ## For datagram sockets, once the buffer fills up, metrics will start dropping.