From 1b81d8f5427827b4cbdf53f635943c223650c78b Mon Sep 17 00:00:00 2001 From: Aliaksandr Valialkin Date: Fri, 27 Jan 2023 13:24:30 -0800 Subject: [PATCH] lib/netutil: move IsTrivialNetworkError() function there, since it is used in multiple places across the code --- app/vmselect/bufferedwriter/bufferedwriter.go | 16 ++++------------ lib/httpserver/httpserver.go | 14 +++----------- lib/netutil/netutil.go | 15 +++++++++++++++ 3 files changed, 22 insertions(+), 23 deletions(-) create mode 100644 lib/netutil/netutil.go diff --git a/app/vmselect/bufferedwriter/bufferedwriter.go b/app/vmselect/bufferedwriter/bufferedwriter.go index a06e59ecb..5e25d6ab3 100644 --- a/app/vmselect/bufferedwriter/bufferedwriter.go +++ b/app/vmselect/bufferedwriter/bufferedwriter.go @@ -4,8 +4,9 @@ import ( "bufio" "fmt" "io" - "strings" "sync" + + "github.com/VictoriaMetrics/VictoriaMetrics/lib/netutil" ) // Get returns buffered writer for the given w. @@ -63,7 +64,7 @@ func (bw *Writer) Write(p []byte) (int, error) { return 0, bw.err } n, err := bw.bw.Write(p) - if err != nil && !isTrivialNetworkError(err) { + if err != nil && !netutil.IsTrivialNetworkError(err) { bw.err = fmt.Errorf("cannot send %d bytes to client: %w", len(p), err) } return n, bw.err @@ -76,7 +77,7 @@ func (bw *Writer) Flush() error { if bw.err != nil { return bw.err } - if err := bw.bw.Flush(); err != nil && !isTrivialNetworkError(err) { + if err := bw.bw.Flush(); err != nil && !netutil.IsTrivialNetworkError(err) { bw.err = fmt.Errorf("cannot flush data to client: %w", err) } return bw.err @@ -88,12 +89,3 @@ func (bw *Writer) Error() error { defer bw.lock.Unlock() return bw.err } - -func isTrivialNetworkError(err error) bool { - // Suppress trivial network errors, which could occur at remote side. - s := err.Error() - if strings.Contains(s, "broken pipe") || strings.Contains(s, "reset by peer") { - return true - } - return false -} diff --git a/lib/httpserver/httpserver.go b/lib/httpserver/httpserver.go index 1668489ef..09a54c505 100644 --- a/lib/httpserver/httpserver.go +++ b/lib/httpserver/httpserver.go @@ -199,7 +199,7 @@ func gzipHandler(s *server, rh RequestHandler) http.HandlerFunc { w = maybeGzipResponseWriter(w, r) handlerWrapper(s, w, r, rh) if zrw, ok := w.(*gzipResponseWriter); ok { - if err := zrw.Close(); err != nil && !isTrivialNetworkError(err) { + if err := zrw.Close(); err != nil && !netutil.IsTrivialNetworkError(err) { logger.Warnf("gzipResponseWriter.Close: %s", err) } } @@ -503,10 +503,10 @@ func (zrw *gzipResponseWriter) Flush() { _, _ = zrw.Write(nil) } if !zrw.disableCompression { - if err := zrw.bw.Flush(); err != nil && !isTrivialNetworkError(err) { + if err := zrw.bw.Flush(); err != nil && !netutil.IsTrivialNetworkError(err) { logger.Warnf("gzipResponseWriter.Flush (buffer): %s", err) } - if err := zrw.zw.Flush(); err != nil && !isTrivialNetworkError(err) { + if err := zrw.zw.Flush(); err != nil && !netutil.IsTrivialNetworkError(err) { logger.Warnf("gzipResponseWriter.Flush (gzip): %s", err) } } @@ -643,14 +643,6 @@ func (e *ErrorWithStatusCode) Error() string { return e.Err.Error() } -func isTrivialNetworkError(err error) bool { - s := err.Error() - if strings.Contains(s, "broken pipe") || strings.Contains(s, "reset by peer") { - return true - } - return false -} - // IsTLS indicates is tls enabled or not func IsTLS() bool { return *tlsEnable diff --git a/lib/netutil/netutil.go b/lib/netutil/netutil.go new file mode 100644 index 000000000..91f72296c --- /dev/null +++ b/lib/netutil/netutil.go @@ -0,0 +1,15 @@ +package netutil + +import ( + "strings" +) + +// IsTrivialNetworkError returns true if the err can be ignored during logging. +func IsTrivialNetworkError(err error) bool { + // Suppress trivial network errors, which could occur at remote side. + s := err.Error() + if strings.Contains(s, "broken pipe") || strings.Contains(s, "reset by peer") { + return true + } + return false +}