From 0f8b8b2c9dfa972883dba9007d071a36eb8fff95 Mon Sep 17 00:00:00 2001 From: luke-lombardi <33990301+luke-lombardi@users.noreply.github.com> Date: Thu, 26 Dec 2024 21:02:38 -0500 Subject: [PATCH] fix SSE --- pkg/abstractions/endpoint/buffer.go | 33 ++++++++++++++++++++++++----- 1 file changed, 28 insertions(+), 5 deletions(-) diff --git a/pkg/abstractions/endpoint/buffer.go b/pkg/abstractions/endpoint/buffer.go index a0668d4eb..7d4de20c4 100644 --- a/pkg/abstractions/endpoint/buffer.go +++ b/pkg/abstractions/endpoint/buffer.go @@ -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 + } } }