13697: Cancel request context after API.RequestTimeout.
[arvados.git] / lib / service / cmd.go
index 1e7a9a36edd3a8142192d14bfcfbf12885e1e857..e67c24f65f39cea4929c95fe30abbdc5ab98a901 100644 (file)
@@ -2,7 +2,7 @@
 //
 // SPDX-License-Identifier: Apache-2.0
 
-// package service provides a cmd.Handler that brings up a system service.
+// Package service provides a cmd.Handler that brings up a system service.
 package service
 
 import (
@@ -12,6 +12,7 @@ import (
        "io"
        "net"
        "net/http"
+       _ "net/http/pprof"
        "net/url"
        "os"
        "strings"
@@ -70,6 +71,7 @@ func (c *command) RunCommand(prog string, args []string, stdin io.Reader, stdout
        loader := config.NewLoader(stdin, log)
        loader.SetupFlags(flags)
        versionFlag := flags.Bool("version", false, "Write version information to stdout and exit 0")
+       pprofAddr := flags.String("pprof", "", "Serve Go profile data at `[addr]:port`")
        err = flags.Parse(args)
        if err == flag.ErrHelp {
                err = nil
@@ -80,6 +82,12 @@ func (c *command) RunCommand(prog string, args []string, stdin io.Reader, stdout
                return cmd.Version.RunCommand(prog, args, stdin, stdout, stderr)
        }
 
+       if *pprofAddr != "" {
+               go func() {
+                       log.Println(http.ListenAndServe(*pprofAddr, nil))
+               }()
+       }
+
        if strings.HasSuffix(prog, "controller") {
                // Some config-loader checks try to make API calls via
                // controller. Those can't be expected to work if this
@@ -118,13 +126,14 @@ func (c *command) RunCommand(prog string, args []string, stdin io.Reader, stdout
        }
 
        instrumented := httpserver.Instrument(reg, log,
-               httpserver.HandlerWithContext(ctx,
+               httpserver.HandlerWithDeadline(cluster.API.RequestTimeout.Duration(),
                        httpserver.AddRequestIDs(
                                httpserver.LogRequests(
                                        httpserver.NewRequestLimiter(cluster.API.MaxConcurrentRequests, handler, reg)))))
        srv := &httpserver.Server{
                Server: http.Server{
-                       Handler: instrumented.ServeAPI(cluster.ManagementToken, instrumented),
+                       Handler:     instrumented.ServeAPI(cluster.ManagementToken, instrumented),
+                       BaseContext: func(net.Listener) context.Context { return ctx },
                },
                Addr: listenURL.Host,
        }
@@ -165,8 +174,6 @@ func (c *command) RunCommand(prog string, args []string, stdin io.Reader, stdout
        return 0
 }
 
-const rfc3339NanoFixed = "2006-01-02T15:04:05.000000000Z07:00"
-
 func getListenAddr(svcs arvados.Services, prog arvados.ServiceName, log logrus.FieldLogger) (arvados.URL, error) {
        svc, ok := svcs.Map()[prog]
        if !ok {
@@ -177,6 +184,9 @@ func getListenAddr(svcs arvados.Services, prog arvados.ServiceName, log logrus.F
        } else if url, err := url.Parse(want); err != nil {
                return arvados.URL{}, fmt.Errorf("$ARVADOS_SERVICE_INTERNAL_URL (%q): %s", want, err)
        } else {
+               if url.Path == "" {
+                       url.Path = "/"
+               }
                return arvados.URL(*url), nil
        }