Fix SSE timeout will affected by http.Server 's WriteTimeout (#5024)

Co-authored-by: csbzy <chenshaobo65@mail.com>
Co-authored-by: Kevin Wan <wanjunfeng@gmail.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
This commit is contained in:
csbzy
2025-07-23 23:04:45 +08:00
committed by GitHub
parent b011a072c7
commit 0be63c3625
2 changed files with 11 additions and 0 deletions

View File

@@ -10,6 +10,7 @@ import (
"github.com/zeromicro/go-zero/core/codec"
"github.com/zeromicro/go-zero/core/load"
"github.com/zeromicro/go-zero/core/logx"
"github.com/zeromicro/go-zero/core/stat"
"github.com/zeromicro/go-zero/rest/chain"
"github.com/zeromicro/go-zero/rest/handler"
@@ -70,6 +71,11 @@ func buildSSERoutes(routes []Route) []Route {
for i, route := range routes {
h := route.Handler
routes[i].Handler = func(w http.ResponseWriter, r *http.Request) {
rc := http.NewResponseController(w)
err := rc.SetWriteDeadline(time.Time{})
if err != nil {
logx.Errorf("set conn write deadline failed:%v", err)
}
w.Header().Set(header.ContentType, header.ContentTypeEventStream)
w.Header().Set(header.CacheControl, header.CacheControlNoCache)
w.Header().Set(header.Connection, header.ConnectionKeepAlive)

View File

@@ -59,3 +59,8 @@ func (w *WithCodeResponseWriter) WriteHeader(code int) {
w.Writer.WriteHeader(code)
w.Code = code
}
// Unwrap returns the underlying ResponseWriter.
func (w *WithCodeResponseWriter) Unwrap() http.ResponseWriter {
return w.Writer
}