feat(relay): add in-memory adaptive circuit breaker and auto-fallback retry mechanism

This commit is contained in:
张名锐
2026-08-28 19:14:48 +08:00
parent e468b73915
commit 378256a538
7 changed files with 358 additions and 24 deletions
+10
View File
@@ -230,12 +230,22 @@ func Relay(c *gin.Context, relayFormat types.RelayFormat) {
if newAPIError == nil {
relayInfo.LastError = nil
service.GlobalCircuitBreaker.RecordSuccess(channel.Id)
useChannel := c.GetStringSlice("use_channel")
if len(useChannel) > 1 {
c.Header("X-New-API-Fallback-Count", fmt.Sprintf("%d", len(useChannel)-1))
logger.LogInfo(c, fmt.Sprintf("[Auto-Fallback] 自动无感降级重试成功: 路径 %s", strings.Trim(strings.Join(strings.Fields(fmt.Sprint(useChannel)), " -> "), "[]")))
}
return
}
newAPIError = service.NormalizeViolationFeeError(newAPIError)
relayInfo.LastError = newAPIError
// 记录失败至自适应熔断器,并将故障渠道加入本次请求的排除列表以实现无感自动降级
service.GlobalCircuitBreaker.RecordFailure(channel.Id, newAPIError.StatusCode)
retryParam.AddExcludedChannel(channel.Id)
processChannelError(c, *types.NewChannelError(channel.Id, channel.Type, channel.Name, channel.ChannelInfo.IsMultiKey, common.GetContextKeyString(c, constant.ContextKeyChannelKey), channel.GetAutoBan()), newAPIError)
if !shouldRetry(c, newAPIError, common.RetryTimes-retryParam.GetRetry()) {