-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhttp_shutdown.go
95 lines (76 loc) · 2.39 KB
/
http_shutdown.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
package ctrl
import (
"context"
"errors"
"log/slog"
"net/http"
"time"
)
// HTTPOption represents a functional option for HTTP server operations.
type HTTPOption func(*httpOptions)
type httpOptions struct {
shutdownTimeout time.Duration
logger *slog.Logger
}
// WithHTTPShutdownTimeout sets the maximum time to wait for server shutdown.
func WithHTTPShutdownTimeout(timeout time.Duration) HTTPOption {
return func(o *httpOptions) {
o.shutdownTimeout = timeout
}
}
// WithHTTPLogger sets a custom logger for HTTP server operations.
func WithHTTPLogger(logger *slog.Logger) HTTPOption {
return func(o *httpOptions) {
o.logger = logger
}
}
// ShutdownHTTPServer gracefully shuts down an HTTP server with a timeout.
// It returns any error encountered during shutdown.
func ShutdownHTTPServer(ctx context.Context, server *http.Server, opts ...HTTPOption) error {
options := httpOptions{
shutdownTimeout: 10 * time.Second, // default shutdown timeout
}
for _, opt := range opts {
opt(&options)
}
shutdownCtx, cancel := context.WithTimeout(ctx, options.shutdownTimeout)
defer cancel()
return server.Shutdown(shutdownCtx)
}
// RunHTTPServerWithContext runs a server start function and ensures it shuts down gracefully
// when the provided context is canceled.
// The startFn is responsible for starting the server (e.g., ListenAndServe).
// It returns a channel that will receive any error from the server.
func RunHTTPServerWithContext(ctx context.Context, server *http.Server, startFn func() error, opts ...HTTPOption) <-chan error {
options := httpOptions{
shutdownTimeout: 10 * time.Second, // default timeout
logger: slog.Default(),
}
for _, opt := range opts {
opt(&options)
}
// channel to report server errors
errCh := make(chan error, 1)
// start server
go func() {
err := startFn()
// only report non-shutdown errors
if err != nil && !errors.Is(err, http.ErrServerClosed) {
errCh <- err
} else {
errCh <- nil
}
close(errCh)
}()
// handle graceful shutdown
go func() {
<-ctx.Done()
options.logger.Info("shutting down HTTP server")
shutdownCtx, cancel := context.WithTimeout(context.Background(), options.shutdownTimeout)
defer cancel()
if err := server.Shutdown(shutdownCtx); err != nil { //nolint:contextcheck // context non-inherited intentionally
options.logger.Error("server shutdown error", "error", err)
}
}()
return errCh
}