mirror of
https://github.com/Finesssee/ProxyPilot.git
synced 2026-11-18 18:04:20 +00:00
fix(sdk): update stream forwarder for compression
Update stream forwarder to work with token-aware compression middleware. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 4.5
parent
f19594e490
commit
d3466b31af
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user