mirror of https://github.com/panjf2000/ants.git
opt: leverage binary-search algorithm to speed up PoolWithFunc.purgeStaleWorkers()
This commit is contained in:
parent
7b1e246b0e
commit
3fbd9567c9
|
@ -40,7 +40,7 @@ jobs:
|
||||||
uses: golangci/golangci-lint-action@v3
|
uses: golangci/golangci-lint-action@v3
|
||||||
with:
|
with:
|
||||||
version: v1.50.1
|
version: v1.50.1
|
||||||
args: -v -E gofumpt -E gocritic -E misspell -E revive -E godot
|
args: --timeout 5m -v -E gofumpt -E gocritic -E misspell -E revive -E godot
|
||||||
test:
|
test:
|
||||||
needs: lint
|
needs: lint
|
||||||
strategy:
|
strategy:
|
||||||
|
|
16
pool_func.go
16
pool_func.go
|
@ -89,17 +89,25 @@ func (p *PoolWithFunc) purgeStaleWorkers(ctx context.Context) {
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
|
|
||||||
currentTime := time.Now()
|
criticalTime := time.Now().Add(-p.options.ExpiryDuration)
|
||||||
|
|
||||||
p.lock.Lock()
|
p.lock.Lock()
|
||||||
idleWorkers := p.workers
|
idleWorkers := p.workers
|
||||||
n := len(idleWorkers)
|
n := len(idleWorkers)
|
||||||
var i int
|
l, r, mid := 0, n-1, 0
|
||||||
for i = 0; i < n && currentTime.Sub(idleWorkers[i].recycleTime) > p.options.ExpiryDuration; i++ {
|
for l <= r {
|
||||||
|
mid = (l + r) / 2
|
||||||
|
if criticalTime.Before(idleWorkers[mid].recycleTime) {
|
||||||
|
r = mid - 1
|
||||||
|
} else {
|
||||||
|
l = mid + 1
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
i := r + 1
|
||||||
expiredWorkers = append(expiredWorkers[:0], idleWorkers[:i]...)
|
expiredWorkers = append(expiredWorkers[:0], idleWorkers[:i]...)
|
||||||
if i > 0 {
|
if i > 0 {
|
||||||
m := copy(idleWorkers, idleWorkers[i:])
|
m := copy(idleWorkers, idleWorkers[i:])
|
||||||
for i = m; i < n; i++ {
|
for i := m; i < n; i++ {
|
||||||
idleWorkers[i] = nil
|
idleWorkers[i] = nil
|
||||||
}
|
}
|
||||||
p.workers = idleWorkers[:m]
|
p.workers = idleWorkers[:m]
|
||||||
|
|
Loading…
Reference in New Issue