审核有关忽略上下文取消功能的警告

I am passing a timeout context to Server.Shutdown (http package). I don't see that I would ever need to call the cancel function returned so I ignore it. But when I run go vet it says the cancel function returned by context.WithTimeout should be called, not discarded, to avoid a context leak.

How do I fix the problem or avoid the go vet error message, if there is no problem?

    go signalShutdown(server, stopCh)

    if err := server.ListenAndServeTLS(cert, key); err != http.ErrServerClosed {
        log.Fatalf("ListenAndServeTLS() error: %v
", err)
    }
    // Note: exit here does not terminate main()
}

// signalShutdown waits for a notification from the OS that the http server
// should be shutdown, then gracefully stops it.
func signalShutdown(server *http.Server, stopCh <-chan struct{}) {
    const ForceShutdownAfter = 10 // Shutdown context times out after this many seconds

    // Setup chan to receive notification of when server should shut down
    quitCh := make(chan os.Signal, 1)
    signal.Notify(quitCh, os.Interrupt, syscall.SIGINT, syscall.SIGTERM)

    // Wait until we get a notification to stop the server
    select {
    case <-quitCh:
        log.Println("WEB : OS signal received on", server.Addr)
    case <-stopCh:
        log.Println("WEB : Shutdown message received on", server.Addr)
    }

    context, _ := context.WithTimeout(context.Background(), ForceShutdownAfter*time.Second)

    // Tell the server to shutdown but only after blocking new connections and waiting for the
    // existing connections to finish (OR if context expires - see ForceShutdownAfter above)
    if err := server.Shutdown(context); err != nil {
        log.Fatalf("Shutdown() error: %v", err)
    }

    os.Exit(0)
}

... the cancel function returned by context.WithTimeout should be called, not discarded, to avoid a context leak.

How do I fix the problem or avoid the go vet error message, if there is no problem?

By calling, not discarding the cancel function, as documented:

context, cancel := context.WithTimeout(context.Background(), ForceShutdownAfter*time.Second)
defer cancel()