Files
CLIProxyAPI/sdk/cliproxy/auth/weight.go
Luis Pater 5dcca50fd9 feat(auth): introduce weighted round-robin scheduler and credential weight validation
- Added support for weighted round-robin authentication scheduling strategy.
- Implemented credential weight validation for attributes and metadata, with strict error handling for invalid weights.
- Enhanced scheduler with smooth weighted state handling and proportional selection logic.
- Introduced tests for credential weight parsing, validation, and weighted round-robin behavior.
- Updated configuration to include `weight` field for credentials with range validation.

Closes: #4470
2026-07-28 14:23:23 +08:00

50 lines
1.3 KiB
Go

package auth
import (
"fmt"
"strconv"
"github.com/router-for-me/CLIProxyAPI/v7/internal/credentialweight"
)
// ValidateAuthWeight validates every explicit credential weight source.
func ValidateAuthWeight(auth *Auth) error {
if auth == nil {
return nil
}
if rawWeight, ok := auth.Attributes[AttributeWeight]; ok {
if _, errParse := credentialweight.ParseString(rawWeight); errParse != nil {
return fmt.Errorf("invalid attributes weight: %w", errParse)
}
}
if rawWeight, ok := auth.Metadata[AttributeWeight]; ok {
if _, errParse := credentialweight.ParseValue(rawWeight); errParse != nil {
return fmt.Errorf("invalid metadata weight: %w", errParse)
}
}
return nil
}
// ApplyAuthWeightMetadata validates the auth and applies a source metadata weight.
func ApplyAuthWeightMetadata(auth *Auth, metadata map[string]any) error {
if errWeight := ValidateAuthWeight(auth); errWeight != nil {
return errWeight
}
if auth == nil || metadata == nil {
return nil
}
rawWeight, ok := metadata[AttributeWeight]
if !ok {
return nil
}
weight, errParse := credentialweight.ParseValue(rawWeight)
if errParse != nil {
return fmt.Errorf("invalid metadata weight: %w", errParse)
}
if auth.Attributes == nil {
auth.Attributes = make(map[string]string)
}
auth.Attributes[AttributeWeight] = strconv.FormatInt(weight, 10)
return nil
}