diff options
author | Matthew Holt <[email protected]> | 2023-05-04 16:29:03 -0600 |
---|---|---|
committer | Matthew Holt <[email protected]> | 2023-05-04 16:29:03 -0600 |
commit | f3e8b9d95fefc81c347ec38d913c9ccd9edb626a (patch) | |
tree | 63068ee0114c302946813cd38469fac76f0ef38c /modules/logging | |
parent | c8032867b14e52d18c239ed97140be3ae05d9f2f (diff) | |
download | caddy-f3e8b9d95fefc81c347ec38d913c9ccd9edb626a.tar.gz caddy-f3e8b9d95fefc81c347ec38d913c9ccd9edb626a.zip |
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.
Diffstat (limited to 'modules/logging')
-rw-r--r-- | modules/logging/netwriter.go | 32 |
1 files changed, 26 insertions, 6 deletions
diff --git a/modules/logging/netwriter.go b/modules/logging/netwriter.go index 5a6cf3947..954a09b2c 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 <address> { // dial_timeout <duration> +// 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 { |