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.
This commit is contained in:
Matthew Holt 2023-05-04 16:29:03 -06:00
parent c8032867b1
commit f3e8b9d95f
No known key found for this signature in database
GPG key ID: 2A349DD577D586A5

View file

@ -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 {