Skip to content

Commit

Permalink
ignore errors caused by request cancellation
Browse files Browse the repository at this point in the history
  • Loading branch information
ShoshinNikita committed Jul 3, 2024
1 parent 9b7ea42 commit 8a61be8
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 8 deletions.
2 changes: 1 addition & 1 deletion rclone/rclone.go
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ func (r *Rclone) redirectRcloneLogs(pipe io.Reader) {
text := s.Text()

// Skip errors caused by request cancellation.
if strings.Contains(text, "Didn't finish writing GET request") {
if strings.Contains(text, "Didn't finish writing GET request") || strings.Contains(text, "context canceled") {
continue
}

Expand Down
29 changes: 22 additions & 7 deletions web/middlewares.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,17 +36,21 @@ func loggingMiddleware(h http.Handler) http.Handler {
}

now := time.Now()
rw := newResponseWriter(w)
rw := newResponseWriter(w, r)

h.ServeHTTP(rw, r)

if rw.errMsg.Len() > 0 {
if !rw.requestCanceledByUser && rw.errMsg.Len() > 0 {
rlog.Errorf(`request "%s %s" failed with code %d: %s`, r.Method, r.URL.Path, rw.statusCode, rw.errMsg.String())
}

statusCode := rw.statusCode
if rw.requestCanceledByUser {
statusCode = 499 // 499 Client Closed Request (Nginx)
}
metrics.HTTPResponseStatuses.
With(prometheus.Labels{
"status": strconv.Itoa(rw.statusCode),
"status": strconv.Itoa(statusCode),
}).
Inc()

Expand All @@ -60,15 +64,18 @@ func loggingMiddleware(h http.Handler) http.Handler {

type responseWriter struct {
w http.ResponseWriter
r *http.Request

statusCode int
headerWritten bool
errMsg *bytes.Buffer
statusCode int
requestCanceledByUser bool
headerWritten bool
errMsg *bytes.Buffer
}

func newResponseWriter(w http.ResponseWriter) *responseWriter {
func newResponseWriter(w http.ResponseWriter, r *http.Request) *responseWriter {
return &responseWriter{
w: w,
r: r,
errMsg: bytes.NewBuffer(nil),
}
}
Expand All @@ -90,6 +97,14 @@ func (rw *responseWriter) Write(data []byte) (int, error) {

func (rw *responseWriter) WriteHeader(code int) {
if !rw.headerWritten {
// Check the context just before writing the header to make sure that request
// was canceled during its processing, if it was.
select {
case <-rw.r.Context().Done():
rw.requestCanceledByUser = true
default:
}

rw.w.WriteHeader(code)
}

Expand Down

0 comments on commit 8a61be8

Please sign in to comment.