Skip to content

Commit

Permalink
Signal handling try micro#2: through the context
Browse files Browse the repository at this point in the history
This approach expects the user to control their context to manage the
service. A signal handler is installed by default by you can specify the
option `SignalHandler()` to turn it off.

The choice of keeping the signal handler in was intended to preserve
backwards compatibility so that users of go-micro today were not
surprised by the signal handling changes.

Signed-off-by: Erik Hollensbe <github@hollensbe.org>
  • Loading branch information
Erik Hollensbe committed Nov 16, 2019
1 parent 383658e commit 70633f1
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 8 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ require (
github.com/mitchellh/hashstructure v1.0.0
github.com/nats-io/nats.go v1.9.1
github.com/nlopes/slack v0.6.0
github.com/oxtoacart/bpool v0.0.0-20190530202638-03653db5a59c
github.com/pkg/errors v0.8.1
github.com/technoweenie/multipartstreamer v1.0.1 // indirect
go.uber.org/zap v1.12.0 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,8 @@ github.com/opencontainers/runtime-tools v0.0.0-20181011054405-1d69bd0f9c39/go.mo
github.com/openzipkin/zipkin-go v0.1.6/go.mod h1:QgAqvLzwWbR/WpD4A3cGpPtJrZXNIiJc5AZX7/PBEpw=
github.com/oracle/oci-go-sdk v7.0.0+incompatible/go.mod h1:VQb79nF8Z2cwLkLS35ukwStZIg5F66tcBccjip/j888=
github.com/ovh/go-ovh v0.0.0-20181109152953-ba5adb4cf014/go.mod h1:joRatxRJaZBsY3JAOEMcoOp05CnZzsx4scTxi95DHyQ=
github.com/oxtoacart/bpool v0.0.0-20190530202638-03653db5a59c h1:rp5dCmg/yLR3mgFuSOe4oEnDDmGLROTvMragMUXpTQw=
github.com/oxtoacart/bpool v0.0.0-20190530202638-03653db5a59c/go.mod h1:X07ZCGwUbLaax7L0S3Tw4hpejzu63ZrrQiUe6W0hcy0=
github.com/pelletier/go-buffruneio v0.2.0/go.mod h1:JkE26KsDizTr40EUHkXVtNPvgGtbSNq5BcowyYOWdKo=
github.com/pierrec/lz4 v2.0.5+incompatible/go.mod h1:pdkljMzZIN41W+lC3N2tnIh5sFi+IEE17M5jbnwPHcY=
github.com/pkg/errors v0.8.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
Expand Down
11 changes: 11 additions & 0 deletions options.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ type Options struct {
// Other options for implementations of the interface
// can be stored in a context
Context context.Context

DisableSignalHandler bool
}

func newOptions(opts ...Option) Options {
Expand Down Expand Up @@ -81,6 +83,15 @@ func Context(ctx context.Context) Option {
}
}

// SignalHandler toggles automatic installation of the signal handler.
// Users of this feature to disable the signal handler, should control liveness
// of the service through the context.
func SignalHandler(b bool) Option {
return func(o *Options) {
o.DisableSignalHandler = !b
}
}

func Server(s server.Server) Option {
return func(o *Options) {
o.Server = s
Expand Down
19 changes: 11 additions & 8 deletions service.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,14 +168,17 @@ func (s *service) Run() error {
return err
}

ch := make(chan os.Signal, 1)
signal.Notify(ch, syscall.SIGTERM, syscall.SIGINT, syscall.SIGQUIT)

select {
// wait on kill signal
case <-ch:
// wait on context cancel
case <-s.opts.Context.Done():
if s.opts.DisableSignalHandler {
<-s.opts.Context.Done()
} else {
ch := make(chan os.Signal, 1)
signal.Notify(ch, syscall.SIGTERM, syscall.SIGINT, syscall.SIGQUIT)
select {
// wait on kill signal
case <-ch:
// wait on context cancel
case <-s.opts.Context.Done():
}
}

return s.Stop()
Expand Down

0 comments on commit 70633f1

Please sign in to comment.