-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoption.go
41 lines (34 loc) · 820 Bytes
/
option.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
package graceful
import (
"time"
)
// Option interface
type Option interface {
// apply option to server
apply(*Server)
}
// option wraps a function that modifies Server into an
// implementation of the Option interface.
type option struct {
f func(*Server)
}
func (o *option) apply(s *Server) {
o.f(s)
}
func newOption(f func(s *Server)) *option {
return &option{f: f}
}
// WithShutdownTimeout set timeout for shutting down server
func WithShutdownTimeout(timeout time.Duration) Option {
return newOption(func(s *Server) {
s.ShutdownTimeout = timeout
})
}
// WithShutdownFunc registers function(s) running while shutting down by calling s.RegisterOnShutdown.
func WithShutdownFunc(fs ...func()) Option {
return newOption(func(s *Server) {
for _, f := range fs {
s.RegisterOnShutdown(f)
}
})
}