SetupSSE configures the response headers and starts the writer goroutine
()
| 95 | |
| 96 | // SetupSSE configures the response headers and starts the writer goroutine |
| 97 | func (h *SSEHandlerCh) SetupSSE() error { |
| 98 | h.lock.Lock() |
| 99 | defer h.lock.Unlock() |
| 100 | |
| 101 | if h.closed { |
| 102 | return fmt.Errorf("SSE handler is closed") |
| 103 | } |
| 104 | |
| 105 | h.initialized = true |
| 106 | |
| 107 | // Reset write deadline for streaming |
| 108 | if err := h.rc.SetWriteDeadline(time.Time{}); err != nil { |
| 109 | return fmt.Errorf("failed to reset write deadline: %v", err) |
| 110 | } |
| 111 | |
| 112 | // Set SSE headers |
| 113 | h.w.Header().Set("Content-Type", SSEContentType) |
| 114 | h.w.Header().Set("Cache-Control", "no-cache, no-store, must-revalidate, no-transform") |
| 115 | h.w.Header().Set("Connection", SSEConnection) |
| 116 | h.w.Header().Set("x-vercel-ai-ui-message-stream", "v1") |
| 117 | h.w.Header().Set("X-Accel-Buffering", "no") |
| 118 | |
| 119 | // Send headers and establish streaming |
| 120 | h.w.WriteHeader(http.StatusOK) |
| 121 | fmt.Fprint(h.w, SSEStreamStartMsg) |
| 122 | if err := h.flush(); err != nil { |
| 123 | return err |
| 124 | } |
| 125 | |
| 126 | // Start the writer goroutine |
| 127 | h.wg.Add(1) |
| 128 | go h.writerLoop() |
| 129 | |
| 130 | return nil |
| 131 | } |
| 132 | |
| 133 | // writerLoop handles all writes and keepalives in a single goroutine |
| 134 | func (h *SSEHandlerCh) writerLoop() { |
no test coverage detected