Skip to content

Commit

Permalink
Add read timeout to socket_listener
Browse files Browse the repository at this point in the history
  • Loading branch information
soldierkam authored and danielnelson committed Jul 18, 2017
1 parent 1d416a4 commit f5a8415
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
- [#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.
- [#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

Expand Down
5 changes: 5 additions & 0 deletions plugins/inputs/socket_listener/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@ This is a sample configuration for the plugin.
## 0 (default) is unlimited.
# 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.
## 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.
Expand Down
20 changes: 18 additions & 2 deletions plugins/inputs/socket_listener/socket_listener.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import (
"strings"
"sync"

"time"

"github.com/influxdata/telegraf"
"github.com/influxdata/telegraf/internal"
"github.com/influxdata/telegraf/plugins/inputs"
Expand Down Expand Up @@ -91,7 +93,13 @@ func (ssl *streamSocketListener) read(c net.Conn) {
defer c.Close()

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())
if err != nil {
ssl.AddError(fmt.Errorf("unable to parse incoming line: %s", err))
Expand All @@ -104,7 +112,9 @@ func (ssl *streamSocketListener) read(c net.Conn) {
}

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)
}
}
Expand Down Expand Up @@ -142,6 +152,7 @@ type SocketListener struct {
ServiceAddress string
MaxConnections int
ReadBufferSize int
ReadTimeout *internal.Duration
KeepAlivePeriod *internal.Duration

parsers.Parser
Expand Down Expand Up @@ -172,6 +183,11 @@ func (sl *SocketListener) SampleConfig() string {
## 0 (default) is unlimited.
# 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.
## 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.
Expand Down

0 comments on commit f5a8415

Please sign in to comment.