mirror of
https://github.com/go-kratos/kratos.git
synced 2024-12-30 21:19:57 +02:00
fix(app): use new context when app stop (#1589)
This commit is contained in:
parent
b353ab91f1
commit
70f58b264a
5
app.go
5
app.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…
Reference in New Issue
Block a user