fix(rate-limit): make 429 fallback cooldown configurable
This commit is contained in:
@@ -2748,6 +2748,55 @@ func (s *SettingService) SetOverloadCooldownSettings(ctx context.Context, settin
|
||||
return s.settingRepo.Set(ctx, SettingKeyOverloadCooldownSettings, string(data))
|
||||
}
|
||||
|
||||
// GetRateLimit429CooldownSettings 获取429默认回避配置
|
||||
func (s *SettingService) GetRateLimit429CooldownSettings(ctx context.Context) (*RateLimit429CooldownSettings, error) {
|
||||
value, err := s.settingRepo.GetValue(ctx, SettingKeyRateLimit429CooldownSettings)
|
||||
if err != nil {
|
||||
if errors.Is(err, ErrSettingNotFound) {
|
||||
return DefaultRateLimit429CooldownSettings(), nil
|
||||
}
|
||||
return nil, fmt.Errorf("get 429 cooldown settings: %w", err)
|
||||
}
|
||||
if value == "" {
|
||||
return DefaultRateLimit429CooldownSettings(), nil
|
||||
}
|
||||
|
||||
var settings RateLimit429CooldownSettings
|
||||
if err := json.Unmarshal([]byte(value), &settings); err != nil {
|
||||
return DefaultRateLimit429CooldownSettings(), nil
|
||||
}
|
||||
|
||||
if settings.CooldownSeconds < 1 {
|
||||
settings.CooldownSeconds = 1
|
||||
}
|
||||
if settings.CooldownSeconds > 7200 {
|
||||
settings.CooldownSeconds = 7200
|
||||
}
|
||||
|
||||
return &settings, nil
|
||||
}
|
||||
|
||||
// SetRateLimit429CooldownSettings 设置429默认回避配置
|
||||
func (s *SettingService) SetRateLimit429CooldownSettings(ctx context.Context, settings *RateLimit429CooldownSettings) error {
|
||||
if settings == nil {
|
||||
return fmt.Errorf("settings cannot be nil")
|
||||
}
|
||||
|
||||
if settings.CooldownSeconds < 1 || settings.CooldownSeconds > 7200 {
|
||||
if settings.Enabled {
|
||||
return fmt.Errorf("cooldown_seconds must be between 1-7200")
|
||||
}
|
||||
settings.CooldownSeconds = 5
|
||||
}
|
||||
|
||||
data, err := json.Marshal(settings)
|
||||
if err != nil {
|
||||
return fmt.Errorf("marshal 429 cooldown settings: %w", err)
|
||||
}
|
||||
|
||||
return s.settingRepo.Set(ctx, SettingKeyRateLimit429CooldownSettings, string(data))
|
||||
}
|
||||
|
||||
// GetOIDCConnectOAuthConfig 返回用于登录的“最终生效” OIDC 配置。
|
||||
//
|
||||
// 优先级:
|
||||
|
||||
Reference in New Issue
Block a user