proxy: snapshot in-flight counts before sorting (least_conn selection O(n))
least_conn mirror selection stable-sorted the RR-rotated pool with a
comparator that called inflightCounter on every comparison. Each call did a
remoteName+"\x00"+url concat plus a sync.Map LoadOrStore with a speculative
new(atomic.Int64), so selection cost O(n log n) map lookups and allocations
per request on the cache-miss path.
Snapshot each mirror's in-flight count exactly once, then sort the snapshot by
plain int, making selection O(n) map loads with zero comparator allocations:
- Add read-only inflightCount(name, url) int64: plain sync.Map Load, returns 0
when the counter is absent (no LoadOrStore, no speculative allocation).
- least_conn branch builds a {url, count} snapshot via one inflightCount per
rotated URL, sort.SliceStable by count ascending, then extracts the URLs.
- beginAttempt/endAttempt keep the create-on-write inflightCounter path; they
legitimately need to create the gauge.
Behavior is unchanged: least-loaded first, RR rotation as the stable tie-break,
round_robin and single-URL paths untouched. Added a multi-mirror tie-break test
asserting all-equal load yields the RR rotation.
This commit is contained in:
@@ -760,13 +760,41 @@ func (e *Engine) baseURLAttemptOrder(remote models.Remote) []string {
|
||||
ordered[i] = urls[(start+i)%len(urls)]
|
||||
}
|
||||
if remote.MirrorStrategy == models.MirrorStrategyLeastConn {
|
||||
sort.SliceStable(ordered, func(a, b int) bool {
|
||||
return e.inflightCounter(remote.Name, ordered[a]).Load() < e.inflightCounter(remote.Name, ordered[b]).Load()
|
||||
// Snapshot each mirror's in-flight count once, then sort the snapshot.
|
||||
// Reading the gauge inside the comparator would repeat an allocating
|
||||
// sync.Map lookup on every comparison (O(n log n) lookups); this is O(n).
|
||||
snap := make([]inflightSnapshot, len(ordered))
|
||||
for i, url := range ordered {
|
||||
snap[i] = inflightSnapshot{url: url, count: e.inflightCount(remote.Name, url)}
|
||||
}
|
||||
sort.SliceStable(snap, func(a, b int) bool {
|
||||
return snap[a].count < snap[b].count
|
||||
})
|
||||
for i := range snap {
|
||||
ordered[i] = snap[i].url
|
||||
}
|
||||
}
|
||||
return ordered
|
||||
}
|
||||
|
||||
// inflightSnapshot pairs a mirror URL with its sampled in-flight count so the
|
||||
// least_conn sort compares plain ints instead of re-reading the gauge.
|
||||
type inflightSnapshot struct {
|
||||
url string
|
||||
count int64
|
||||
}
|
||||
|
||||
// inflightCount reads the in-flight request gauge for a given (remote, upstream
|
||||
// URL) without creating it, returning 0 when the counter is absent. This keeps
|
||||
// the selection read path allocation-free (plain Load, no LoadOrStore).
|
||||
func (e *Engine) inflightCount(remoteName, url string) int64 {
|
||||
v, ok := e.inflight.Load(remoteName + "\x00" + url)
|
||||
if !ok {
|
||||
return 0
|
||||
}
|
||||
return v.(*atomic.Int64).Load()
|
||||
}
|
||||
|
||||
// inflightCounter returns the shared in-flight request gauge for a given
|
||||
// (remote, upstream URL), creating it on first use.
|
||||
func (e *Engine) inflightCounter(remoteName, url string) *atomic.Int64 {
|
||||
|
||||
Reference in New Issue
Block a user