Skip to content

Commit

Permalink
fix SSE
Browse files Browse the repository at this point in the history
  • Loading branch information
luke-lombardi committed Dec 27, 2024
1 parent 6eec9fc commit 0f8b8b2
Showing 1 changed file with 28 additions and 5 deletions.
33 changes: 28 additions & 5 deletions pkg/abstractions/endpoint/buffer.go
Original file line number Diff line number Diff line change
Expand Up @@ -494,11 +494,34 @@ func (rb *RequestBuffer) handleHttpRequest(req *request, c container) {
}
req.ctx.Response().WriteHeader(resp.StatusCode)

_, err = io.Copy(req.ctx.Response().Writer, resp.Body)
if err != nil {
req.ctx.JSON(http.StatusInternalServerError, map[string]interface{}{
"error": "Internal server error",
})
// Check if we can stream the response
streamingSupported := true
flusher, ok := req.ctx.Response().Writer.(http.Flusher)
if !ok {
streamingSupported = false
}

// Send response to client in chunks
buf := make([]byte, 4096)
for {
n, err := resp.Body.Read(buf)
if n > 0 {
req.ctx.Response().Writer.Write(buf[:n])

if streamingSupported {
flusher.Flush()
}
}

if err != nil {
if err != io.EOF {
req.ctx.JSON(http.StatusInternalServerError, map[string]interface{}{
"error": "Internal server error",
})
}

break
}
}
}

Expand Down

0 comments on commit 0f8b8b2

Please sign in to comment.