-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshutdown.go
163 lines (140 loc) · 5.05 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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
// Copyright 2024 The seacraft Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http:www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package shutdown
import (
"sync"
)
// ShutdownCallback is an interface you have to implement for callbacks.
// OnShutdown will be called when shutdown is requested. The parameter
// is the name of the ShutdownManager that requested shutdown.
type ShutdownCallback interface {
OnShutdown(string) error
}
// ShutdownFunc is a helper type, so you can easily provide anonymous functions
// as ShutdownCallbacks.
type ShutdownFunc func(string) error
// OnShutdown defines the action needed to run when shutdown triggered.
func (f ShutdownFunc) OnShutdown(shutdownManager string) error {
return f(shutdownManager)
}
// ShutdownManager is an interface implemnted by ShutdownManagers.
// GetName returns the name of ShutdownManager.
// ShutdownManagers start listening for shutdown requests in Start.
// When they call StartShutdown on GSInterface,
// first ShutdownStart() is called, then all ShutdownCallbacks are executed
// and once all ShutdownCallbacks return, ShutdownFinish is called.
type ShutdownManager interface {
GetName() string
Start(gs GSInterface) error
ShutdownStart() error
ShutdownFinish() error
}
// ErrorHandler is an interface you can pass to SetErrorHandler to
// handle asynchronous errors.
type ErrorHandler interface {
OnError(err error)
}
// ErrorFunc is a helper type, so you can easily provide anonymous functions
// as ErrorHandlers.
type ErrorFunc func(err error)
// OnError defines the action needed to run when error occurred.
func (f ErrorFunc) OnError(err error) {
f(err)
}
// GSInterface is an interface implemented by GracefulShutdown,
// that gets passed to ShutdownManager to call StartShutdown when shutdown
// is requested.
type GSInterface interface {
StartShutdown(sm ShutdownManager)
ReportError(err error)
AddShutdownCallback(shutdownCallback ShutdownCallback)
}
// GracefulShutdown is main struct that handles ShutdownCallbacks and
// ShutdownManagers. Initialize it with New.
type GracefulShutdown struct {
callbacks []ShutdownCallback
managers []ShutdownManager
errorHandler ErrorHandler
}
// New initializes GracefulShutdown.
func New() *GracefulShutdown {
return &GracefulShutdown{
callbacks: make([]ShutdownCallback, 0, 10),
managers: make([]ShutdownManager, 0, 3),
}
}
// Start calls Start on all added ShutdownManagers. The ShutdownManagers
// start to listen to shutdown requests. Returns an error if any ShutdownManagers
// return an error.
func (gs *GracefulShutdown) Start() error {
for _, manager := range gs.managers {
if err := manager.Start(gs); err != nil {
return err
}
}
return nil
}
// AddShutdownManager adds a ShutdownManager that will listen to shutdown requests.
func (gs *GracefulShutdown) AddShutdownManager(manager ShutdownManager) {
gs.managers = append(gs.managers, manager)
}
// AddShutdownCallback adds a ShutdownCallback that will be called when
// shutdown is requested.
//
// You can provide anything that implements ShutdownCallback interface,
// or you can supply a function like this:
//
// AddShutdownCallback(shutdown.ShutdownFunc(func() error {
// // callback code
// return nil
// }))
func (gs *GracefulShutdown) AddShutdownCallback(shutdownCallback ShutdownCallback) {
gs.callbacks = append(gs.callbacks, shutdownCallback)
}
// SetErrorHandler sets an ErrorHandler that will be called when an error
// is encountered in ShutdownCallback or in ShutdownManager.
//
// You can provide anything that implements ErrorHandler interface,
// or you can supply a function like this:
//
// SetErrorHandler(shutdown.ErrorFunc(func (err error) {
// // handle error
// }))
func (gs *GracefulShutdown) SetErrorHandler(errorHandler ErrorHandler) {
gs.errorHandler = errorHandler
}
// StartShutdown is called from a ShutdownManager and will initiate shutdown.
// first call ShutdownStart on Shutdownmanager,
// call all ShutdownCallbacks, wait for callbacks to finish and
// call ShutdownFinish on ShutdownManager.
func (gs *GracefulShutdown) StartShutdown(sm ShutdownManager) {
gs.ReportError(sm.ShutdownStart())
var wg sync.WaitGroup
for _, shutdownCallback := range gs.callbacks {
wg.Add(1)
go func(shutdownCallback ShutdownCallback) {
defer wg.Done()
gs.ReportError(shutdownCallback.OnShutdown(sm.GetName()))
}(shutdownCallback)
}
wg.Wait()
gs.ReportError(sm.ShutdownFinish())
}
// ReportError is a function that can be used to report errors to
// ErrorHandler. It is used in ShutdownManagers.
func (gs *GracefulShutdown) ReportError(err error) {
if err != nil && gs.errorHandler != nil {
gs.errorHandler.OnError(err)
}
}