From f3e8b9d95fefc81c347ec38d913c9ccd9edb626a Mon Sep 17 00:00:00 2001 From: Matthew Holt Date: Thu, 4 May 2023 16:29:03 -0600 Subject: logging: Soft start for net writer (close #5520) If enabled and there is an error when opening the net writer, ignore the error and report it along with subsequent logs to stderr. --- modules/logging/netwriter.go | 32 ++++++++++++++++++++++++++------ 1 file changed, 26 insertions(+), 6 deletions(-) (limited to 'modules') diff --git a/modules/logging/netwriter.go b/modules/logging/netwriter.go index 5a6cf39..954a09b 100644 --- a/modules/logging/netwriter.go +++ b/modules/logging/netwriter.go @@ -40,6 +40,11 @@ type NetWriter struct { // The timeout to wait while connecting to the socket. DialTimeout caddy.Duration `json:"dial_timeout,omitempty"` + // If enabled, allow connections errors when first opening the + // writer. The error and subsequent log entries will be reported + // to stderr instead until a connection can be re-established. + SoftStart bool `json:"soft_start,omitempty"` + addr caddy.NetworkAddress } @@ -92,7 +97,9 @@ func (nw NetWriter) OpenWriter() (io.WriteCloser, error) { } conn, err := reconn.dial() if err != nil { - return nil, err + // don't block config load if remote is down or some other external problem; + // we can dump logs to stderr for now (see issue #5520) + fmt.Fprintf(os.Stderr, "[ERROR] net log writer failed to connect: %v (will retry connection and print errors here in the meantime)\n", err) } reconn.connMu.Lock() reconn.Conn = conn @@ -104,6 +111,7 @@ func (nw NetWriter) OpenWriter() (io.WriteCloser, error) { // // net
{ // dial_timeout +// soft_start // } func (nw *NetWriter) UnmarshalCaddyfile(d *caddyfile.Dispenser) error { for d.Next() { @@ -128,6 +136,12 @@ func (nw *NetWriter) UnmarshalCaddyfile(d *caddyfile.Dispenser) error { return d.ArgErr() } nw.DialTimeout = caddy.Duration(timeout) + + case "soft_start": + if d.NextArg() { + return d.ArgErr() + } + nw.SoftStart = true } } } @@ -151,8 +165,10 @@ func (reconn *redialerConn) Write(b []byte) (n int, err error) { reconn.connMu.RLock() conn := reconn.Conn reconn.connMu.RUnlock() - if n, err = conn.Write(b); err == nil { - return + if conn != nil { + if n, err = conn.Write(b); err == nil { + return + } } // problem with the connection - lock it and try to fix it @@ -161,8 +177,10 @@ func (reconn *redialerConn) Write(b []byte) (n int, err error) { // if multiple concurrent writes failed on the same broken conn, then // one of them might have already re-dialed by now; try writing again - if n, err = reconn.Conn.Write(b); err == nil { - return + if reconn.Conn != nil { + if n, err = reconn.Conn.Write(b); err == nil { + return + } } // there's still a problem, so try to re-attempt dialing the socket @@ -178,7 +196,9 @@ func (reconn *redialerConn) Write(b []byte) (n int, err error) { return } if n, err = conn2.Write(b); err == nil { - reconn.Conn.Close() + if reconn.Conn != nil { + reconn.Conn.Close() + } reconn.Conn = conn2 } } else { -- cgit v1.2.3