Skip to content

Commit

Permalink
fix: do not save same response body and content
Browse files Browse the repository at this point in the history
  • Loading branch information
zijiren233 committed Jan 3, 2025
1 parent da0bcdc commit ef39d8d
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 15 deletions.
18 changes: 3 additions & 15 deletions service/aiproxy/relay/controller/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"bytes"
"context"
"errors"
"io"
"net/http"
"sync"
"time"
Expand Down Expand Up @@ -256,10 +255,9 @@ func DoHelper(

// 3. Handle error response
if isErrorHappened(resp) {
if err := handleErrorResponse(resp, &detail); err != nil {
return nil, &detail, err
}
return nil, &detail, utils.RelayErrorHandler(meta, resp)
relayErr := utils.RelayErrorHandler(meta, resp)
// detail.ResponseBody = relayErr.JSON()
return nil, &detail, relayErr
}

// 4. Handle success response
Expand Down Expand Up @@ -348,16 +346,6 @@ func doRequest(a adaptor.Adaptor, c *gin.Context, meta *meta.Meta, req *http.Req
return resp, nil
}

func handleErrorResponse(resp *http.Response, detail *model.RequestDetail) *relaymodel.ErrorWithStatusCode {
respBody, err := io.ReadAll(resp.Body)
if err != nil {
return openai.ErrorWrapperWithMessage("read response body failed: "+err.Error(), "read_response_body_failed", http.StatusBadRequest)
}
detail.ResponseBody = conv.BytesToString(respBody)
resp.Body = io.NopCloser(bytes.NewReader(respBody))
return nil
}

func handleSuccessResponse(a adaptor.Adaptor, c *gin.Context, meta *meta.Meta, resp *http.Response, detail *model.RequestDetail) (*relaymodel.Usage, *relaymodel.ErrorWithStatusCode) {
buf := getBuffer()
defer putBuffer(buf)
Expand Down
8 changes: 8 additions & 0 deletions service/aiproxy/relay/model/misc.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,11 @@ type ErrorWithStatusCode struct {
Error Error `json:"error"`
StatusCode int `json:"-"`
}

func (e *ErrorWithStatusCode) JSON() string {
jsonBuf, err := json.Marshal(e)
if err != nil {
return ""
}
return conv.BytesToString(jsonBuf)
}

0 comments on commit ef39d8d

Please sign in to comment.