(c *gin.Context)
| 386 | } |
| 387 | |
| 388 | func RelayTask(c *gin.Context) { |
| 389 | retryTimes := common.RetryTimes |
| 390 | channelId := c.GetInt("channel_id") |
| 391 | relayMode := c.GetInt("relay_mode") |
| 392 | group := c.GetString("group") |
| 393 | originalModel := c.GetString("original_model") |
| 394 | c.Set("use_channel", []string{fmt.Sprintf("%d", channelId)}) |
| 395 | taskErr := taskRelayHandler(c, relayMode) |
| 396 | if taskErr == nil { |
| 397 | retryTimes = 0 |
| 398 | } |
| 399 | for i := 0; shouldRetryTaskRelay(c, channelId, taskErr, retryTimes) && i < retryTimes; i++ { |
| 400 | channel, newAPIError := getChannel(c, group, originalModel, i) |
| 401 | if newAPIError != nil { |
| 402 | common.LogError(c, fmt.Sprintf("CacheGetRandomSatisfiedChannel failed: %s", newAPIError.Error())) |
| 403 | taskErr = service.TaskErrorWrapperLocal(newAPIError.Err, "get_channel_failed", http.StatusInternalServerError) |
| 404 | break |
| 405 | } |
| 406 | channelId = channel.Id |
| 407 | useChannel := c.GetStringSlice("use_channel") |
| 408 | useChannel = append(useChannel, fmt.Sprintf("%d", channelId)) |
| 409 | c.Set("use_channel", useChannel) |
| 410 | common.LogInfo(c, fmt.Sprintf("using channel #%d to retry (remain times %d)", channel.Id, i)) |
| 411 | //middleware.SetupContextForSelectedChannel(c, channel, originalModel) |
| 412 | |
| 413 | requestBody, _ := common.GetRequestBody(c) |
| 414 | c.Request.Body = io.NopCloser(bytes.NewBuffer(requestBody)) |
| 415 | taskErr = taskRelayHandler(c, relayMode) |
| 416 | } |
| 417 | useChannel := c.GetStringSlice("use_channel") |
| 418 | if len(useChannel) > 1 { |
| 419 | retryLogStr := fmt.Sprintf("重试:%s", strings.Trim(strings.Join(strings.Fields(fmt.Sprint(useChannel)), "->"), "[]")) |
| 420 | common.LogInfo(c, retryLogStr) |
| 421 | } |
| 422 | if taskErr != nil { |
| 423 | if taskErr.StatusCode == http.StatusTooManyRequests { |
| 424 | taskErr.Message = "当前分组上游负载已饱和,请稍后再试" |
| 425 | } |
| 426 | c.JSON(taskErr.StatusCode, taskErr) |
| 427 | } |
| 428 | } |
| 429 | |
| 430 | func taskRelayHandler(c *gin.Context, relayMode int) *dto.TaskError { |
| 431 | var err *dto.TaskError |
nothing calls this directly
no test coverage detected