cleanupStreamIfIdle removes the chat entry from the sync.Map when there are no subscribers, the stream is not buffering, and any grace period for late-connecting relay subscribers has elapsed. If the grace window is still open it returns without rescheduling. streamJanitorLoop is the backstop that r
(chatID uuid.UUID, state *chatStreamState)
| 4727 | // installed by a racing getOrCreateStreamState. Returns true |
| 4728 | // if the state was deleted, false otherwise. |
| 4729 | func (p *Server) cleanupStreamIfIdle(chatID uuid.UUID, state *chatStreamState) bool { |
| 4730 | if state.buffering || len(state.subscribers) > 0 { |
| 4731 | return false |
| 4732 | } |
| 4733 | // Keep stream state alive during the grace period so |
| 4734 | // late-connecting cross-replica relay subscribers can |
| 4735 | // register against this chat before GC. |
| 4736 | if !state.bufferRetainedAt.IsZero() && |
| 4737 | p.clock.Now().Before(state.bufferRetainedAt.Add(bufferRetainGracePeriod)) { |
| 4738 | return false |
| 4739 | } |
| 4740 | if !p.chatStreams.CompareAndDelete(chatID, state) { |
| 4741 | return false |
| 4742 | } |
| 4743 | p.workspaceMCPToolsCache.Delete(chatID) |
| 4744 | return true |
| 4745 | } |
| 4746 | |
| 4747 | // streamJanitorLoop periodically reaps idle chat stream states whose |
| 4748 | // grace period has expired. It is the backstop for the grace-window |