Skip to content

Commit 8595bce

Browse files
committed
better load balancing
1 parent a353dcd commit 8595bce

File tree

1 file changed

+16
-3
lines changed

1 file changed

+16
-3
lines changed

common/mux/client.go

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -70,14 +70,27 @@ func (p *IncrementalWorkerPicker) cleanup() {
7070
p.workers = activeWorkers
7171
}
7272

73+
func (p *IncrementalWorkerPicker) findAvailable() int {
74+
for idx, w := range p.workers {
75+
if !w.IsFull() {
76+
return idx
77+
}
78+
}
79+
80+
return -1
81+
}
82+
7383
func (p *IncrementalWorkerPicker) pickInternal() (*ClientWorker, error, bool) {
7484
p.access.Lock()
7585
defer p.access.Unlock()
7686

77-
for _, w := range p.workers {
78-
if !w.IsFull() {
79-
return w, nil, false
87+
idx := p.findAvailable()
88+
if idx >= 0 {
89+
n := len(p.workers)
90+
if n > 1 && idx != n-1 {
91+
p.workers[n-1], p.workers[idx] = p.workers[idx], p.workers[n-1]
8092
}
93+
return p.workers[idx], nil, false
8194
}
8295

8396
p.cleanup()

0 commit comments

Comments
 (0)