cmd/composer: gracefully shut down on SIG{INT,TERM}

Call `Shutdown()` on all http servers. This means we will finish processing
any pending requests (including depsolving), but we will not listen to new
ones.

In particular, we will not answer to the readiness probe, so no new traffic
will be routed to this container.

Once all pending requests have been handled composer will shut down
gracefully and the liveness probe will return failure.

Note that in order for this to work correctly no requests should ever take longer
than the shutdown timeout (by default 30s).
This commit is contained in:
Tom Gundersen 2022-03-16 00:49:53 +00:00 committed by Ondřej Budai
parent d3cd3197c0
commit c3d66b5a33
2 changed files with 120 additions and 71 deletions

View file

@ -3,6 +3,7 @@ package weldr
import (
"archive/tar"
"bytes"
"context"
"crypto/rand"
"encoding/json"
errors_package "errors"
@ -52,6 +53,7 @@ type API struct {
logger *log.Logger
router *httprouter.Router
server http.Server
compatOutputDir string
@ -271,9 +273,9 @@ func setupRouter(api *API) *API {
}
func (api *API) Serve(listener net.Listener) error {
server := http.Server{Handler: api}
api.server = http.Server{Handler: api}
err := server.Serve(listener)
err := api.server.Serve(listener)
if err != nil && err != http.ErrServerClosed {
return err
}
@ -281,6 +283,10 @@ func (api *API) Serve(listener net.Listener) error {
return nil
}
func (api *API) Shutdown(ctx context.Context) error {
return api.server.Shutdown(ctx)
}
func (api *API) ServeHTTP(writer http.ResponseWriter, request *http.Request) {
if api.logger != nil {
log.Println(request.Method, request.URL.Path)