mirror of
https://github.com/caddyserver/caddy.git
synced 2024-12-26 21:53:48 +03:00
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:
parent
c8032867b1
commit
f3e8b9d95f
1 changed files with 26 additions and 6 deletions
|
@ -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 {
|
||||
|
|
Loading…
Reference in a new issue