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 341026902e
commit bccbe07c33
3 changed files with 22 additions and 23 deletions

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