diff --git a/sdk/api/handlers/stream_forwarder.go b/sdk/api/handlers/stream_forwarder.go index 401baca8..4a16074c 100644 --- a/sdk/api/handlers/stream_forwarder.go +++ b/sdk/api/handlers/stream_forwarder.go @@ -13,6 +13,10 @@ type StreamForwardOptions struct { // If nil, the configured default is used. If set to <= 0, keep-alives are disabled. KeepAliveInterval *time.Duration + // MaxChunkSize overrides the configured maximum chunk size. + // If nil, the configured default is used. If set to <= 0, chunk splitting is disabled. + MaxChunkSize *int + // WriteChunk writes a single data chunk to the response body. It should not flush. WriteChunk func(chunk []byte) @@ -61,6 +65,12 @@ func (h *BaseAPIHandler) ForwardStream(c *gin.Context, flusher http.Flusher, can keepAliveC = keepAlive.C } + // Determine max chunk size for splitting large payloads + maxChunkSize := StreamingMaxChunkSize(h.Cfg) + if opts.MaxChunkSize != nil { + maxChunkSize = *opts.MaxChunkSize + } + var terminalErr *interfaces.ErrorMessage for { select { @@ -94,8 +104,20 @@ func (h *BaseAPIHandler) ForwardStream(c *gin.Context, flusher http.Flusher, can cancel(nil) return } - writeChunk(chunk) - flusher.Flush() + // Split large chunks to reduce latency (like vibeproxy's 64KB optimization) + if maxChunkSize > 0 && len(chunk) > maxChunkSize { + for offset := 0; offset < len(chunk); offset += maxChunkSize { + end := offset + maxChunkSize + if end > len(chunk) { + end = len(chunk) + } + writeChunk(chunk[offset:end]) + flusher.Flush() + } + } else { + writeChunk(chunk) + flusher.Flush() + } case errMsg, ok := <-errs: if !ok { continue