mirror of
https://github.com/zeromicro/go-zero.git
synced 2026-05-07 15:10:01 +08:00
fix: SSE handler blocking (#5181)
Co-authored-by: Kevin Wan <wanjunfeng@gmail.com> Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
This commit is contained in:
@@ -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))
|
||||
|
||||
Reference in New Issue
Block a user