From 988fb9d9bfdd4123ba20ca83a79c370c9220d554 Mon Sep 17 00:00:00 2001 From: Remember <36129334+wuqinqiang@users.noreply.github.com> Date: Fri, 26 Sep 2025 21:53:42 +0800 Subject: [PATCH] fix: SSE handler blocking (#5181) Co-authored-by: Kevin Wan Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> --- tools/goctl/api/gogen/sse_handler.tpl | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/tools/goctl/api/gogen/sse_handler.tpl b/tools/goctl/api/gogen/sse_handler.tpl index 722167095..84aea28b8 100644 --- a/tools/goctl/api/gogen/sse_handler.tpl +++ b/tools/goctl/api/gogen/sse_handler.tpl @@ -30,11 +30,10 @@ func {{.HandlerName}}(svcCtx *svc.ServiceContext) http.HandlerFunc { // w.Header().Set("Cache-Control", "no-cache") // w.Header().Set("Connection", "keep-alive") client := make(chan {{.ResponseType}}, 16) - defer func() { - close(client) - }() + l := {{.LogicName}}.New{{.LogicType}}(r.Context(), svcCtx) threading.GoSafeCtx(r.Context(), func() { + defer close(client) err := l.{{.Call}}({{if .HasRequest}}&req, {{end}}client) if err != nil { logc.Errorw(r.Context(), "{{.HandlerName}}", logc.Field("error", err)) @@ -44,7 +43,10 @@ func {{.HandlerName}}(svcCtx *svc.ServiceContext) http.HandlerFunc { for { select { - case data := <-client: + case data, ok := <-client: + if !ok { + return + } output, err := json.Marshal(data) if err != nil { logc.Errorw(r.Context(), "{{.HandlerName}}", logc.Field("error", err))