| 491 | } |
| 492 | |
| 493 | func (a *application) graceShutdown() { |
| 494 | var wg sync.WaitGroup |
| 495 | |
| 496 | atomic.StoreInt32(&a.isShutdowning, 1) |
| 497 | pid := os.Getpid() |
| 498 | |
| 499 | var graceShutdownTimeout time.Duration |
| 500 | if atomic.LoadInt32(&a.isShutdownByAdmin) == 1 { |
| 501 | // shutdown by admin,we should need shorten the timeout |
| 502 | graceShutdownTimeout = tools.ParseTimeOut(GracedownTimeout) |
| 503 | } else { |
| 504 | graceShutdownTimeout = a.svrCfg.GracedownTimeout |
| 505 | } |
| 506 | |
| 507 | TLOG.Infof("grace shutdown start %d in %v", pid, graceShutdownTimeout) |
| 508 | ctx, cancel := context.WithTimeout(context.Background(), graceShutdownTimeout) |
| 509 | // deregister service |
| 510 | wg.Add(1) |
| 511 | go func() { |
| 512 | defer wg.Done() |
| 513 | a.deregisterAdapters(ctx) |
| 514 | }() |
| 515 | |
| 516 | for _, obj := range a.destroyableObjs { |
| 517 | wg.Add(1) |
| 518 | go func(wg *sync.WaitGroup, obj destroyableImp) { |
| 519 | defer wg.Done() |
| 520 | obj.Destroy() |
| 521 | TLOG.Infof("grace Destroy success %d", pid) |
| 522 | }(&wg, obj) |
| 523 | } |
| 524 | |
| 525 | for _, obj := range a.objRunList { |
| 526 | if s, ok := a.httpSvrs[obj]; ok { |
| 527 | wg.Add(1) |
| 528 | go func(s *http.Server, ctx context.Context, wg *sync.WaitGroup, objstr string) { |
| 529 | defer wg.Done() |
| 530 | if err := s.Shutdown(ctx); err != nil { |
| 531 | TLOG.Errorf("grace shutdown http %s failed within %v, err%v", objstr, graceShutdownTimeout, err) |
| 532 | } else { |
| 533 | TLOG.Infof("grace shutdown http %s success %d", objstr, pid) |
| 534 | } |
| 535 | }(s, ctx, &wg, obj) |
| 536 | } |
| 537 | |
| 538 | if s, ok := a.goSvrs[obj]; ok { |
| 539 | wg.Add(1) |
| 540 | go func(s *transport.TarsServer, ctx context.Context, wg *sync.WaitGroup, objstr string) { |
| 541 | defer wg.Done() |
| 542 | if err := s.Shutdown(ctx); err != nil { |
| 543 | TLOG.Errorf("grace shutdown tars %s failed within %v, err: %v", objstr, graceShutdownTimeout, err) |
| 544 | } else { |
| 545 | TLOG.Infof("grace shutdown tars %s success %d", objstr, pid) |
| 546 | } |
| 547 | }(s, ctx, &wg, obj) |
| 548 | } |
| 549 | } |
| 550 | |