lib/netutil: move IsTrivialNetworkError() function there, since it is used in multiple places across the code

This commit is contained in:
Aliaksandr Valialkin 2023-01-27 13:24:30 -08:00
parent 7e355080ce
commit 1b81d8f542
No known key found for this signature in database
GPG key ID: A72BEC6CD3D0DED1
3 changed files with 22 additions and 23 deletions

View file

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

View file

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

15
lib/netutil/netutil.go Normal file
View file

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