Skip to content

Commit f4b22a0

Browse files
zyszys
authored andcommitted
fix(bridge): never route requests across account regions
The unified console started the CN bridge from buildAccountPool(), which mixes every account regardless of region. Slot selection only checked the model catalog, never the region, so a CN request for a model that exists on both deployments (qmodel_38max, qfmodel, ...) could be handed to a global account and spend its credits - even though the operator never started the global side. - build the CN bridge from buildRegionPool(acct.Region) so 8963 only holds same-region accounts; 8964 already used the global-only pool - bind the region onto Bridge via NewPoolBridgeFor and filter on it in both the catalog match and the fallback pick, so a cross-region slot can never be selected even if a pool is misbuilt - drop buildAccountPool() now that nothing calls it - log the serving account, its id, region and model on every upstream call and on failover, so billing surprises are traceable with 'grep "using account" logs/qccg-*.log'
1 parent 2cd2ccd commit f4b22a0

3 files changed

Lines changed: 45 additions & 43 deletions

File tree

‎internal/bridge/bridge.go‎

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -406,7 +406,7 @@ func (b *Bridge) CallQoderWithOpts(ctx context.Context, agent string, messages [
406406
if len(prompt) > 80 {
407407
preview = preview[:80] + "..."
408408
}
409-
logger.Info("callQoder model=%s prompt=%s", model, preview)
409+
logger.Info("callQoder region=%s model=%s prompt=%s", b.region, model, preview)
410410

411411
// ---- 账号池选号 + 失败换号 -------------------------------------------
412412
// 逐个尝试池内账号:某账号失败且「尚未向客户端发出任何内容」、且错误属于
@@ -419,7 +419,10 @@ func (b *Bridge) CallQoderWithOpts(ctx context.Context, agent string, messages [
419419
if !ok {
420420
break
421421
}
422-
logger.Info("callQoder: using account %s (%s)", slot.Name, slot.ID)
422+
// 记录本次实际使用的账号(含 region),便于对账与排查
423+
// 「请求是否路由到了未启动的那一侧账号」类问题。
424+
logger.Info("callQoder: using account=%s id=%s region=%s model=%s",
425+
slot.Name, slot.ID, slot.Region, model)
423426
// 每个账号用全新的请求 id / session,避免跨账号复用同一标识
424427
nid := cosy.NewUUID()
425428
body["request_id"] = nid
@@ -449,7 +452,8 @@ func (b *Bridge) CallQoderWithOpts(ctx context.Context, agent string, messages [
449452
}
450453
b.coolSlot(slot.ID, CooldownFor(err), err.Error())
451454
tried[slot.ID] = struct{}{}
452-
logger.Info("callQoder: account %s failed, failing over to next account: %v", slot.ID, err)
455+
logger.Info("callQoder: account=%s id=%s region=%s failed, failing over to next account: %v",
456+
slot.Name, slot.ID, slot.Region, err)
453457
}
454458
if lastErr == nil {
455459
lastErr = ErrNoAccount

‎internal/bridge/bridge_pool.go‎

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,11 @@ func NewPoolBridge(pool *Pool, templateBase map[string]interface{}) *Bridge {
2121
return &Bridge{pool: pool, templateBase: templateBase}
2222
}
2323

24+
// NewPoolBridgeFor 指定本桥 region,用于选号时拒绝跨区账号。
25+
func NewPoolBridgeFor(region account.Region, pool *Pool, templateBase map[string]interface{}) *Bridge {
26+
return &Bridge{pool: pool, templateBase: templateBase, region: region}
27+
}
28+
2429
// candidateSlots 返回当前可参与选号的账号:池优先;无池时退化为单账号
2530
// (保持既有单账号构造方式与测试兼容)。
2631
func (b *Bridge) candidateSlots() []*Slot {
@@ -108,17 +113,38 @@ func (b *Bridge) slotSupportsModel(slot *Slot, model string) bool {
108113
return slot.catalog[model]
109114
}
110115

111-
// pickSlotForModel 按模型选号:优先选目录里有该模型的账号;都没有则退回任意可用账号。
116+
// pickSlotForModel 按模型选号:优先选目录里有该模型的同 region 账号。
117+
// 兜底同样限定同 region——跨 region 会把请求派给别区账号并扣其 credits
118+
// (实测 8963 曾把 CN 请求派给 global 账号产生扣费)。
112119
func (b *Bridge) pickSlotForModel(model string, exclude map[string]struct{}) (*Slot, bool) {
120+
// 本桥期望的区域:Bridge.region(单账号模式天然有值);
121+
// 池模式若 region 为空则回退到池内首个 slot。
122+
wantRegion := b.region
123+
if wantRegion == "" && b.pool != nil {
124+
b.pool.mu.Lock()
125+
for _, s := range b.pool.slots {
126+
wantRegion = s.Region
127+
break
128+
}
129+
b.pool.mu.Unlock()
130+
}
131+
// 防御性过滤:即使池被误构建为跨区,也不选别区账号
132+
// (跨区会扣到用户根本没启动的那侧账号的 credits)。
133+
sameRegion := func(s *Slot) bool { return wantRegion == "" || s.Region == wantRegion }
134+
113135
if b.pool == nil {
114136
return b.pickSlot(exclude)
115137
}
116138
slot, ok := b.pool.Pick(exclude, func(s *Slot) bool {
139+
if !sameRegion(s) {
140+
return false
141+
}
117142
b.ensureCatalog(s)
118143
return b.slotSupportsModel(s, model)
119144
})
120145
if ok {
121146
return slot, true
122147
}
123-
return b.pool.Pick(exclude)
148+
// 池内同 region 账号目录均不含该模型:仍在同 region 内兜底。
149+
return b.pool.Pick(exclude, sameRegion)
124150
}

‎service.go‎

Lines changed: 10 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -242,15 +242,18 @@ func (s *Service) startBridgeWithAccount(acct *account.Account) error {
242242
var templateBase map[string]interface{}
243243
_ = json.Unmarshal([]byte(tmpl), &templateBase)
244244

245-
// 账号池:把所有有凭证的账号都纳入。请求时轮询选号,某个账号限额/限流/
246-
// 瞬时故障时冷却该账号并自动换下一个(见 internal/bridge/pool.go)。
247-
pool := s.buildAccountPool()
245+
// 主桥账号池:只纳入与本桥区域一致的账号。
246+
// 必须按 region 隔离:若把 global 账号也放进 8963 的池,模型目录命中
247+
// (如 qmodel_38max / qfmodel 两区都有)会把请求派给 global 账号并扣其 credits,
248+
// 而用户可能根本没启动 global 侧。请求时轮询选号,限额/限流/瞬时故障时
249+
// 冷却该账号并自动换下一个(见 internal/bridge/pool.go)。
250+
pool := s.buildRegionPool(acct.Region)
248251
if pool.Size() == 0 {
249-
return fmt.Errorf("no account with usable secret; please OAuth/PAT login in web console")
252+
return fmt.Errorf("no %s account with usable secret; please OAuth/PAT login in web console", acct.Region)
250253
}
251-
logger.Info("startBridgeWithAccount: pool size=%d (requested active=%s)", pool.Size(), acct.Name)
254+
logger.Info("startBridgeWithAccount: region=%s pool size=%d (requested active=%s)", acct.Region, pool.Size(), acct.Name)
252255

253-
b := bridge.NewPoolBridge(pool, templateBase)
256+
b := bridge.NewPoolBridgeFor(acct.Region, pool, templateBase)
254257

255258
mux := http.NewServeMux()
256259
mux.HandleFunc("/v1/chat/completions", b.HandleChatCompletions)
@@ -294,7 +297,7 @@ func (s *Service) startGlobalBridge(templateBase map[string]interface{}) error {
294297
if pool.Size() == 0 {
295298
return fmt.Errorf("no global account with usable secret")
296299
}
297-
b := bridge.NewPoolBridge(pool, templateBase)
300+
b := bridge.NewPoolBridgeFor(account.RegionGlobal, pool, templateBase)
298301

299302
mux := http.NewServeMux()
300303
mux.HandleFunc("/v1/chat/completions", b.HandleChatCompletions)
@@ -320,37 +323,6 @@ func (s *Service) startGlobalBridge(templateBase map[string]interface{}) error {
320323
return nil
321324
}
322325

323-
// buildAccountPool 把所有有 secret 的账号建成运行时账号池。
324-
// 单个账号建会话失败(凭证失效/网络等)只跳过该账号,不阻断整池启动。
325-
func (s *Service) buildAccountPool() *bridge.Pool {
326-
pool := bridge.NewPoolWithState(filepath.Join(account.DataRoot(), "pool_state.json"))
327-
accounts, err := account.List()
328-
if err != nil {
329-
logger.Error("buildAccountPool: list accounts: %v", err)
330-
return pool
331-
}
332-
for _, a := range accounts {
333-
if !account.HasSecret(a.ID) {
334-
continue
335-
}
336-
pat, err := account.GetSecret(a.ID)
337-
if err != nil {
338-
logger.Error("buildAccountPool: get secret for %s: %v", a.Name, err)
339-
continue
340-
}
341-
slot, err := bridge.NewSlotFromSecret(a.ID, a.Name, a.Region, pat)
342-
if err != nil {
343-
logger.Error("buildAccountPool: account %s unusable, skipped: %v", a.Name, err)
344-
continue
345-
}
346-
pool.Add(slot)
347-
logger.Info("buildAccountPool: added account %s (region=%s)", a.Name, a.Region)
348-
}
349-
// 账号全部入池后再恢复冷却状态(构造时空池 load 匹配不到 ID)。
350-
pool.LoadState()
351-
return pool
352-
}
353-
354326
// buildRegionPool 只把指定区域的账号建成池(用于 8964 Global 专用桥)。
355327
// 状态文件按区域隔离,避免与主桥互相覆盖冷却记录。
356328
func (s *Service) buildRegionPool(region account.Region) *bridge.Pool {

0 commit comments

Comments
 (0)