13697: Remove HandlerWithContext.
authorTom Clegg <tom@curii.com>
Thu, 23 Sep 2021 13:44:29 +0000 (09:44 -0400)
committerTom Clegg <tom@curii.com>
Thu, 23 Sep 2021 15:12:21 +0000 (11:12 -0400)
Use http.Server's BaseContext feature instead.

Arvados-DCO-1.1-Signed-off-by: Tom Clegg <tom@curii.com>

lib/controller/auth_test.go
lib/controller/federation_test.go
lib/controller/server_test.go
sdk/go/httpserver/logger.go
services/keep-web/server.go

index 69458655ba0c8ce7caf9839d05eb5985f3fd0b7b..17524114671e840ecdac05e2457d9ebbb96c5635 100644 (file)
@@ -8,6 +8,7 @@ import (
        "context"
        "encoding/json"
        "fmt"
+       "net"
        "net/http"
        "net/http/httptest"
        "os"
@@ -99,9 +100,10 @@ func (s *AuthSuite) SetUpTest(c *check.C) {
 
        s.testHandler = &Handler{Cluster: cluster}
        s.testServer = newServerFromIntegrationTestEnv(c)
-       s.testServer.Server.Handler = httpserver.HandlerWithContext(
-               ctxlog.Context(context.Background(), s.log),
-               httpserver.AddRequestIDs(httpserver.LogRequests(s.testHandler)))
+       s.testServer.Server.BaseContext = func(net.Listener) context.Context {
+               return ctxlog.Context(context.Background(), s.log)
+       }
+       s.testServer.Server.Handler = httpserver.AddRequestIDs(httpserver.LogRequests(s.testHandler))
        c.Assert(s.testServer.Start(), check.IsNil)
 }
 
index 6d74ab65f93b6f7c73c95905b601a6090bb50a70..211c7619809ed6a8855248915facef843da55081 100644 (file)
@@ -11,6 +11,7 @@ import (
        "fmt"
        "io"
        "io/ioutil"
+       "net"
        "net/http"
        "net/http/httptest"
        "net/url"
@@ -71,9 +72,10 @@ func (s *FederationSuite) SetUpTest(c *check.C) {
        arvadostest.SetServiceURL(&cluster.Services.Controller, "http://localhost:/")
        s.testHandler = &Handler{Cluster: cluster}
        s.testServer = newServerFromIntegrationTestEnv(c)
-       s.testServer.Server.Handler = httpserver.HandlerWithContext(
-               ctxlog.Context(context.Background(), s.log),
-               httpserver.AddRequestIDs(httpserver.LogRequests(s.testHandler)))
+       s.testServer.Server.BaseContext = func(net.Listener) context.Context {
+               return ctxlog.Context(context.Background(), s.log)
+       }
+       s.testServer.Server.Handler = httpserver.AddRequestIDs(httpserver.LogRequests(s.testHandler))
 
        cluster.RemoteClusters = map[string]arvados.RemoteCluster{
                "zzzzz": {
index 051f355716c421e486e25ce1fb717d8355847e30..b2b3365a2015b2ac899a3b62f45d563042267ac9 100644 (file)
@@ -6,6 +6,7 @@ package controller
 
 import (
        "context"
+       "net"
        "net/http"
        "os"
        "path/filepath"
@@ -48,9 +49,10 @@ func newServerFromIntegrationTestEnv(c *check.C) *httpserver.Server {
 
        srv := &httpserver.Server{
                Server: http.Server{
-                       Handler: httpserver.HandlerWithContext(
-                               ctxlog.Context(context.Background(), log),
-                               httpserver.AddRequestIDs(httpserver.LogRequests(handler))),
+                       BaseContext: func(net.Listener) context.Context {
+                               return ctxlog.Context(context.Background(), log)
+                       },
+                       Handler: httpserver.AddRequestIDs(httpserver.LogRequests(handler)),
                },
                Addr: ":",
        }
index 1916880963494333ce9d4356f1d1c9d2eba55f3b..a0ca6bf28d8d37daf2f8a0406d9ce15e7d4571e6 100644 (file)
@@ -22,15 +22,6 @@ var (
        requestTimeContextKey = contextKey{"requestTime"}
 )
 
-// HandlerWithContext returns an http.Handler that changes the request
-// context to ctx (replacing http.Server's default
-// context.Background()), then calls next.
-func HandlerWithContext(ctx context.Context, next http.Handler) http.Handler {
-       return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
-               next.ServeHTTP(w, r.WithContext(ctx))
-       })
-}
-
 // HandlerWithDeadline cancels the request context if the request
 // takes longer than the specified timeout.
 func HandlerWithDeadline(timeout time.Duration, next http.Handler) http.Handler {
index 8f623c627d067f843f2746a2b8b64248006b1a18..b0375ff8d9496dfc41c910a969963209649f9531 100644 (file)
@@ -6,6 +6,7 @@ package main
 
 import (
        "context"
+       "net"
        "net/http"
 
        "git.arvados.org/arvados.git/sdk/go/arvados"
@@ -24,10 +25,10 @@ func (srv *server) Start(logger *logrus.Logger) error {
        h := &handler{Config: srv.Config}
        reg := prometheus.NewRegistry()
        h.Config.Cache.registry = reg
-       ctx := ctxlog.Context(context.Background(), logger)
-       mh := httpserver.Instrument(reg, logger, httpserver.HandlerWithContext(ctx, httpserver.AddRequestIDs(httpserver.LogRequests(h))))
+       mh := httpserver.Instrument(reg, logger, httpserver.AddRequestIDs(httpserver.LogRequests(h)))
        h.MetricsAPI = mh.ServeAPI(h.Config.cluster.ManagementToken, http.NotFoundHandler())
        srv.Handler = mh
+       srv.BaseContext = func(net.Listener) context.Context { return ctxlog.Context(context.Background(), logger) }
        var listen arvados.URL
        for listen = range srv.Config.cluster.Services.WebDAV.InternalURLs {
                break