fix: ensure SSE ping packets are sent before upstream response

These changes ensures SSE ping packets are sent before receiving a response from the upstream. The previous implementation did not send ping packets until after the upstream response, rendering the feature ineffective.
This commit is contained in:
9
2025-05-07 23:27:09 +08:00
parent fd6838e690
commit 02acc52fdb
2 changed files with 80 additions and 43 deletions

View File

@@ -192,7 +192,16 @@ func TextHelper(c *gin.Context) (openaiErr *dto.OpenAIErrorWithStatusCode) {
}
var httpResp *http.Response
resp, err := adaptor.DoRequest(c, relayInfo, requestBody)
var resp any
if relayInfo.IsStream {
// Streaming requests can use SSE ping to keep alive and avoid connection timeout
// The judgment of whether ping is enabled will be made within the function
resp, err = helper.DoStreamRequestWithPinger(adaptor.DoRequest, c, relayInfo, requestBody)
} else {
resp, err = adaptor.DoRequest(c, relayInfo, requestBody)
}
if err != nil {
return service.OpenAIErrorWrapper(err, "do_request_failed", http.StatusInternalServerError)
}