From: Tom Clegg Date: Fri, 24 Jun 2022 15:25:59 +0000 (-0400) Subject: 19205: Remove redundant RequestTimeout implementation. X-Git-Tag: 2.5.0~129^2~1 X-Git-Url: https://git.arvados.org/arvados.git/commitdiff_plain/13ea738547ce7232c152873970770c21e97d2830 19205: Remove redundant RequestTimeout implementation. lib/controller doesn't need its own -- it uses lib/service, which uses httpserver.HandlerWithDeadline. Arvados-DCO-1.1-Signed-off-by: Tom Clegg --- diff --git a/lib/controller/handler.go b/lib/controller/handler.go index f5840b34ce..665fd5c636 100644 --- a/lib/controller/handler.go +++ b/lib/controller/handler.go @@ -13,7 +13,6 @@ import ( "net/url" "strings" "sync" - "time" "git.arvados.org/arvados.git/lib/controller/api" "git.arvados.org/arvados.git/lib/controller/federation" @@ -61,12 +60,6 @@ func (h *Handler) ServeHTTP(w http.ResponseWriter, req *http.Request) { req.URL.Path = strings.Replace(req.URL.Path, "//", "/", -1) } } - if h.Cluster.API.RequestTimeout > 0 { - ctx, cancel := context.WithDeadline(req.Context(), time.Now().Add(time.Duration(h.Cluster.API.RequestTimeout))) - req = req.WithContext(ctx) - defer cancel() - } - h.handlerStack.ServeHTTP(w, req) } diff --git a/lib/controller/handler_test.go b/lib/controller/handler_test.go index 5e467cb058..39c2b1c68e 100644 --- a/lib/controller/handler_test.go +++ b/lib/controller/handler_test.go @@ -204,17 +204,21 @@ func (s *HandlerSuite) TestProxyDiscoveryDoc(c *check.C) { c.Check(len(dd.Schemas), check.Not(check.Equals), 0) } -func (s *HandlerSuite) TestRequestTimeout(c *check.C) { - s.cluster.API.RequestTimeout = arvados.Duration(time.Nanosecond) - req := httptest.NewRequest("GET", "/discovery/v1/apis/arvados/v1/rest", nil) +// Handler should give up and exit early if request context is +// cancelled due to client hangup, httpserver.HandlerWithDeadline, +// etc. +func (s *HandlerSuite) TestRequestCancel(c *check.C) { + ctx, cancel := context.WithCancel(context.Background()) + req := httptest.NewRequest("GET", "/discovery/v1/apis/arvados/v1/rest", nil).WithContext(ctx) resp := httptest.NewRecorder() + cancel() s.handler.ServeHTTP(resp, req) c.Check(resp.Code, check.Equals, http.StatusBadGateway) var jresp httpserver.ErrorResponse err := json.Unmarshal(resp.Body.Bytes(), &jresp) c.Check(err, check.IsNil) c.Assert(len(jresp.Errors), check.Equals, 1) - c.Check(jresp.Errors[0], check.Matches, `.*context deadline exceeded.*`) + c.Check(jresp.Errors[0], check.Matches, `.*context canceled`) } func (s *HandlerSuite) TestProxyWithoutToken(c *check.C) { diff --git a/sdk/go/httpserver/logger.go b/sdk/go/httpserver/logger.go index 5a46635e91..b71adf7118 100644 --- a/sdk/go/httpserver/logger.go +++ b/sdk/go/httpserver/logger.go @@ -47,7 +47,13 @@ func (hn hijackNotifier) Hijack() (net.Conn, *bufio.ReadWriter, error) { // HandlerWithDeadline cancels the request context if the request // takes longer than the specified timeout without having its // connection hijacked. +// +// If timeout is 0, there is no deadline: HandlerWithDeadline is a +// no-op. func HandlerWithDeadline(timeout time.Duration, next http.Handler) http.Handler { + if timeout == 0 { + return next + } return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { ctx, cancel := context.WithCancel(r.Context()) defer cancel()