Skip to content

Commit

Permalink
perf: reuse http client to reduce delay
Browse files Browse the repository at this point in the history
  • Loading branch information
songquanpeng committed Jul 23, 2023
1 parent dccd66b commit 3da119e
Show file tree
Hide file tree
Showing 5 changed files with 11 additions and 9 deletions.
3 changes: 1 addition & 2 deletions controller/channel-billing.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,15 +85,14 @@ func GetAuthHeader(token string) http.Header {
}

func GetResponseBody(method, url string, channel *model.Channel, headers http.Header) ([]byte, error) {
client := &http.Client{}
req, err := http.NewRequest(method, url, nil)
if err != nil {
return nil, err
}
for k := range headers {
req.Header.Add(k, headers.Get(k))
}
res, err := client.Do(req)
res, err := httpClient.Do(req)
if err != nil {
return nil, err
}
Expand Down
3 changes: 1 addition & 2 deletions controller/channel-test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,7 @@ func testChannel(channel *model.Channel, request ChatRequest) (error, *OpenAIErr
req.Header.Set("Authorization", "Bearer "+channel.Key)
}
req.Header.Set("Content-Type", "application/json")
client := &http.Client{}
resp, err := client.Do(req)
resp, err := httpClient.Do(req)
if err != nil {
return err, nil
}
Expand Down
3 changes: 1 addition & 2 deletions controller/relay-image.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,8 +109,7 @@ func relayImageHelper(c *gin.Context, relayMode int) *OpenAIErrorWithStatusCode
req.Header.Set("Content-Type", c.Request.Header.Get("Content-Type"))
req.Header.Set("Accept", c.Request.Header.Get("Accept"))

client := &http.Client{}
resp, err := client.Do(req)
resp, err := httpClient.Do(req)
if err != nil {
return errorWrapper(err, "do_request_failed", http.StatusInternalServerError)
}
Expand Down
2 changes: 1 addition & 1 deletion controller/relay-openai.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ func openaiHandler(c *gin.Context, resp *http.Response, consumeQuota bool) (*Ope
}
// We shouldn't set the header before we parse the response body, because the parse part may fail.
// And then we will have to send an error response, but in this case, the header has already been set.
// So the client will be confused by the response.
// So the httpClient will be confused by the response.
// For example, Postman will report error, and we cannot check the response at all.
for k, v := range resp.Header {
c.Writer.Header().Set(k, v[0])
Expand Down
9 changes: 7 additions & 2 deletions controller/relay-text.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,12 @@ const (
APITypeZhipu
)

var httpClient *http.Client

func init() {
httpClient = &http.Client{}
}

func relayTextHelper(c *gin.Context, relayMode int) *OpenAIErrorWithStatusCode {
channelType := c.GetInt("channel")
tokenId := c.GetInt("token_id")
Expand Down Expand Up @@ -244,8 +250,7 @@ func relayTextHelper(c *gin.Context, relayMode int) *OpenAIErrorWithStatusCode {
req.Header.Set("Content-Type", c.Request.Header.Get("Content-Type"))
req.Header.Set("Accept", c.Request.Header.Get("Accept"))
//req.Header.Set("Connection", c.Request.Header.Get("Connection"))
client := &http.Client{}
resp, err := client.Do(req)
resp, err := httpClient.Do(req)
if err != nil {
return errorWrapper(err, "do_request_failed", http.StatusInternalServerError)
}
Expand Down

0 comments on commit 3da119e

Please sign in to comment.