/
server.go
53 lines (43 loc) · 1.36 KB
/
server.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
package webutil
import (
"context"
"net"
"net/http"
"time"
"github.com/pkg/errors"
"github.com/rebuy-de/rebuy-go-sdk/v7/pkg/logutil"
"golang.org/x/sync/errgroup"
)
// ListenAndServeWithContext does the same as http.ListenAndServe with the
// difference that is properly utilises the context. This means it does a
// graceful shutdown when the context is done and a context cancellation gets
// propagated down to the actual request context.
func ListenAndServeWithContext(ctx context.Context, addr string, handler http.Handler) error {
server := &http.Server{
Addr: addr,
Handler: handler,
BaseContext: func(_ net.Listener) context.Context {
ctx := logutil.Start(ctx, "request")
return ctx
},
}
grp, ctx := errgroup.WithContext(ctx)
grp.Go(func() error {
err := server.ListenAndServe()
if err == http.ErrServerClosed {
// We do not want to print an error on graceful shutdown.
return nil
}
return errors.WithStack(err)
})
grp.Go(func() error {
<-ctx.Done()
logutil.Get(ctx).Warn("Got shutdown signal")
time.Sleep(3 * time.Second) // Give systems some time to populate shutdown.
logutil.Get(ctx).Debug("Shutting down")
shutdownCtx, cancel := context.WithTimeout(context.Background(), 15*time.Second)
defer cancel()
return errors.WithStack(server.Shutdown(shutdownCtx))
})
return errors.Wrap(grp.Wait(), "http server failed")
}