DD asked for weighted round robin as a group routing, next to In turn.
A provider's keys could already route by weight (#841). Groups could not.
What changes:
- provider: Group.Weights maps a member's spelling to its weight, 1 to
1000. A member left out, or set to 0, weighs 1. SaveGroup accepts
routing "weight". It also cleans weights the way it cleans fast and
off: it strips :fast, drops weights of members that are gone and of
members weighing 1, and refuses a weight below 0 or above 1000.
Weights follow a member when it is renamed (RenameMember, Rename,
RenameGroup) and survive Share/Import.
- gateway: planLevel treats each member of a weight-routed group as one
unit: a model with its keys in its provider's order, or a nested group
routed its own way. weightedUnits orders the units by smooth WRR on a
round per group ("group/<id>"), sharing wrrPick with the keys. The
round's pick goes first and the rest stay in member order for
failover. A unit whose head is resting leaves the round. When a
conversation is kept on another member (session/turn/cache/strict),
the charge moves to that member (wrrSeat, wrrMove).
- cli: routing=weight (also weighted, wrr). weight=<model>=<n>[,...]
sets weights and keeps the rest; weight= on its own clears them. The
group view shows "· weight N" for each member.
- tui: By weight is in the group routing list.
- gui: the group editor offers By weight. Each member then shows
"Weight N · S%", its share among the members that are on. Clicking it
edits the number in place: Enter or blur keeps it, Escape cancels,
and a value outside 1 to 1000 shows a warning. The trace header uses
group wording for a weight-routed group. Strings are added in zh,
zh-TW, ja and de.
- docs: reference.md, gateway-routing.md, and site start pages in en,
zh and ja.
Run:
- go test -tags nogui ./internal/provider ./internal/cli ./internal/tui:
ok.
- ./internal/gateway: ok in 988s with -timeout 45m. The first run hit
the default 10m timeout at load average ~170. The test running at
that moment had been running for 2s; it was not a hang.
- go vet -tags nogui on those packages, for darwin, GOOS=linux and
GOOS=windows: clean. gofmt -l: clean.
- New group-member-weight.test.cjs: 8/8 in Chromium and WebKit, en and
zh at 1100, en and de at 420. The WebKit de 420 case also passed six
runs at once.
- gui-zh-tw, gui-ja, gui-de, i18n-keys, routing-*, group-*,
panel-routing*, settings-groups and agent-model-groups: all green
except two.
- routing-steady (WebKit) is #1804. It is red 2/3 alone on origin/main
e1b70c13d too.
- routing-save-blank (chromium en) was red under load only, and 3/3
green alone. The overshoot in its wheel loop is filed as #1924.
Broken by hand, each red, then restored:
- byWeight forced false:
--- FAIL: TestWeightedGroup (weighted_group_test.go:69 its turn ...)
- cleanWeights call skipped:
--- FAIL: TestSaveGroupWeights (groupweight_test.go:39 weights map[...
an/claude-opus-5-5:0 ... oa/gpt-6.1-sol:high:fast:3 ...])
- CLI weight case renamed:
--- FAIL: TestGroupWeightCLI (unknown field "weight")
- GUI chip not appended: group-member-weight chromium en 1100 timed out
on the weight button.
- Not red: the test's "moved nothing" check. A scroll forced on click,
or padding added above, did not turn it red, because the view's own
scroll guard and scroll anchoring hold the clicked row in place. It
only shows that the row stays under the pointer.
Not tried: real accounts through a running gateway. The routing is
proven by the plan-level test with synthetic providers only.
167 lines
5.1 KiB
Go
167 lines
5.1 KiB
Go
package gateway
|
|
|
|
// Weighted routing (#841): a provider's requests spread over its keys by
|
|
// each key's weight, as NGINX's smooth weighted round robin does — keys
|
|
// weighing 3 and 1 go a, a, b, a, a, a, b, a…, the ratio held over a
|
|
// handful of requests rather than only on average. A key resting after a
|
|
// failure is left out of the round while it rests, so the others share
|
|
// its requests by their own weights and it takes up its share when it is
|
|
// back. A conversation still stays with the key that answered it while
|
|
// its affinity says (affinity.go); a request kept so counts against the
|
|
// key that took it, not the one the round picked (weightedKept), so new
|
|
// conversations make up the share.
|
|
//
|
|
// A group routed by weight (#1910) spreads its requests over its members
|
|
// the same way, by each member's weight (provider.Group.Weights): a
|
|
// member's own keys are ordered by its provider's routing, and a group in
|
|
// it is one member, planned by its own.
|
|
|
|
import (
|
|
"sync"
|
|
|
|
"github.com/yetone/magpie/internal/provider"
|
|
)
|
|
|
|
// wrr is each weighted provider's round: every key's current weight, and
|
|
// the total the last pick was charged.
|
|
var wrr = struct {
|
|
sync.Mutex
|
|
m map[string]*wrrRound // provider id → its round
|
|
}{m: map[string]*wrrRound{}}
|
|
|
|
type wrrRound struct {
|
|
cur map[string]int // candidate rest, or a group's member → current weight
|
|
total int
|
|
last string // the one the last pick was charged to
|
|
}
|
|
|
|
// keyWeight is a candidate's weight: its key's, 1 when none.
|
|
func keyWeight(c candidate) int { return max(c.p.KeyWeight, 1) }
|
|
|
|
// resting says whether c rests now, as restLast will find.
|
|
func resting(c candidate) bool {
|
|
if _, ok := restOf(c.restKey()); ok {
|
|
return true
|
|
}
|
|
if c.restID() != c.restKey() {
|
|
_, ok := restOf(c.restID())
|
|
return ok
|
|
}
|
|
return false
|
|
}
|
|
|
|
// weightedFirst puts first the key the provider id's round picks now, the
|
|
// others after it in their order, for failover.
|
|
func weightedFirst(id string, cs []candidate) []candidate {
|
|
keys, weights, rests := make([]string, len(cs)), make([]int, len(cs)), make([]bool, len(cs))
|
|
for i, c := range cs {
|
|
keys[i], weights[i], rests[i] = c.rest, keyWeight(c), resting(c)
|
|
}
|
|
best := wrrPick(id, keys, weights, rests)
|
|
if best <= 0 {
|
|
return cs // first already, or all resting: restLast orders them
|
|
}
|
|
return append(append([]candidate{cs[best]}, cs[:best]...), cs[best+1:]...)
|
|
}
|
|
|
|
// wrrPick is which of keys round id picks now, each weighing its weight,
|
|
// those resting left out of the round; -1 when every one rests. The
|
|
// pick is charged the round's total.
|
|
func wrrPick(id string, keys []string, weights []int, rests []bool) int {
|
|
wrr.Lock()
|
|
defer wrr.Unlock()
|
|
r := wrr.m[id]
|
|
if r == nil {
|
|
r = &wrrRound{cur: map[string]int{}}
|
|
wrr.m[id] = r
|
|
}
|
|
// keys no longer on are dropped from the round
|
|
on := make(map[string]bool, len(keys))
|
|
for _, k := range keys {
|
|
on[k] = true
|
|
}
|
|
for k := range r.cur {
|
|
if !on[k] {
|
|
delete(r.cur, k)
|
|
}
|
|
}
|
|
best, total := -1, 0
|
|
for i, k := range keys {
|
|
if rests[i] {
|
|
continue
|
|
}
|
|
r.cur[k] += weights[i]
|
|
total += weights[i]
|
|
if best < 0 || r.cur[k] > r.cur[keys[best]] {
|
|
best = i
|
|
}
|
|
}
|
|
if best < 0 {
|
|
return -1
|
|
}
|
|
r.cur[keys[best]] -= total
|
|
r.total, r.last = total, keys[best]
|
|
return best
|
|
}
|
|
|
|
// weightedKept moves the pick of picked's round to kept, when affinity
|
|
// keeps a conversation with kept, another key of the same weighted
|
|
// provider, or another member of a group routed by weight that both are
|
|
// in: the round counts the requests each key, and each member, really
|
|
// took.
|
|
func weightedKept(picked, kept candidate) {
|
|
if picked.p.Routing == provider.Weighted && picked.p.ID == kept.p.ID && picked.rest != kept.rest {
|
|
wrrMove(picked.p.ID, picked.rest, kept.rest)
|
|
}
|
|
for _, s := range picked.seats {
|
|
for _, t := range kept.seats {
|
|
if s.round == t.round && s.member != t.member {
|
|
wrrMove(s.round, s.member, t.member)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
// wrrMove charges round id's pick to kept rather than picked.
|
|
func wrrMove(id, picked, kept string) {
|
|
wrr.Lock()
|
|
defer wrr.Unlock()
|
|
r := wrr.m[id]
|
|
if r == nil || r.last != picked {
|
|
return
|
|
}
|
|
if _, ok := r.cur[kept]; !ok {
|
|
return
|
|
}
|
|
r.cur[picked] += r.total
|
|
r.cur[kept] -= r.total
|
|
r.last = kept
|
|
}
|
|
|
|
// wrrSeat is a candidate's place in the round of a group routed by
|
|
// weight it is in: the round (the group's ref) and the member it is of.
|
|
type wrrSeat struct{ round, member string }
|
|
|
|
// weightedUnits orders the units of a group g routed by weight — each
|
|
// member, a group in it as one — by g's round over their members'
|
|
// weights: the one it picks first, the others after it in their order,
|
|
// for failover. heads are what each is weighed by: one resting is left
|
|
// out of the round.
|
|
func weightedUnits(g provider.Group, members []string, heads []candidate) []int {
|
|
weights, rests := make([]int, len(members)), make([]bool, len(members))
|
|
for i, m := range members {
|
|
weights[i], rests[i] = g.MemberWeight(m), resting(heads[i])
|
|
}
|
|
order := make([]int, 0, len(members))
|
|
best := wrrPick(provider.GroupPrefix+g.ID, members, weights, rests)
|
|
if best > 0 {
|
|
order = append(order, best)
|
|
}
|
|
for i := range members {
|
|
if i != best || best <= 0 {
|
|
order = append(order, i)
|
|
}
|
|
}
|
|
return order
|
|
}
|