From ed7ca4c631ff1c80a2c9bc34150ba97227ed13b2 Mon Sep 17 00:00:00 2001 From: Aliaksandr Valialkin Date: Fri, 19 Feb 2016 12:01:27 +0200 Subject: [PATCH] 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 --- server.go | 11 +++++++++++ workerpool.go | 9 +++++---- 2 files changed, 16 insertions(+), 4 deletions(-) diff --git a/server.go b/server.go index 316a708..cbc1da2 100644 --- a/server.go +++ b/server.go @@ -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() diff --git a/workerpool.go b/workerpool.go index 931ab35..03aee88 100644 --- a/workerpool.go +++ b/workerpool.go @@ -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) } }