add keep-alive support to socket_listener & socket_writer (#2697)
closes #2635
This commit is contained in:
committed by
Daniel Nelson
parent
ddc2f64593
commit
b1a2f896a2
@@ -19,6 +19,12 @@ It can output data in any of the [supported output formats](https://github.com/i
|
||||
# address = "unix:///tmp/telegraf.sock"
|
||||
# address = "unixgram:///tmp/telegraf.sock"
|
||||
|
||||
## Period between keep alive probes.
|
||||
## Only applies to TCP sockets.
|
||||
## 0 disables keep alive probes.
|
||||
## Defaults to the OS configuration.
|
||||
# keep_alive_period = "5m"
|
||||
|
||||
## Data format to generate.
|
||||
## Each data format has it's own unique set of configuration options, read
|
||||
## more about them here:
|
||||
|
||||
@@ -2,16 +2,19 @@ package socket_writer
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"net"
|
||||
"strings"
|
||||
|
||||
"github.com/influxdata/telegraf"
|
||||
"github.com/influxdata/telegraf/internal"
|
||||
"github.com/influxdata/telegraf/plugins/outputs"
|
||||
"github.com/influxdata/telegraf/plugins/serializers"
|
||||
)
|
||||
|
||||
type SocketWriter struct {
|
||||
Address string
|
||||
Address string
|
||||
KeepAlivePeriod *internal.Duration
|
||||
|
||||
serializers.Serializer
|
||||
|
||||
@@ -36,6 +39,12 @@ func (sw *SocketWriter) SampleConfig() string {
|
||||
# address = "unix:///tmp/telegraf.sock"
|
||||
# address = "unixgram:///tmp/telegraf.sock"
|
||||
|
||||
## Period between keep alive probes.
|
||||
## Only applies to TCP sockets.
|
||||
## 0 disables keep alive probes.
|
||||
## Defaults to the OS configuration.
|
||||
# keep_alive_period = "5m"
|
||||
|
||||
## Data format to generate.
|
||||
## Each data format has it's own unique set of configuration options, read
|
||||
## more about them here:
|
||||
@@ -59,10 +68,31 @@ func (sw *SocketWriter) Connect() error {
|
||||
return err
|
||||
}
|
||||
|
||||
if err := sw.setKeepAlive(c); err != nil {
|
||||
log.Printf("unable to configure keep alive (%s): %s", sw.Address, err)
|
||||
}
|
||||
|
||||
sw.Conn = c
|
||||
return nil
|
||||
}
|
||||
|
||||
func (sw *SocketWriter) setKeepAlive(c net.Conn) error {
|
||||
if sw.KeepAlivePeriod == nil {
|
||||
return nil
|
||||
}
|
||||
tcpc, ok := c.(*net.TCPConn)
|
||||
if !ok {
|
||||
return fmt.Errorf("cannot set keep alive on a %s socket", strings.SplitN(sw.Address, "://", 2)[0])
|
||||
}
|
||||
if sw.KeepAlivePeriod.Duration == 0 {
|
||||
return tcpc.SetKeepAlive(false)
|
||||
}
|
||||
if err := tcpc.SetKeepAlive(true); err != nil {
|
||||
return err
|
||||
}
|
||||
return tcpc.SetKeepAlivePeriod(sw.KeepAlivePeriod.Duration)
|
||||
}
|
||||
|
||||
// Write writes the given metrics to the destination.
|
||||
// If an error is encountered, it is up to the caller to retry the same write again later.
|
||||
// Not parallel safe.
|
||||
|
||||
Reference in New Issue
Block a user