fix: starter parent ctx (#1895)

* fix: starter parent ctx

* fix stop ctx
pull/1900/head
Tony Chen 3 years ago committed by GitHub
parent e66a2905ab
commit 7a5c2207a1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 17
      app.go

@ -84,27 +84,25 @@ func (a *App) Run() error {
if err != nil {
return err
}
ctx := NewContext(a.ctx, a)
eg, ctx := errgroup.WithContext(ctx)
eg, ctx := errgroup.WithContext(NewContext(a.ctx, a))
wg := sync.WaitGroup{}
for _, srv := range a.opts.servers {
srv := srv
eg.Go(func() error {
<-ctx.Done() // wait for stop signal
sctx, cancel := context.WithTimeout(NewContext(context.Background(), a), a.opts.stopTimeout)
stopCtx, cancel := context.WithTimeout(NewContext(a.opts.ctx, a), a.opts.stopTimeout)
defer cancel()
return srv.Stop(sctx)
return srv.Stop(stopCtx)
})
wg.Add(1)
eg.Go(func() error {
wg.Done()
tctx := NewContext(context.Background(), a)
return srv.Start(tctx)
return srv.Start(NewContext(a.opts.ctx, a))
})
}
wg.Wait()
if a.opts.registrar != nil {
rctx, rcancel := context.WithTimeout(a.opts.ctx, a.opts.registrarTimeout)
rctx, rcancel := context.WithTimeout(ctx, a.opts.registrarTimeout)
defer rcancel()
if err := a.opts.registrar.Register(rctx, instance); err != nil {
return err
@ -121,8 +119,7 @@ func (a *App) Run() error {
case <-ctx.Done():
return ctx.Err()
case <-c:
err := a.Stop()
if err != nil {
if err := a.Stop(); err != nil {
a.opts.logger.Errorf("failed to stop app: %v", err)
return err
}
@ -141,7 +138,7 @@ func (a *App) Stop() error {
instance := a.instance
a.lk.Unlock()
if a.opts.registrar != nil && instance != nil {
ctx, cancel := context.WithTimeout(a.opts.ctx, a.opts.registrarTimeout)
ctx, cancel := context.WithTimeout(NewContext(a.ctx, a), a.opts.registrarTimeout)
defer cancel()
if err := a.opts.registrar.Deregister(ctx, instance); err != nil {
return err

Loading…
Cancel
Save