/
process.go
51 lines (41 loc) · 1.25 KB
/
process.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
package pcap
import (
"os"
"os/signal"
"go.uber.org/zap"
"google.golang.org/grpc"
)
// Stoppable provides an interface for services that can be stopped.
//
// Primarily used with StopOnSignal.
type Stoppable interface {
Stop()
}
// WaitingStoppable adds a Wait() function to Stoppable, which allows implementing an appropriate wait.
type WaitingStoppable interface {
Stoppable
Wait()
}
// StopOnSignal is a reusable function to handle stop signals.
//
// The Stoppable interface defines what to do when stopping a particular process, and stopSignals defines a list of
// signals, for which Stop() is called.
//
// When a server is given, it is shut down gracefully.
//
// The provided Stoppable can also be a WaitingStoppable. Then the Wait() function is also called.
func StopOnSignal(log *zap.Logger, stoppable Stoppable, server *grpc.Server, stopSignals ...os.Signal) {
signals := make(chan os.Signal, 1)
signal.Notify(signals, stopSignals...)
sig := <-signals
log.Info("received signal, stopping.", zap.String("signal", sig.String()))
stoppable.Stop()
if waitingStoppable, ok := stoppable.(WaitingStoppable); ok {
log.Info("waiting for stop")
waitingStoppable.Wait()
}
if server != nil {
log.Info("shutting down server")
server.GracefulStop()
}
}