From a1c6cdf193993f0d9d922f9275f1a4237535891a Mon Sep 17 00:00:00 2001 From: Erik Hollensbe Date: Sat, 16 Nov 2019 03:13:34 -0800 Subject: [PATCH] Now specify HandleSignal as an option to toggle signal handling. (#948) Signed-off-by: Erik Hollensbe --- options.go | 12 ++++++++++++ service.go | 4 +++- 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/options.go b/options.go index d566a353..aa90c88f 100644 --- a/options.go +++ b/options.go @@ -31,6 +31,8 @@ type Options struct { // Other options for implementations of the interface // can be stored in a context Context context.Context + + Signal bool } func newOptions(opts ...Option) Options { @@ -42,6 +44,7 @@ func newOptions(opts ...Option) Options { Registry: registry.DefaultRegistry, Transport: transport.DefaultTransport, Context: context.Background(), + Signal: true, } 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 { return func(o *Options) { o.Server = s diff --git a/service.go b/service.go index f2d9fea8..2c94387e 100644 --- a/service.go +++ b/service.go @@ -169,7 +169,9 @@ func (s *service) Run() error { } ch := make(chan os.Signal, 1) - signal.Notify(ch, syscall.SIGTERM, syscall.SIGINT, syscall.SIGQUIT) + if s.opts.Signal { + signal.Notify(ch, syscall.SIGTERM, syscall.SIGINT, syscall.SIGQUIT) + } select { // wait on kill signal