chore: 优化自动禁用代码

This commit is contained in:
CalciumIon 2024-08-03 17:32:28 +08:00
parent 8af0d9f22f
commit 5acf074541
5 changed files with 23 additions and 14 deletions

View File

@ -240,7 +240,7 @@ func testAllChannels(notify bool) error {
} }
// parse *int to bool // parse *int to bool
if channel.AutoBan != nil && *channel.AutoBan == 0 { if !channel.GetAutoBan() {
ban = false ban = false
} }

View File

@ -59,7 +59,7 @@ func Relay(c *gin.Context) {
return // 成功处理请求,直接返回 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) { if !shouldRetry(c, openaiErr, common.RetryTimes-i) {
break 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) { func getChannel(c *gin.Context, group, originalModel string, retryCount int) (*model.Channel, error) {
if retryCount == 0 { if retryCount == 0 {
autoBan := c.GetBool("auto_ban")
autoBanInt := 1
if !autoBan {
autoBanInt = 0
}
return &model.Channel{ return &model.Channel{
Id: c.GetInt("channel_id"), Id: c.GetInt("channel_id"),
Type: c.GetInt("channel_type"), Type: c.GetInt("channel_type"),
Name: c.GetString("channel_name"), Name: c.GetString("channel_name"),
AutoBan: &autoBanInt,
}, nil }, nil
} }
channel, err := model.CacheGetRandomSatisfiedChannel(group, originalModel, retryCount) channel, err := model.CacheGetRandomSatisfiedChannel(group, originalModel, retryCount)
@ -154,8 +160,9 @@ func shouldRetry(c *gin.Context, openaiErr *dto.OpenAIErrorWithStatusCode, retry
return true return true
} }
func processChannelError(c *gin.Context, channelId int, channelType int, channelName string, err *dto.OpenAIErrorWithStatusCode) { func processChannelError(c *gin.Context, channelId int, channelType int, channelName string, autoBan bool, err *dto.OpenAIErrorWithStatusCode) {
autoBan := c.GetBool("auto_ban") // 不要使用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)) 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 { if service.ShouldDisableChannel(channelType, err) && autoBan {
service.DisableChannel(channelId, channelName, err.Error.Message) service.DisableChannel(channelId, channelName, err.Error.Message)

View File

@ -187,15 +187,10 @@ func SetupContextForSelectedChannel(c *gin.Context, channel *model.Channel, mode
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)
ban := true
// parse *int to bool
if channel.AutoBan != nil && *channel.AutoBan == 0 {
ban = false
}
if nil != channel.OpenAIOrganization && "" != *channel.OpenAIOrganization { if nil != channel.OpenAIOrganization && "" != *channel.OpenAIOrganization {
c.Set("channel_organization", *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("model_mapping", channel.GetModelMapping())
c.Set("status_code_mapping", channel.GetStatusCodeMapping()) c.Set("status_code_mapping", channel.GetStatusCodeMapping())
c.Request.Header.Set("Authorization", fmt.Sprintf("Bearer %s", channel.Key)) c.Request.Header.Set("Authorization", fmt.Sprintf("Bearer %s", channel.Key))

View File

@ -61,6 +61,13 @@ func (channel *Channel) SetOtherInfo(otherInfo map[string]interface{}) {
channel.OtherInfo = string(otherInfoBytes) channel.OtherInfo = string(otherInfoBytes)
} }
func (channel *Channel) GetAutoBan() bool {
if channel.AutoBan == nil {
return false
}
return *channel.AutoBan == 1
}
func (channel *Channel) Save() error { func (channel *Channel) Save() error {
return DB.Save(channel).Error return DB.Save(channel).Error
} }

View File

@ -549,7 +549,7 @@ func RelayMidjourneySubmit(c *gin.Context, relayMode int) *dto.MidjourneyRespons
if err != nil { if err != nil {
common.SysError("get_channel_null: " + err.Error()) 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") model.UpdateChannelStatusById(midjourneyTask.ChannelId, 2, "No available account instance")
} }
} }