Now specify HandleSignal as an option to toggle signal handling. (#948)

Signed-off-by: Erik Hollensbe <github@hollensbe.org>
This commit is contained in:
Erik Hollensbe 2019-11-16 03:13:34 -08:00 committed by Asim Aslam
parent bec13a45cd
commit a1c6cdf193
2 changed files with 15 additions and 1 deletions

View File

@ -31,6 +31,8 @@ type Options struct {
// Other options for implementations of the interface // Other options for implementations of the interface
// can be stored in a context // can be stored in a context
Context context.Context Context context.Context
Signal bool
} }
func newOptions(opts ...Option) Options { func newOptions(opts ...Option) Options {
@ -42,6 +44,7 @@ func newOptions(opts ...Option) Options {
Registry: registry.DefaultRegistry, Registry: registry.DefaultRegistry,
Transport: transport.DefaultTransport, Transport: transport.DefaultTransport,
Context: context.Background(), Context: context.Background(),
Signal: true,
} }
for _, o := range opts { for _, o := range opts {
@ -81,6 +84,15 @@ func Context(ctx context.Context) Option {
} }
} }
// HandleSignal toggles automatic installation of the signal handler that
// traps TERM, INT, and QUIT. Users of this feature to disable the signal
// handler, should control liveness of the service through the context.
func HandleSignal(b bool) Option {
return func(o *Options) {
o.Signal = b
}
}
func Server(s server.Server) Option { func Server(s server.Server) Option {
return func(o *Options) { return func(o *Options) {
o.Server = s o.Server = s

View File

@ -169,7 +169,9 @@ func (s *service) Run() error {
} }
ch := make(chan os.Signal, 1) ch := make(chan os.Signal, 1)
if s.opts.Signal {
signal.Notify(ch, syscall.SIGTERM, syscall.SIGINT, syscall.SIGQUIT) signal.Notify(ch, syscall.SIGTERM, syscall.SIGINT, syscall.SIGQUIT)
}
select { select {
// wait on kill signal // wait on kill signal