1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-09-25 21:36:13 -04:00

better load balancing

This commit is contained in:
Darien Raymond 2018-10-26 12:06:21 +02:00
parent a353dcd681
commit 8595bce33b
No known key found for this signature in database
GPG Key ID: 7251FFA14BB18169

View File

@ -70,14 +70,27 @@ func (p *IncrementalWorkerPicker) cleanup() {
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) {
p.access.Lock()
defer p.access.Unlock()
for _, w := range p.workers {
if !w.IsFull() {
return w, nil, false
idx := p.findAvailable()
if idx >= 0 {
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()