CC Stream 响应流中出现 error 时, 增加返回重试 (#86)
* 响应流中出现 error, 返回重试 * 响应流中出现 error, 返回重试
This commit is contained in:
@@ -216,7 +216,7 @@ func (h *GatewayHandler) Messages(c *gin.Context) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const maxAccountSwitches = 3
|
const maxAccountSwitches = 10
|
||||||
switchCount := 0
|
switchCount := 0
|
||||||
failedAccountIDs := make(map[int64]struct{})
|
failedAccountIDs := make(map[int64]struct{})
|
||||||
lastFailoverStatus := 0
|
lastFailoverStatus := 0
|
||||||
|
|||||||
@@ -695,6 +695,11 @@ func (s *GatewayService) Forward(ctx context.Context, c *gin.Context, account *A
|
|||||||
if req.Stream {
|
if req.Stream {
|
||||||
streamResult, err := s.handleStreamingResponse(ctx, resp, c, account, startTime, originalModel, req.Model)
|
streamResult, err := s.handleStreamingResponse(ctx, resp, c, account, startTime, originalModel, req.Model)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
if err.Error() == "have error in stream" {
|
||||||
|
return nil, &UpstreamFailoverError{
|
||||||
|
StatusCode: 403,
|
||||||
|
}
|
||||||
|
}
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
usage = streamResult.usage
|
usage = streamResult.usage
|
||||||
@@ -969,6 +974,9 @@ func (s *GatewayService) handleStreamingResponse(ctx context.Context, resp *http
|
|||||||
|
|
||||||
for scanner.Scan() {
|
for scanner.Scan() {
|
||||||
line := scanner.Text()
|
line := scanner.Text()
|
||||||
|
if line == "event: error" {
|
||||||
|
return nil, errors.New("have error in stream")
|
||||||
|
}
|
||||||
|
|
||||||
// Extract data from SSE line (supports both "data: " and "data:" formats)
|
// Extract data from SSE line (supports both "data: " and "data:" formats)
|
||||||
if sseDataRe.MatchString(line) {
|
if sseDataRe.MatchString(line) {
|
||||||
|
|||||||
Reference in New Issue
Block a user