ShutdownWithTimeout 带超时的关闭
(timeout time.Duration)
| 381 | |
| 382 | // ShutdownWithTimeout 带超时的关闭 |
| 383 | func (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 全局消息分发器 |
| 419 | func (s *System) dispatcher() { |