渠道: 模型名称映射(网关生效 + 渠道侧管理)
- 候选渠道携带 upstream_model, prepareUpstream 改写请求体 model 字段为上游名 - 新增渠道视角绑定 CRUD: GET/POST/PATCH/DELETE /admin/channels/:id/models - 前端渠道页新增"模型映射": 列出绑定、内联改上游名、解除、添加 - rewriteModel 三种协议通用(model 均在顶层) Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
@@ -27,69 +27,74 @@ func NewService(db *gorm.DB, enc *crypto.Encryptor) *Service {
|
||||
return &Service{db: db, enc: enc, sems: map[uint64]chan struct{}{}}
|
||||
}
|
||||
|
||||
// Candidate 一个候选渠道 + 该模型的映射关系。
|
||||
type Candidate struct {
|
||||
Channel *store.Channel
|
||||
UpstreamModel string // 全局模型在此渠道的映射名(无绑定则为空,用客户端模型名)
|
||||
}
|
||||
|
||||
// Candidates 返回可用渠道候选:健康 + 启用,按优先级、权重降序、id 升序排列。
|
||||
// model 非空时优先取绑定该模型的渠道;无绑定则退回全局。
|
||||
func (s *Service) Candidates(model string) []*store.Channel {
|
||||
// model 非空时优先取绑定该模型的渠道(携带 upstream_model 映射);无绑定则退回全局。
|
||||
func (s *Service) Candidates(model string) []Candidate {
|
||||
if model != "" {
|
||||
var b []store.ChannelModelBinding
|
||||
var modelIDs []uint64
|
||||
s.db.Model(&store.Model{}).Where("name = ? AND enabled = ?", model, true).Pluck("id", &modelIDs)
|
||||
if len(modelIDs) > 0 {
|
||||
s.db.Where("model_id IN ?", modelIDs).Find(&b)
|
||||
chs := s.loadBound(b)
|
||||
if len(chs) > 0 {
|
||||
return chs
|
||||
if cands := s.loadBound(b); len(cands) > 0 {
|
||||
return cands
|
||||
}
|
||||
}
|
||||
}
|
||||
var chs []store.Channel
|
||||
s.db.Where("enabled = ? AND health_status = ?", true, store.ChannelHealthHealthy).
|
||||
Order("priority ASC, weight DESC, id ASC").Find(&chs)
|
||||
out := make([]*store.Channel, 0, len(chs))
|
||||
out := make([]Candidate, 0, len(chs))
|
||||
for i := range chs {
|
||||
out = append(out, &chs[i])
|
||||
out = append(out, Candidate{Channel: &chs[i]})
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
// loadBound 按绑定顺序加载渠道,过滤健康/启用。
|
||||
func (s *Service) loadBound(bindings []store.ChannelModelBinding) []*store.Channel {
|
||||
// loadBound 按绑定顺序加载渠道候选,过滤健康/启用,携带 upstream_model 映射。
|
||||
func (s *Service) loadBound(bindings []store.ChannelModelBinding) []Candidate {
|
||||
if len(bindings) == 0 {
|
||||
return nil
|
||||
}
|
||||
// channel_id -> 绑定(取该渠道对该模型的映射)
|
||||
byChannel := map[uint64]store.ChannelModelBinding{}
|
||||
ids := make([]uint64, 0, len(bindings))
|
||||
seen := map[uint64]bool{}
|
||||
for _, b := range bindings {
|
||||
if !seen[b.ChannelID] {
|
||||
seen[b.ChannelID] = true
|
||||
if _, ok := byChannel[b.ChannelID]; !ok {
|
||||
ids = append(ids, b.ChannelID)
|
||||
}
|
||||
byChannel[b.ChannelID] = b
|
||||
}
|
||||
var chs []store.Channel
|
||||
s.db.Where("id IN ? AND enabled = ? AND health_status = ?", ids, true, store.ChannelHealthHealthy).
|
||||
Order("priority ASC, weight DESC, id ASC").Find(&chs)
|
||||
// 保持绑定顺序
|
||||
byID := map[uint64]*store.Channel{}
|
||||
for i := range chs {
|
||||
byID[chs[i].ID] = &chs[i]
|
||||
}
|
||||
out := make([]*store.Channel, 0, len(ids))
|
||||
out := make([]Candidate, 0, len(ids))
|
||||
for _, id := range ids {
|
||||
if ch, ok := byID[id]; ok {
|
||||
out = append(out, ch)
|
||||
out = append(out, Candidate{Channel: ch, UpstreamModel: byChannel[id].UpstreamModel})
|
||||
}
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
// Pick 按权重加权随机选一个候选(负载均衡)。
|
||||
func (s *Service) Pick(cands []*store.Channel) *store.Channel {
|
||||
// Pick 按权重加权随机选一个候选渠道(负载均衡)。
|
||||
func (s *Service) Pick(cands []Candidate) *store.Channel {
|
||||
if len(cands) == 0 {
|
||||
return nil
|
||||
}
|
||||
total := 0
|
||||
for _, c := range cands {
|
||||
w := c.Weight
|
||||
w := c.Channel.Weight
|
||||
if w <= 0 {
|
||||
w = 1
|
||||
}
|
||||
@@ -98,16 +103,16 @@ func (s *Service) Pick(cands []*store.Channel) *store.Channel {
|
||||
n, _ := rand.Int(rand.Reader, big.NewInt(int64(total)))
|
||||
acc := 0
|
||||
for _, c := range cands {
|
||||
w := c.Weight
|
||||
w := c.Channel.Weight
|
||||
if w <= 0 {
|
||||
w = 1
|
||||
}
|
||||
acc += w
|
||||
if int(n.Int64()) < acc {
|
||||
return c
|
||||
return c.Channel
|
||||
}
|
||||
}
|
||||
return cands[len(cands)-1]
|
||||
return cands[len(cands)-1].Channel
|
||||
}
|
||||
|
||||
// TryAcquire 尝试获取渠道并发槽;渠道满载返回 false(调用方可溢出到其他渠道)。
|
||||
|
||||
@@ -54,8 +54,8 @@ func TestCandidatesFiltersUnhealthy(t *testing.T) {
|
||||
if len(cands) != 2 {
|
||||
t.Fatalf("candidates = %d, want 2", len(cands))
|
||||
}
|
||||
if cands[0].Name != "a" {
|
||||
t.Fatalf("first by priority should be a, got %s", cands[0].Name)
|
||||
if cands[0].Channel.Name != "a" {
|
||||
t.Fatalf("first by priority should be a, got %s", cands[0].Channel.Name)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user