mirror of https://github.com/v2ray/v2ray-core
better load balancing
parent
a353dcd681
commit
8595bce33b
|
@ -70,14 +70,27 @@ func (p *IncrementalWorkerPicker) cleanup() {
|
||||||
p.workers = activeWorkers
|
p.workers = activeWorkers
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (p *IncrementalWorkerPicker) findAvailable() int {
|
||||||
|
for idx, w := range p.workers {
|
||||||
|
if !w.IsFull() {
|
||||||
|
return idx
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return -1
|
||||||
|
}
|
||||||
|
|
||||||
func (p *IncrementalWorkerPicker) pickInternal() (*ClientWorker, error, bool) {
|
func (p *IncrementalWorkerPicker) pickInternal() (*ClientWorker, error, bool) {
|
||||||
p.access.Lock()
|
p.access.Lock()
|
||||||
defer p.access.Unlock()
|
defer p.access.Unlock()
|
||||||
|
|
||||||
for _, w := range p.workers {
|
idx := p.findAvailable()
|
||||||
if !w.IsFull() {
|
if idx >= 0 {
|
||||||
return w, nil, false
|
n := len(p.workers)
|
||||||
|
if n > 1 && idx != n-1 {
|
||||||
|
p.workers[n-1], p.workers[idx] = p.workers[idx], p.workers[n-1]
|
||||||
}
|
}
|
||||||
|
return p.workers[idx], nil, false
|
||||||
}
|
}
|
||||||
|
|
||||||
p.cleanup()
|
p.cleanup()
|
||||||
|
|
Loading…
Reference in New Issue