getOrCreateStreamState returns the per-chat stream state, creating one atomically if it doesn't exist. The returned state has its own mutex — callers must lock state.mu for access.
(chatID uuid.UUID)
| 4704 | // state has its own mutex — callers must lock state.mu for |
| 4705 | // access. |
| 4706 | func (p *Server) getOrCreateStreamState(chatID uuid.UUID) *chatStreamState { |
| 4707 | if val, ok := p.chatStreams.Load(chatID); ok { |
| 4708 | state, _ := val.(*chatStreamState) |
| 4709 | return state |
| 4710 | } |
| 4711 | val, _ := p.chatStreams.LoadOrStore(chatID, &chatStreamState{ |
| 4712 | subscribers: make(map[uuid.UUID]chan codersdk.ChatStreamEvent), |
| 4713 | }) |
| 4714 | state, _ := val.(*chatStreamState) |
| 4715 | return state |
| 4716 | } |
| 4717 | |
| 4718 | // cleanupStreamIfIdle removes the chat entry from the sync.Map when |
| 4719 | // there are no subscribers, the stream is not buffering, and any |