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
a1284e4a
authored
Aug 03, 2024
by
CalciumIon
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
chore: 优化自动禁用代码
parent
421d34b3
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
23 additions
and
14 deletions
+23
-14
controller/channel-test.go
+1
-1
controller/relay.go
+13
-6
middleware/distributor.go
+1
-6
model/channel.go
+7
-0
relay/relay-mj.go
+1
-1
No files found.
controller/channel-test.go
View file @
a1284e4a
...
...
@@ -240,7 +240,7 @@ func testAllChannels(notify bool) error {
}
// parse *int to bool
if
channel
.
AutoBan
!=
nil
&&
*
channel
.
AutoBan
==
0
{
if
!
channel
.
GetAutoBan
()
{
ban
=
false
}
...
...
controller/relay.go
View file @
a1284e4a
...
...
@@ -59,7 +59,7 @@ func Relay(c *gin.Context) {
return
// 成功处理请求,直接返回
}
go
processChannelError
(
c
,
channel
.
Id
,
channel
.
Type
,
channel
.
Name
,
openaiErr
)
go
processChannelError
(
c
,
channel
.
Id
,
channel
.
Type
,
channel
.
Name
,
channel
.
GetAutoBan
(),
openaiErr
)
if
!
shouldRetry
(
c
,
openaiErr
,
common
.
RetryTimes
-
i
)
{
break
...
...
@@ -97,10 +97,16 @@ func addUsedChannel(c *gin.Context, channelId int) {
func
getChannel
(
c
*
gin
.
Context
,
group
,
originalModel
string
,
retryCount
int
)
(
*
model
.
Channel
,
error
)
{
if
retryCount
==
0
{
autoBan
:=
c
.
GetBool
(
"auto_ban"
)
autoBanInt
:=
1
if
!
autoBan
{
autoBanInt
=
0
}
return
&
model
.
Channel
{
Id
:
c
.
GetInt
(
"channel_id"
),
Type
:
c
.
GetInt
(
"channel_type"
),
Name
:
c
.
GetString
(
"channel_name"
),
Id
:
c
.
GetInt
(
"channel_id"
),
Type
:
c
.
GetInt
(
"channel_type"
),
Name
:
c
.
GetString
(
"channel_name"
),
AutoBan
:
&
autoBanInt
,
},
nil
}
channel
,
err
:=
model
.
CacheGetRandomSatisfiedChannel
(
group
,
originalModel
,
retryCount
)
...
...
@@ -154,8 +160,9 @@ func shouldRetry(c *gin.Context, openaiErr *dto.OpenAIErrorWithStatusCode, retry
return
true
}
func
processChannelError
(
c
*
gin
.
Context
,
channelId
int
,
channelType
int
,
channelName
string
,
err
*
dto
.
OpenAIErrorWithStatusCode
)
{
autoBan
:=
c
.
GetBool
(
"auto_ban"
)
func
processChannelError
(
c
*
gin
.
Context
,
channelId
int
,
channelType
int
,
channelName
string
,
autoBan
bool
,
err
*
dto
.
OpenAIErrorWithStatusCode
)
{
// 不要使用context获取渠道信息,异步处理时可能会出现渠道信息不一致的情况
// do not use context to get channel info, there may be inconsistent channel info when processing asynchronously
common
.
LogError
(
c
.
Request
.
Context
(),
fmt
.
Sprintf
(
"relay error (channel #%d, status code: %d): %s"
,
channelId
,
err
.
StatusCode
,
err
.
Error
.
Message
))
if
service
.
ShouldDisableChannel
(
channelType
,
err
)
&&
autoBan
{
service
.
DisableChannel
(
channelId
,
channelName
,
err
.
Error
.
Message
)
...
...
middleware/distributor.go
View file @
a1284e4a
...
...
@@ -187,15 +187,10 @@ func SetupContextForSelectedChannel(c *gin.Context, channel *model.Channel, mode
c
.
Set
(
"channel_id"
,
channel
.
Id
)
c
.
Set
(
"channel_name"
,
channel
.
Name
)
c
.
Set
(
"channel_type"
,
channel
.
Type
)
ban
:=
true
// parse *int to bool
if
channel
.
AutoBan
!=
nil
&&
*
channel
.
AutoBan
==
0
{
ban
=
false
}
if
nil
!=
channel
.
OpenAIOrganization
&&
""
!=
*
channel
.
OpenAIOrganization
{
c
.
Set
(
"channel_organization"
,
*
channel
.
OpenAIOrganization
)
}
c
.
Set
(
"auto_ban"
,
ban
)
c
.
Set
(
"auto_ban"
,
channel
.
GetAutoBan
()
)
c
.
Set
(
"model_mapping"
,
channel
.
GetModelMapping
())
c
.
Set
(
"status_code_mapping"
,
channel
.
GetStatusCodeMapping
())
c
.
Request
.
Header
.
Set
(
"Authorization"
,
fmt
.
Sprintf
(
"Bearer %s"
,
channel
.
Key
))
...
...
model/channel.go
View file @
a1284e4a
...
...
@@ -61,6 +61,13 @@ func (channel *Channel) SetOtherInfo(otherInfo map[string]interface{}) {
channel
.
OtherInfo
=
string
(
otherInfoBytes
)
}
func
(
channel
*
Channel
)
GetAutoBan
()
bool
{
if
channel
.
AutoBan
==
nil
{
return
false
}
return
*
channel
.
AutoBan
==
1
}
func
(
channel
*
Channel
)
Save
()
error
{
return
DB
.
Save
(
channel
)
.
Error
}
...
...
relay/relay-mj.go
View file @
a1284e4a
...
...
@@ -549,7 +549,7 @@ func RelayMidjourneySubmit(c *gin.Context, relayMode int) *dto.MidjourneyRespons
if
err
!=
nil
{
common
.
SysError
(
"get_channel_null: "
+
err
.
Error
())
}
if
channel
.
AutoBan
!=
nil
&&
*
channel
.
AutoBan
==
1
&&
common
.
AutomaticDisableChannelEnabled
{
if
channel
.
GetAutoBan
()
&&
common
.
AutomaticDisableChannelEnabled
{
model
.
UpdateChannelStatusById
(
midjourneyTask
.
ChannelId
,
2
,
"No available account instance"
)
}
}
...
...
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