Pause VT parser for idle sessions to eliminate CPU waste
When no WebSocket client is connected, the session's readLoop still processes every byte of terminal output through the go-te VT parser (tracker.Feed), Screen.Draw grapheme segmentation, and string allocations — even though nobody is consuming the screen state. For programs like btop inside tmux that produce continuous full-screen redraws, this causes sustained CPU usage and GC pressure over hours. Fix: after a 10-second idle threshold (no client connected), skip tracker.Feed() and only maintain the replay buffer. When a client reconnects (UpdateConnector) or a screenshot is requested (GetScreenSnapshot), rebuild the tracker by replaying the buffer through a fresh VT parser instance. Changes: - Add idleSince atomic timestamp + MarkIdle() to Session interface - handleOutput() skips tracker.Feed when idle > threshold - UpdateConnector() clears idle flag and rebuilds tracker from replay - GetScreenSnapshot() rebuilds stale tracker on-demand for screenshots - Wire MarkIdle() call into handleWebSocket cleanup (client disconnect) - Add TestIdleTrackerPauseAndRebuild covering the full lifecycle
This commit is contained in:
@@ -88,6 +88,7 @@ func (b *blockingSession) SendMeta(map[string]any) bool { return true }
|
||||
func (b *blockingSession) GetReplayBuffer() []byte { return nil }
|
||||
func (b *blockingSession) ForceRedraw() error { return nil }
|
||||
func (b *blockingSession) UpdateConnector(SessionConnector) {}
|
||||
func (b *blockingSession) MarkIdle() {}
|
||||
func (b *blockingSession) GetScreenSnapshot() terminalstate.Snapshot {
|
||||
return terminalstate.Snapshot{Width: 80, Height: 24, Buffer: make([][]terminalstate.Cell, 24)}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user