Skip to content
Toggle navigation
P
Projects
G
Groups
S
Snippets
Help
phsl
/
new-api
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Pipelines
Wiki
Snippets
Members
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit
a989fef5
authored
Jul 23, 2023
by
JustSong
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
perf: reuse http client to reduce delay
parent
df026eff
Show whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
11 additions
and
9 deletions
+11
-9
controller/channel-billing.go
+1
-2
controller/channel-test.go
+1
-2
controller/relay-image.go
+1
-2
controller/relay-openai.go
+1
-1
controller/relay-text.go
+7
-2
No files found.
controller/channel-billing.go
View file @
a989fef5
...
...
@@ -85,7 +85,6 @@ 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
...
...
@@ -93,7 +92,7 @@ func GetResponseBody(method, url string, channel *model.Channel, headers http.He
for
k
:=
range
headers
{
req
.
Header
.
Add
(
k
,
headers
.
Get
(
k
))
}
res
,
err
:=
c
lient
.
Do
(
req
)
res
,
err
:=
httpC
lient
.
Do
(
req
)
if
err
!=
nil
{
return
nil
,
err
}
...
...
controller/channel-test.go
View file @
a989fef5
...
...
@@ -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
}
...
...
controller/relay-image.go
View file @
a989fef5
...
...
@@ -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
)
}
...
...
controller/relay-openai.go
View file @
a989fef5
...
...
@@ -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
c
lient will be confused by the response.
// So the
httpC
lient 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
])
...
...
controller/relay-text.go
View file @
a989fef5
...
...
@@ -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"
)
...
...
@@ -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
)
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment