@@ -92,8 +92,31 @@ type Controller struct {
9292 config Config
9393 conn WebsocketConnection
9494
95- // data channel which data providers write messages to.
96- // writer routine reads from this channel and writes messages to connection
95+ // The `multiplexedStream` is a core channel used for communication between the
96+ // `Controller` and Data Providers. Its lifecycle is as follows:
97+ //
98+ // 1. **Data Providers**:
99+ // - Data providers write their data into this channel, which is consumed by
100+ // the writer routine to send messages to the client.
101+ // 2. **Reader Routine**:
102+ // - Writes OK/error responses to the channel as a result of processing client messages.
103+ // 3. **Writer Routine**:
104+ // - Reads messages from this channel and forwards them to the client WebSocket connection.
105+ //
106+ // 4. **Channel Closing**:
107+ // - The `Controller` is responsible for starting and managing the lifecycle of the channel.
108+ // - If an unrecoverable error occurs in any of the three routines (reader, writer, or keepalive),
109+ // the parent context is canceled. This triggers data providers to stop their work.
110+ // - The `multiplexedStream` will not be closed until all data providers signal that
111+ // they have stopped writing to it via the `dataProvidersGroup` wait group.
112+ //
113+ // 5. **Edge Case - Writer Routine Finished Before Providers**:
114+ // - If the writer routine finishes before all data providers, a separate draining routine
115+ // ensures that the `multiplexedStream` is fully drained to prevent deadlocks.
116+ // All remaining messages in this case will be discarded.
117+ //
118+ // This design ensures that the channel is only closed when it is safe to do so, avoiding
119+ // issues such as sending on a closed channel while maintaining proper cleanup.
97120 multiplexedStream chan interface {}
98121
99122 dataProviders * concurrentmap.Map [uuid.UUID , dp.DataProvider ]
0 commit comments