mirror of
https://github.com/zeromicro/go-zero.git
synced 2026-05-13 18:00:00 +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("Cache-Control", "no-cache")
|
||||||
// w.Header().Set("Connection", "keep-alive")
|
// w.Header().Set("Connection", "keep-alive")
|
||||||
client := make(chan {{.ResponseType}}, 16)
|
client := make(chan {{.ResponseType}}, 16)
|
||||||
defer func() {
|
|
||||||
close(client)
|
|
||||||
}()
|
|
||||||
l := {{.LogicName}}.New{{.LogicType}}(r.Context(), svcCtx)
|
l := {{.LogicName}}.New{{.LogicType}}(r.Context(), svcCtx)
|
||||||
threading.GoSafeCtx(r.Context(), func() {
|
threading.GoSafeCtx(r.Context(), func() {
|
||||||
|
defer close(client)
|
||||||
err := l.{{.Call}}({{if .HasRequest}}&req, {{end}}client)
|
err := l.{{.Call}}({{if .HasRequest}}&req, {{end}}client)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logc.Errorw(r.Context(), "{{.HandlerName}}", logc.Field("error", err))
|
logc.Errorw(r.Context(), "{{.HandlerName}}", logc.Field("error", err))
|
||||||
@@ -44,7 +43,10 @@ func {{.HandlerName}}(svcCtx *svc.ServiceContext) http.HandlerFunc {
|
|||||||
|
|
||||||
for {
|
for {
|
||||||
select {
|
select {
|
||||||
case data := <-client:
|
case data, ok := <-client:
|
||||||
|
if !ok {
|
||||||
|
return
|
||||||
|
}
|
||||||
output, err := json.Marshal(data)
|
output, err := json.Marshal(data)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logc.Errorw(r.Context(), "{{.HandlerName}}", logc.Field("error", err))
|
logc.Errorw(r.Context(), "{{.HandlerName}}", logc.Field("error", err))
|
||||||
|
|||||||
Reference in New Issue
Block a user