Improve helper concurrency (#1214)

This commit is contained in:
Tomas Della Vedova
2020-06-04 10:55:51 +02:00
committed by GitHub
parent 3751f50774
commit 7dfaa6c5b4
4 changed files with 92 additions and 90 deletions

View File

@ -301,6 +301,7 @@ class Helpers {
function semaphore () {
if (running < concurrency) {
running += 1
return pImmediate(send)
} else {
return new Promise((resolve, reject) => {
@ -311,13 +312,13 @@ class Helpers {
function send (msearchBody, callbacks) {
/* istanbul ignore if */
if (running >= concurrency) {
if (running > concurrency) {
throw new Error('Max concurrency reached')
}
running += 1
msearchOperation(msearchBody, callbacks, () => {
running -= 1
if (resolveSemaphore) {
running += 1
resolveSemaphore(send)
resolveSemaphore = null
} else if (resolveFinish && running === 0) {
@ -575,6 +576,7 @@ class Helpers {
function semaphore () {
if (running < concurrency) {
running += 1
return pImmediate(send)
} else {
return new Promise((resolve, reject) => {
@ -585,10 +587,9 @@ class Helpers {
function send (bulkBody) {
/* istanbul ignore if */
if (running >= concurrency) {
if (running > concurrency) {
throw new Error('Max concurrency reached')
}
running += 1
bulkOperation(bulkBody, err => {
running -= 1
if (err) {
@ -596,6 +597,7 @@ class Helpers {
error = err
}
if (resolveSemaphore) {
running += 1
resolveSemaphore(send)
resolveSemaphore = null
} else if (resolveFinish && running === 0) {