Shutdown gracefully shuts down the session manager and all active sessions. This should be called when the application is shutting down to ensure proper cleanup of resources.
()
| 622 | // This should be called when the application is shutting down to ensure |
| 623 | // proper cleanup of resources. |
| 624 | func (sm *SessionManager) Shutdown() error { |
| 625 | sm.mutex.Lock() |
| 626 | defer sm.mutex.Unlock() |
| 627 | |
| 628 | if sm.logger != nil { |
| 629 | sm.logger.Info("Shutting down VNC Session Manager: active_sessions=%d", len(sm.sessions)) |
| 630 | } |
| 631 | |
| 632 | // Stop the cleanup process |
| 633 | if sm.cleanupTicker != nil { |
| 634 | sm.cleanupTicker.Stop() |
| 635 | } |
| 636 | |
| 637 | // Cancel the manager context |
| 638 | if sm.cancelFunc != nil { |
| 639 | sm.cancelFunc() |
| 640 | } |
| 641 | |
| 642 | // Shut down all active sessions |
| 643 | var shutdownErrors []error |
| 644 | |
| 645 | for sessionID, session := range sm.sessions { |
| 646 | if err := session.Shutdown(); err != nil { |
| 647 | shutdownErrors = append(shutdownErrors, fmt.Errorf("failed to shutdown session %s: %w", sessionID, err)) |
| 648 | } |
| 649 | } |
| 650 | |
| 651 | // Clear sessions and ports |
| 652 | sm.sessions = make(map[string]*VNCSession) |
| 653 | sm.usedPorts = make(map[int]bool) |
| 654 | |
| 655 | // Return any shutdown errors |
| 656 | if len(shutdownErrors) > 0 { |
| 657 | return fmt.Errorf("encountered %d errors during shutdown: %v", len(shutdownErrors), shutdownErrors) |
| 658 | } |
| 659 | |
| 660 | return nil |
| 661 | } |
| 662 | |
| 663 | // findReusableSession finds an existing session that can be reused for the given target. |
| 664 | // This method must be called with the manager mutex held. |