Преглед изворни кода

fix(stream): default non-positive streaming timeout

master
fengsilin пре 3 недеља
родитељ
комит
19bdae5509
2 измењених фајлова са 22 додато и 0 уклоњено
  1. +4
    -0
      relay/helper/stream_scanner.go
  2. +18
    -0
      relay/helper/stream_scanner_test.go

+ 4
- 0
relay/helper/stream_scanner.go Прегледај датотеку

@@ -24,6 +24,7 @@ import (
const (
InitialScannerBufferSize = 64 << 10 // 64KB (64*1024)
DefaultMaxScannerBufferSize = 64 << 20 // 64MB (64*1024*1024) default SSE buffer size
DefaultStreamingTimeout = 300 * time.Second
DefaultPingInterval = 10 * time.Second
)

@@ -48,6 +49,9 @@ func StreamScannerHandler(c *gin.Context, resp *http.Response, info *relaycommon
}()

streamingTimeout := time.Duration(constant.StreamingTimeout) * time.Second
if streamingTimeout <= 0 {
streamingTimeout = DefaultStreamingTimeout
}

var (
stopChan = make(chan bool, 3) // 增加缓冲区避免阻塞


+ 18
- 0
relay/helper/stream_scanner_test.go Прегледај датотеку

@@ -97,6 +97,24 @@ func TestStreamScannerHandler_EmptyBody(t *testing.T) {
assert.False(t, called.Load(), "handler should not be called for empty body")
}

func TestStreamScannerHandler_NonPositiveStreamingTimeoutDoesNotPanic(t *testing.T) {
oldTimeout := constant.StreamingTimeout
constant.StreamingTimeout = 0
t.Cleanup(func() {
constant.StreamingTimeout = oldTimeout
})

recorder := httptest.NewRecorder()
c, _ := gin.CreateTestContext(recorder)
c.Request = httptest.NewRequest(http.MethodPost, "/v1/chat/completions", nil)
resp := &http.Response{Body: io.NopCloser(strings.NewReader(""))}
info := &relaycommon.RelayInfo{ChannelMeta: &relaycommon.ChannelMeta{}}

require.NotPanics(t, func() {
StreamScannerHandler(c, resp, info, func(data string) bool { return true })
})
}

func TestStreamScannerHandler_1000Chunks(t *testing.T) {
t.Parallel()



Loading…
Откажи
Сачувај