MCPcopy Create free account
hub / github.com/astercloud/aster / ShutdownWithTimeout

Method ShutdownWithTimeout

pkg/actor/system.go:383–416  ·  view source on GitHub ↗

ShutdownWithTimeout 带超时的关闭

(timeout time.Duration)

Source from the content-addressed store, hash-verified

381
382// ShutdownWithTimeout 带超时的关闭
383func (s *System) ShutdownWithTimeout(timeout time.Duration) {
384 actorLog.Info(context.Background(), "system shutting down", map[string]any{"name": s.name})
385
386 s.isRunning.Store(false)
387
388 // 停止所有 Actor
389 s.actorsMu.RLock()
390 pids := make([]*PID, 0, len(s.actors))
391 for _, cell := range s.actors {
392 pids = append(pids, cell.pid)
393 }
394 s.actorsMu.RUnlock()
395
396 for _, pid := range pids {
397 s.Stop(pid)
398 }
399
400 // 取消上下文
401 s.cancel()
402
403 // 等待所有 goroutine 完成
404 done := make(chan struct{})
405 go func() {
406 s.wg.Wait()
407 close(done)
408 }()
409
410 select {
411 case <-done:
412 actorLog.Info(context.Background(), "system shutdown complete", map[string]any{"name": s.name})
413 case <-time.After(timeout):
414 actorLog.Warn(context.Background(), "system shutdown timeout, forcing exit", map[string]any{"name": s.name})
415 }
416}
417
418// dispatcher 全局消息分发器
419func (s *System) dispatcher() {

Callers 1

ShutdownMethod · 0.95

Calls 5

StopMethod · 0.95
WaitMethod · 0.80
InfoMethod · 0.65
WarnMethod · 0.65
cancelMethod · 0.45

Tested by

no test coverage detected