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>
This commit is contained in:
@@ -0,0 +1,49 @@
|
||||
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
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
package balancer_test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/rose_cat707/luminary/internal/balancer"
|
||||
"github.com/rose_cat707/luminary/internal/model"
|
||||
)
|
||||
|
||||
func TestSelectWeighted(t *testing.T) {
|
||||
candidates := []balancer.KeyCandidate{
|
||||
{Key: model.ProviderKey{ID: 1, Name: "a"}, Weight: 1},
|
||||
{Key: model.ProviderKey{ID: 2, Name: "b"}, Weight: 3},
|
||||
}
|
||||
counts := map[uint]int{}
|
||||
for i := 0; i < 1000; i++ {
|
||||
k := balancer.SelectWeighted(candidates)
|
||||
if k == nil {
|
||||
t.Fatal("nil key")
|
||||
}
|
||||
counts[k.ID]++
|
||||
}
|
||||
if counts[2] <= counts[1] {
|
||||
t.Fatalf("expected heavier weight key selected more often: %v", counts)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSelectWeightedEmpty(t *testing.T) {
|
||||
if k := balancer.SelectWeighted(nil); k != nil {
|
||||
t.Fatal("expected nil")
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user