-
Notifications
You must be signed in to change notification settings - Fork 2
/
shutdown.go
53 lines (43 loc) · 1.32 KB
/
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
package main
import (
"context"
"os"
"time"
logrus "github.com/sirupsen/logrus"
)
// GRPCServer defines an interface for a gRPC server.
type GRPCServer interface {
GracefulStop()
}
// HTTPServer defines an interface for an HTTP server.
type HTTPServer interface {
Shutdown(ctx context.Context) error
}
// gracefulShutdown handles graceful shutdown of the servers.
func gracefulShutdown(sigChan chan os.Signal, grpcServer GRPCServer,
httpServer HTTPServer, pprofServer HTTPServer) {
// Block until a signal is received.
<-sigChan
logrus.Info("Shutting down servers...")
// Graceful shutdown the gRPC server.
grpcServer.GracefulStop()
logrus.Info("gRPC server has been stopped.")
// Graceful shutdown the HTTP server.
if err := httpServer.Shutdown(context.Background()); err != nil {
logrus.Errorf("HTTP server shutdown error: %v", err)
} else {
logrus.Info("HTTP server has been stopped.")
}
// Create context timeout with 5 seconds for pprof server to shutdown.
pprofCtx, pprofCancel := context.WithTimeout(
context.Background(), 5*time.Second,
)
defer pprofCancel()
// Graceful shutdown the pprof server.
if err := pprofServer.Shutdown(pprofCtx); err != nil {
logrus.Errorf("PProf server shutdown error: %v", err)
} else {
logrus.Info("PProf server has been stopped.")
}
logrus.Info("Exited gracefully")
}