Skip to content

Commit

Permalink
fix: fix getAndValidateTextRequest failed: unexpected end of JSON inp…
Browse files Browse the repository at this point in the history
…ut (close #1043)
  • Loading branch information
songquanpeng committed Feb 26, 2024
1 parent 6b27d66 commit b747cdb
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 6 deletions.
18 changes: 15 additions & 3 deletions common/gin.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,24 @@ import (
"strings"
)

func UnmarshalBodyReusable(c *gin.Context, v any) error {
const KeyRequestBody = "key_request_body"

func GetRequestBody(c *gin.Context) ([]byte, error) {
requestBody, _ := c.Get(KeyRequestBody)
if requestBody != nil {
return requestBody.([]byte), nil
}
requestBody, err := io.ReadAll(c.Request.Body)
if err != nil {
return err
return nil, err
}
err = c.Request.Body.Close()
_ = c.Request.Body.Close()
c.Set(KeyRequestBody, requestBody)
return requestBody.([]byte), nil
}

func UnmarshalBodyReusable(c *gin.Context, v any) error {
requestBody, err := GetRequestBody(c)
if err != nil {
return err
}
Expand Down
14 changes: 11 additions & 3 deletions controller/relay.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package controller

import (
"bytes"
"context"
"fmt"
"github.com/gin-gonic/gin"
"github.com/songquanpeng/one-api/common"
"github.com/songquanpeng/one-api/common/config"
"github.com/songquanpeng/one-api/common/helper"
"github.com/songquanpeng/one-api/common/logger"
Expand All @@ -13,6 +15,7 @@ import (
"github.com/songquanpeng/one-api/relay/controller"
"github.com/songquanpeng/one-api/relay/model"
"github.com/songquanpeng/one-api/relay/util"
"io"
"net/http"
)

Expand Down Expand Up @@ -50,8 +53,8 @@ func Relay(c *gin.Context) {
go processChannelRelayError(ctx, channelId, channelName, bizErr)
requestId := c.GetString(logger.RequestIdKey)
retryTimes := config.RetryTimes
if !shouldRetry(bizErr.StatusCode) {
logger.Errorf(ctx, "relay error happen, but status code is %d, won't retry in this case", bizErr.StatusCode)
if !shouldRetry(c, bizErr.StatusCode) {
logger.Errorf(ctx, "relay error happen, status code is %d, won't retry in this case", bizErr.StatusCode)
retryTimes = 0
}
for i := retryTimes; i > 0; i-- {
Expand All @@ -65,6 +68,8 @@ func Relay(c *gin.Context) {
continue
}
middleware.SetupContextForSelectedChannel(c, channel, originalModel)
requestBody, err := common.GetRequestBody(c)
c.Request.Body = io.NopCloser(bytes.NewBuffer(requestBody))
bizErr = relay(c, relayMode)
if bizErr == nil {
return
Expand All @@ -85,7 +90,10 @@ func Relay(c *gin.Context) {
}
}

func shouldRetry(statusCode int) bool {
func shouldRetry(c *gin.Context, statusCode int) bool {
if _, ok := c.Get("specific_channel_id"); ok {
return false
}
if statusCode == http.StatusTooManyRequests {
return true
}
Expand Down

0 comments on commit b747cdb

Please sign in to comment.