-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathoption.go
More file actions
58 lines (50 loc) · 1.12 KB
/
option.go
File metadata and controls
58 lines (50 loc) · 1.12 KB
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
package reload
import (
"os"
"syscall"
)
type options struct {
logger Logger
sigHandle sigHandle
}
// Option 参数
type Option func(*options)
var defaultOptions = &options{
logger: &defaultLogger{},
sigHandle: make(sigHandle),
}
// evaluateOptions 参数处理
func evaluateOptions(opts []Option) *options {
optCopy := &options{}
*optCopy = *defaultOptions
for _, o := range opts {
o(optCopy)
}
return optCopy
}
// WithLogger 日志
func WithLogger(l Logger) Option {
return func(o *options) {
o.logger = l
}
}
// WithHandleFunc 信号处理
func WithHandleFunc(s os.Signal, h HandleFunc) Option {
return func(o *options) {
o.sigHandle[s] = h
}
}
// WithDefaultHandle 默认信号处理
func WithDefaultHandle() Option {
return func(o *options) {
o.sigHandle[syscall.SIGUSR1] = func(s Service) {
if err := s.Reload(); err != nil {
s.Logger().Error(err)
}
}
// 设置会退出的信号量
o.sigHandle[syscall.SIGINT] = func(s Service) { s.Shutdown() }
o.sigHandle[syscall.SIGTERM] = func(s Service) { s.Shutdown() }
o.sigHandle[syscall.SIGTSTP] = func(s Service) { s.Shutdown() }
}
}