Explorar o código

chore: 优化自动禁用代码

CalciumIon hai 1 ano
pai
achega
5acf074541
Modificáronse 5 ficheiros con 23 adicións e 14 borrados
  1. 1 1
      controller/channel-test.go
  2. 13 6
      controller/relay.go
  3. 1 6
      middleware/distributor.go
  4. 7 0
      model/channel.go
  5. 1 1
      relay/relay-mj.go

+ 1 - 1
controller/channel-test.go

@@ -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
 			}
 

+ 13 - 6
controller/relay.go

@@ -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)

+ 1 - 6
middleware/distributor.go

@@ -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))

+ 7 - 0
model/channel.go

@@ -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
 }

+ 1 - 1
relay/relay-mj.go

@@ -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")
 		}
 	}