From cbd02c58be51b04f2e0b359997607a76db0ebfc1 Mon Sep 17 00:00:00 2001 From: Anis Elleuch Date: Wed, 31 Jul 2019 19:08:10 +0100 Subject: [PATCH] federation: Avoid printing context canceled error (#7997) Golang proactively prints this error `http: proxy error: context canceled` when a request arrived to the current deployment and redirected to another deployment in a federated setup. Since this error can confuse users, this commit will just hide it. --- cmd/generic-handlers.go | 3 +++ pkg/handlers/forwarder.go | 13 ++++++++++++- 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/cmd/generic-handlers.go b/cmd/generic-handlers.go index 6fadccc2b..d326fcc1f 100644 --- a/cmd/generic-handlers.go +++ b/cmd/generic-handlers.go @@ -746,6 +746,9 @@ func setBucketForwardingHandler(h http.Handler) http.Handler { fwd := handlers.NewForwarder(&handlers.Forwarder{ PassHost: true, RoundTripper: NewCustomHTTPTransport(), + Logger: func(err error) { + logger.LogIf(context.Background(), err) + }, }) return bucketForwardingHandler{fwd, h} } diff --git a/pkg/handlers/forwarder.go b/pkg/handlers/forwarder.go index 1065e1911..9c0eb2ad5 100644 --- a/pkg/handlers/forwarder.go +++ b/pkg/handlers/forwarder.go @@ -1,5 +1,5 @@ /* - * MinIO Cloud Storage, (C) 2018 MinIO, Inc. + * MinIO Cloud Storage, (C) 2018-2019 MinIO, Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -32,6 +32,7 @@ const defaultFlushInterval = time.Duration(100) * time.Millisecond type Forwarder struct { RoundTripper http.RoundTripper PassHost bool + Logger func(error) // internal variables rewriter *headerRewriter @@ -58,10 +59,20 @@ func (f *Forwarder) ServeHTTP(w http.ResponseWriter, inReq *http.Request) { }, Transport: f.RoundTripper, FlushInterval: defaultFlushInterval, + ErrorHandler: f.customErrHandler, } revproxy.ServeHTTP(w, outReq) } +// customErrHandler is originally implemented to avoid having the following error +// `http: proxy error: context canceled` printed by Golang +func (f *Forwarder) customErrHandler(w http.ResponseWriter, r *http.Request, err error) { + if f.Logger != nil && err != context.Canceled { + f.Logger(err) + } + w.WriteHeader(http.StatusBadGateway) +} + func (f *Forwarder) getURLFromRequest(req *http.Request) *url.URL { // If the Request was created by Go via a real HTTP request, RequestURI will // contain the original query string. If the Request was created in code, RequestURI