tcp outlet: fix error handling on write failure

Also: clarify semantics of RetryInterval
This commit is contained in:
Christian Schwarz 2017-09-30 16:38:48 +02:00
parent 0cbee78b40
commit aab43af27c

View File

@ -27,13 +27,17 @@ func (h WriterOutlet) WriteEntry(ctx context.Context, entry logger.Entry) error
} }
type TCPOutlet struct { type TCPOutlet struct {
Formatter EntryFormatter Formatter EntryFormatter
Net, Address string Net, Address string
Dialer net.Dialer Dialer net.Dialer
TLS *tls.Config TLS *tls.Config
// Specifies how much time must pass between a connection error and a reconnection attempt
// Log entries written to the outlet during this time interval are silently dropped.
RetryInterval time.Duration RetryInterval time.Duration
conn net.Conn // nil if there was an error sending / connecting to remote server
retry time.Time conn net.Conn
// Last time an error occurred when sending / connecting to remote server
retry time.Time
} }
func (h *TCPOutlet) WriteEntry(ctx context.Context, e logger.Entry) error { func (h *TCPOutlet) WriteEntry(ctx context.Context, e logger.Entry) error {
@ -45,8 +49,8 @@ func (h *TCPOutlet) WriteEntry(ctx context.Context, e logger.Entry) error {
if h.conn == nil { if h.conn == nil {
if time.Now().Sub(h.retry) < h.RetryInterval { if time.Now().Sub(h.retry) < h.RetryInterval {
return nil // this is not an error toward the logger // cool-down phase, drop the log entry
//return errors.New("TCP hook reconnect prohibited by retry interval") return nil
} }
if h.TLS != nil { if h.TLS != nil {
@ -66,10 +70,10 @@ func (h *TCPOutlet) WriteEntry(ctx context.Context, e logger.Entry) error {
_, err = h.conn.Write([]byte("\n")) _, err = h.conn.Write([]byte("\n"))
} }
if err != nil { if err != nil {
return errors.Wrap(err, "cannot write")
h.conn.Close() h.conn.Close()
h.conn = nil h.conn = nil
return err h.retry = time.Now()
return errors.Wrap(err, "cannot write")
} }
return nil return nil