1
0
mirror of https://github.com/redis/node-redis.git synced 2025-08-04 15:02:09 +03:00

chore: improve multi performance by refactoring a array check away

This commit is contained in:
Ruben Bridgewater
2017-05-26 17:07:21 +02:00
parent f69aba72d1
commit a3a74559da
3 changed files with 25 additions and 26 deletions

View File

@@ -59,7 +59,7 @@ function multiCallback (multi, replies) {
i++
}
}
multi._client._multi = false
return replies
}
@@ -70,33 +70,36 @@ function multiCallback (multi, replies) {
* @returns Promise<any[]>
*/
function execTransaction (multi) {
if (multi.monitoring || multi._client.monitoring) {
const client = multi._client
const queue = multi._queue
if (multi.monitoring || client.monitoring) {
const err = new RangeError(
'Using transaction with a client that is in monitor mode does not work due to faulty return values of Redis.'
)
err.command = 'EXEC'
err.code = 'EXECABORT'
return new Promise((resolve, reject) => {
utils.replyInOrder(multi._client, (err, res) => {
utils.replyInOrder(client, (err, res) => {
if (err) return reject(err)
resolve(res)
}, null, [])
})
}
const len = multi._queue.length
const len = queue.length
multi.errors = []
multi._client.cork()
client.cork()
client._multi = true
multi.wantsBuffers = new Array(len)
// Silently ignore this error. We'll receive the error for the exec as well
const promises = [multi._client.internalSendCommand(new Command('multi', [])).catch(() => {})]
const promises = [client.internalSendCommand(new Command('multi', [])).catch(() => {})]
// Drain queue, callback will catch 'QUEUED' or error
for (var index = 0; index < len; index++) {
// The commands may not be shifted off, since they are needed in the result handler
promises.push(pipelineTransactionCommand(multi, multi._queue.get(index), index).catch((e) => e))
promises.push(pipelineTransactionCommand(multi, queue.get(index), index).catch((e) => e))
}
const main = multi._client.internalSendCommand(new Command('exec', []))
multi._client.uncork()
const main = client.internalSendCommand(new Command('exec', []))
client.uncork()
return Promise.all(promises).then(() => main.then((replies) => multiCallback(multi, replies)).catch((err) => {
err.errors = multi.errors
return Promise.reject(err)
@@ -110,25 +113,27 @@ function execTransaction (multi) {
* @returns Promise<any[]>
*/
function execBatch (multi) {
if (multi._queue.length === 0) {
const client = multi._client
const queue = multi._queue
if (queue.length === 0) {
// TODO: return an error if not "ready"
return new Promise((resolve) => {
utils.replyInOrder(multi._client, (e, res) => {
utils.replyInOrder(client, (e, res) => {
resolve(res)
}, null, [])
})
}
var error = false
multi._client.cork()
client.cork()
const promises = []
while (multi._queue.length) {
const commandObj = multi._queue.shift()
promises.push(multi._client.internalSendCommand(commandObj).catch((e) => {
while (queue.length) {
const command = queue.shift()
promises.push(client.internalSendCommand(command).catch((e) => {
error = true
return e
}))
}
multi._client.uncork()
client.uncork()
return Promise.all(promises).then((res) => {
if (error) {
const err = new Error('bla failed')