fix(handler): adjust SSE heartbeat interval and cache control header
All checks were successful
Build Backend / build (push) Successful in 14m55s
Build Backend / build-docker (push) Successful in 1m0s

- Updated Cache-Control header to include 'no-transform'.
- Reduced heartbeat interval from 25 seconds to 10 seconds to accommodate common proxy idle timeouts.
- Added comments for clarity on keepalive handling for SSE intermediaries and proxies.
This commit is contained in:
lafay
2026-03-24 04:23:23 +08:00
parent ab4ee30690
commit b41567a39a
2 changed files with 9 additions and 3 deletions

View File

@@ -65,7 +65,7 @@ func (h *MessageHandler) HandleSSE(c *gin.Context) {
}
w.Header().Set("Content-Type", "text/event-stream")
w.Header().Set("Cache-Control", "no-cache")
w.Header().Set("Cache-Control", "no-cache, no-transform")
w.Header().Set("Connection", "keep-alive")
w.Header().Set("X-Accel-Buffering", "no")
c.Status(http.StatusOK)
@@ -89,7 +89,8 @@ func (h *MessageHandler) HandleSSE(c *gin.Context) {
}
}
heartbeat := time.NewTicker(25 * time.Second)
// Use a shorter heartbeat interval to survive common 50-60s proxy idle timeout.
heartbeat := time.NewTicker(10 * time.Second)
defer heartbeat.Stop()
for {
@@ -101,6 +102,11 @@ func (h *MessageHandler) HandleSSE(c *gin.Context) {
return
}
case <-heartbeat.C:
// Comment keepalive line for SSE intermediaries/proxies.
if _, err := fmt.Fprint(w, ": keepalive\n\n"); err != nil {
return
}
flusher.Flush()
if _, err := fmt.Fprint(w, "event: heartbeat\ndata: {}\n\n"); err != nil {
return
}

View File

@@ -1,4 +1,4 @@
package service
package service
import (
"context"