Added LogAllErrors config parameter to Server, which allows logging the most frequent errors such as 'connection reset by peer', 'broken pipe' and 'i/o timeout'. By default such errors are suppressed

This commit is contained in:
Aliaksandr Valialkin
2016-02-19 12:01:27 +02:00
parent 14b2ff3d2b
commit ed7ca4c631
2 changed files with 16 additions and 4 deletions
+11
View File
@@ -201,6 +201,16 @@ type Server struct {
// Server accepts all the requests by default.
GetOnly bool
// Logs all errors, including the most frequent
// 'connection reset by peer', 'broken pipe' and 'connection timeout'
// errors. Such errors are common in production serving real-world
// clients.
//
// By default the most frequent errors such as
// 'connection reset by peer', 'broken pipe' and 'connection timeout'
// are suppressed in order to limit output log traffic.
LogAllErrors bool
// Logger, which is used by RequestCtx.Logger().
//
// By default standard logger from log package is used.
@@ -1062,6 +1072,7 @@ func (s *Server) Serve(ln net.Listener) error {
wp := &workerPool{
WorkerFunc: s.serveConn,
MaxWorkersCount: maxWorkersCount,
LogAllErrors: s.LogAllErrors,
Logger: s.logger(),
}
wp.Start()
+5 -4
View File
@@ -19,10 +19,10 @@ type workerPool struct {
// It must leave c unclosed.
WorkerFunc func(c net.Conn) error
// Maximum number of workers to create.
MaxWorkersCount int
// Logger used by workerPool.
LogAllErrors bool
Logger Logger
lock sync.Mutex
@@ -194,8 +194,9 @@ func (wp *workerPool) workerFunc(ch *workerChan) {
}
if err = wp.WorkerFunc(c); err != nil && err != errHijacked {
errStr := err.Error()
if !strings.Contains(errStr, "broken pipe") && !strings.Contains(errStr, "reset by peer") &&
!strings.Contains(errStr, "i/o timeout") {
if wp.LogAllErrors || !(strings.Contains(errStr, "broken pipe") ||
strings.Contains(errStr, "reset by peer") ||
strings.Contains(errStr, "i/o timeout")) {
wp.Logger.Printf("error when serving connection %q<->%q: %s", c.LocalAddr(), c.RemoteAddr(), err)
}
}