fix(handler): adjust SSE heartbeat interval and cache control header
- 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:
@@ -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
|
||||
}
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
package service
|
||||
package service
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
Reference in New Issue
Block a user