76ba500417
CI / docker (push) Successful in 2m4s
OpenAI-compatible AI gateway with Vue admin UI, multi-provider egress, ingress key governance, monitoring, and security controls. Co-authored-by: Cursor <cursoragent@cursor.com>
50 lines
746 B
Go
50 lines
746 B
Go
package balancer
|
|
|
|
import (
|
|
"math/rand"
|
|
|
|
"github.com/rose_cat707/luminary/internal/model"
|
|
)
|
|
|
|
type KeyCandidate struct {
|
|
Key model.ProviderKey
|
|
Weight float64
|
|
}
|
|
|
|
func SelectWeighted(candidates []KeyCandidate) *model.ProviderKey {
|
|
if len(candidates) == 0 {
|
|
return nil
|
|
}
|
|
if len(candidates) == 1 {
|
|
k := candidates[0].Key
|
|
return &k
|
|
}
|
|
var total float64
|
|
for _, c := range candidates {
|
|
w := c.Weight
|
|
if w <= 0 {
|
|
w = 1
|
|
}
|
|
total += w
|
|
}
|
|
if total <= 0 {
|
|
k := candidates[0].Key
|
|
return &k
|
|
}
|
|
r := rand.Float64() * total
|
|
var acc float64
|
|
for _, c := range candidates {
|
|
w := c.Weight
|
|
if w <= 0 {
|
|
w = 1
|
|
}
|
|
acc += w
|
|
if r <= acc {
|
|
k := c.Key
|
|
return &k
|
|
}
|
|
}
|
|
k := candidates[len(candidates)-1].Key
|
|
return &k
|
|
}
|