fix(app): use new context when app stop (#1589)

pull/1563/head
包子 3 years ago committed by GitHub
parent b353ab91f1
commit 70f58b264a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 5
      app.go
  2. 6
      options.go

@ -42,6 +42,7 @@ func New(opts ...Option) *App {
logger: log.NewHelper(log.DefaultLogger),
sigs: []os.Signal{syscall.SIGTERM, syscall.SIGQUIT, syscall.SIGINT},
registrarTimeout: 10 * time.Second,
stopTimeout: 10 * time.Second,
}
if id, err := uuid.NewUUID(); err == nil {
o.id = id.String()
@ -90,7 +91,9 @@ func (a *App) Run() error {
srv := srv
eg.Go(func() error {
<-ctx.Done() // wait for stop signal
return srv.Stop(ctx)
sctx, cancel := context.WithTimeout(context.Background(), a.opts.stopTimeout)
defer cancel()
return srv.Stop(sctx)
})
wg.Add(1)
eg.Go(func() error {

@ -28,6 +28,7 @@ type options struct {
logger *log.Helper
registrar registry.Registrar
registrarTimeout time.Duration
stopTimeout time.Duration
servers []transport.Server
}
@ -87,3 +88,8 @@ func Registrar(r registry.Registrar) Option {
func RegistrarTimeout(t time.Duration) Option {
return func(o *options) { o.registrarTimeout = t }
}
// StopTimeout with app stop timeout.
func StopTimeout(t time.Duration) Option {
return func(o *options) { o.stopTimeout = t }
}

Loading…
Cancel
Save