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

Loading…
Cancel
Save