1
0
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:
包子 2021-10-28 11:09:17 +08:00 committed by GitHub
parent b353ab91f1
commit 70f58b264a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 10 additions and 1 deletions

5
app.go
View File

@ -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 {

View File

@ -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 }
}