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
658eba65
authored
Aug 03, 2024
by
CalciumIon
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
chore: 优化relay代码
parent
0c0d6b4b
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
48 additions
and
26 deletions
+48
-26
controller/relay.go
+48
-25
middleware/distributor.go
+0
-1
No files found.
controller/relay.go
View file @
658eba65
...
@@ -39,38 +39,28 @@ func relayHandler(c *gin.Context, relayMode int) *dto.OpenAIErrorWithStatusCode
...
@@ -39,38 +39,28 @@ func relayHandler(c *gin.Context, relayMode int) *dto.OpenAIErrorWithStatusCode
func
Relay
(
c
*
gin
.
Context
)
{
func
Relay
(
c
*
gin
.
Context
)
{
relayMode
:=
constant
.
Path2RelayMode
(
c
.
Request
.
URL
.
Path
)
relayMode
:=
constant
.
Path2RelayMode
(
c
.
Request
.
URL
.
Path
)
retryTimes
:=
common
.
RetryTimes
requestId
:=
c
.
GetString
(
common
.
RequestIdKey
)
requestId
:=
c
.
GetString
(
common
.
RequestIdKey
)
channelId
:=
c
.
GetInt
(
"channel_id"
)
channelType
:=
c
.
GetInt
(
"channel_type"
)
channelName
:=
c
.
GetString
(
"channel_name"
)
group
:=
c
.
GetString
(
"group"
)
group
:=
c
.
GetString
(
"group"
)
originalModel
:=
c
.
GetString
(
"original_model"
)
originalModel
:=
c
.
GetString
(
"original_model"
)
openaiErr
:=
relayHandler
(
c
,
relayMode
)
var
openaiErr
*
dto
.
OpenAIErrorWithStatusCode
c
.
Set
(
"use_channel"
,
[]
string
{
fmt
.
Sprintf
(
"%d"
,
channelId
)})
if
openaiErr
!=
nil
{
for
i
:=
0
;
i
<=
common
.
RetryTimes
;
i
++
{
go
processChannelError
(
c
,
channelId
,
channelType
,
channelName
,
openaiErr
)
channel
,
err
:=
getChannel
(
c
,
group
,
originalModel
,
i
)
}
else
{
retryTimes
=
0
}
for
i
:=
0
;
shouldRetry
(
c
,
channelId
,
openaiErr
,
retryTimes
)
&&
i
<
retryTimes
;
i
++
{
channel
,
err
:=
model
.
CacheGetRandomSatisfiedChannel
(
group
,
originalModel
,
i
)
if
err
!=
nil
{
if
err
!=
nil
{
common
.
LogError
(
c
.
Request
.
Context
(),
fmt
.
Sprintf
(
"CacheGetRandomSatisfiedChannel failed
: %s"
,
err
.
Error
()))
common
.
LogError
(
c
,
fmt
.
Sprintf
(
"Failed to get channel
: %s"
,
err
.
Error
()))
break
break
}
}
channelId
=
channel
.
Id
useChannel
:=
c
.
GetStringSlice
(
"use_channel"
)
useChannel
=
append
(
useChannel
,
fmt
.
Sprintf
(
"%d"
,
channel
.
Id
))
c
.
Set
(
"use_channel"
,
useChannel
)
common
.
LogInfo
(
c
.
Request
.
Context
(),
fmt
.
Sprintf
(
"using channel #%d to retry (remain times %d)"
,
channel
.
Id
,
i
))
middleware
.
SetupContextForSelectedChannel
(
c
,
channel
,
originalModel
)
requestBody
,
err
:=
common
.
GetRequestBody
(
c
)
openaiErr
=
relayRequest
(
c
,
relayMode
,
channel
)
c
.
Request
.
Body
=
io
.
NopCloser
(
bytes
.
NewBuffer
(
requestBody
))
openaiErr
=
relayHandler
(
c
,
relayMode
)
if
openaiErr
==
nil
{
if
openaiErr
!=
nil
{
return
// 成功处理请求,直接返回
}
go
processChannelError
(
c
,
channel
.
Id
,
channel
.
Type
,
channel
.
Name
,
openaiErr
)
go
processChannelError
(
c
,
channel
.
Id
,
channel
.
Type
,
channel
.
Name
,
openaiErr
)
if
!
shouldRetry
(
c
,
openaiErr
,
common
.
RetryTimes
-
i
)
{
break
}
}
}
}
useChannel
:=
c
.
GetStringSlice
(
"use_channel"
)
useChannel
:=
c
.
GetStringSlice
(
"use_channel"
)
...
@@ -90,7 +80,36 @@ func Relay(c *gin.Context) {
...
@@ -90,7 +80,36 @@ func Relay(c *gin.Context) {
}
}
}
}
func
shouldRetry
(
c
*
gin
.
Context
,
channelId
int
,
openaiErr
*
dto
.
OpenAIErrorWithStatusCode
,
retryTimes
int
)
bool
{
func
relayRequest
(
c
*
gin
.
Context
,
relayMode
int
,
channel
*
model
.
Channel
)
*
dto
.
OpenAIErrorWithStatusCode
{
addUsedChannel
(
c
,
channel
.
Id
)
requestBody
,
_
:=
common
.
GetRequestBody
(
c
)
c
.
Request
.
Body
=
io
.
NopCloser
(
bytes
.
NewBuffer
(
requestBody
))
return
relayHandler
(
c
,
relayMode
)
}
func
addUsedChannel
(
c
*
gin
.
Context
,
channelId
int
)
{
useChannel
:=
c
.
GetStringSlice
(
"use_channel"
)
useChannel
=
append
(
useChannel
,
fmt
.
Sprintf
(
"%d"
,
channelId
))
c
.
Set
(
"use_channel"
,
useChannel
)
}
func
getChannel
(
c
*
gin
.
Context
,
group
,
originalModel
string
,
retryCount
int
)
(
*
model
.
Channel
,
error
)
{
if
retryCount
==
0
{
return
&
model
.
Channel
{
Id
:
c
.
GetInt
(
"channel_id"
),
Type
:
c
.
GetInt
(
"channel_type"
),
Name
:
c
.
GetString
(
"channel_name"
),
},
nil
}
channel
,
err
:=
model
.
CacheGetRandomSatisfiedChannel
(
group
,
originalModel
,
retryCount
)
if
err
!=
nil
{
return
nil
,
err
}
middleware
.
SetupContextForSelectedChannel
(
c
,
channel
,
originalModel
)
return
channel
,
nil
}
func
shouldRetry
(
c
*
gin
.
Context
,
openaiErr
*
dto
.
OpenAIErrorWithStatusCode
,
retryTimes
int
)
bool
{
if
openaiErr
==
nil
{
if
openaiErr
==
nil
{
return
false
return
false
}
}
...
@@ -114,6 +133,10 @@ func shouldRetry(c *gin.Context, channelId int, openaiErr *dto.OpenAIErrorWithSt
...
@@ -114,6 +133,10 @@ func shouldRetry(c *gin.Context, channelId int, openaiErr *dto.OpenAIErrorWithSt
return
true
return
true
}
}
if
openaiErr
.
StatusCode
==
http
.
StatusBadRequest
{
if
openaiErr
.
StatusCode
==
http
.
StatusBadRequest
{
channelType
:=
c
.
GetInt
(
"channel_type"
)
if
channelType
==
common
.
ChannelTypeAnthropic
{
return
true
}
return
false
return
false
}
}
if
openaiErr
.
StatusCode
==
408
{
if
openaiErr
.
StatusCode
==
408
{
...
...
middleware/distributor.go
View file @
658eba65
...
@@ -184,7 +184,6 @@ func SetupContextForSelectedChannel(c *gin.Context, channel *model.Channel, mode
...
@@ -184,7 +184,6 @@ func SetupContextForSelectedChannel(c *gin.Context, channel *model.Channel, mode
if
channel
==
nil
{
if
channel
==
nil
{
return
return
}
}
c
.
Set
(
"channel"
,
channel
.
Type
)
c
.
Set
(
"channel_id"
,
channel
.
Id
)
c
.
Set
(
"channel_id"
,
channel
.
Id
)
c
.
Set
(
"channel_name"
,
channel
.
Name
)
c
.
Set
(
"channel_name"
,
channel
.
Name
)
c
.
Set
(
"channel_type"
,
channel
.
Type
)
c
.
Set
(
"channel_type"
,
channel
.
Type
)
...
...
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