Skip to content

Commit

Permalink
fix: starter parent ctx (#1895)
Browse files Browse the repository at this point in the history
* fix: starter parent ctx

* fix stop ctx
  • Loading branch information
tonybase committed Mar 18, 2022
1 parent e66a290 commit 7a5c220
Showing 1 changed file with 7 additions and 10 deletions.
17 changes: 7 additions & 10 deletions app.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
}
Expand All @@ -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
Expand Down

0 comments on commit 7a5c220

Please sign in to comment.