-
Notifications
You must be signed in to change notification settings - Fork 0
/
shutdown.go
108 lines (85 loc) · 2.14 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
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
96
97
98
99
100
101
102
103
104
105
106
107
108
package goshutdown
import (
"context"
"errors"
"fmt"
"os"
"os/signal"
"syscall"
"time"
)
var ErrShutdown = errors.New("shutdown error")
const (
DefaultTimeout = 10 * time.Second
)
type (
Handler = func(ctx context.Context) error
NotifyContext = func(ctx context.Context, sig ...os.Signal) (context.Context, context.CancelFunc)
)
type Shutdown struct {
Timeout time.Duration
Handler Handler
Signals []os.Signal
NotifyContext NotifyContext
}
// New creates a new Shutdown instance.
//
// The default timeout is 10 seconds.
func New() *Shutdown {
return &Shutdown{
Timeout: DefaultTimeout,
Handler: nil,
Signals: []os.Signal{syscall.SIGINT, syscall.SIGTERM},
NotifyContext: signal.NotifyContext,
}
}
// WithTimeout sets the timeout for the shutdown process.
func (s *Shutdown) WithTimeout(timeout time.Duration) *Shutdown {
s.Timeout = timeout
return s
}
// WithHandler sets the handler for the shutdown process.
func (s *Shutdown) WithHandler(handler Handler) *Shutdown {
s.Handler = handler
return s
}
// WithNotifyContext sets the notify context function.
func (s *Shutdown) WithNotifyContext(notifyContext NotifyContext) *Shutdown {
s.NotifyContext = notifyContext
return s
}
// WithSignals sets the signals to listen for.
func (s *Shutdown) WithSignals(signals ...os.Signal) *Shutdown {
s.Signals = signals
return s
}
// Wait waits for the shutdown signal to be received or the timeout to expire.
// It returns an error if the shutdown process encounters an error or if the timeout is exceeded.
func (s *Shutdown) Wait() error {
ctx, stop := s.NotifyContext(context.Background(), s.Signals...)
defer stop()
<-ctx.Done()
ctx, cancel := context.WithTimeout(context.Background(), s.Timeout)
defer cancel()
done := make(chan error)
if s.Handler != nil {
go func() {
defer close(done)
err := s.Handler(ctx)
if err != nil {
done <- err
}
}()
}
select {
case err := <-done:
if err != nil {
return fmt.Errorf("%w: %w", ErrShutdown, err)
}
case <-ctx.Done():
}
if err := ctx.Err(); err != nil {
return fmt.Errorf("%w: %w", ErrShutdown, err)
}
return nil
}