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

chore: add callback functionality back in

This also improves the performance for multi / batch commands a lot.
The reason is that now there are only callbacks internally even if
a promise is going to be returned in the end.
This commit is contained in:
Ruben Bridgewater
2017-11-29 19:16:40 -02:00
parent 2b4ab10305
commit d7c31da598
10 changed files with 288 additions and 184 deletions

View File

@@ -1,6 +1,6 @@
'use strict'
const Command = require('./command')
const { Command, MultiCommand } = require('./command')
const debug = require('./debug')
const Multi = require('./multi')
const utils = require('./utils')
@@ -29,16 +29,20 @@ function selectCallback(client, db) {
// Store db in this.selectDb to restore it on reconnect
client.selectedDb = db
}
return err || res
return [err, res]
}
}
RedisClient.prototype.select = function select(db) {
return this.internalSendCommand(new Command('select', [db], null, selectCallback(this, db)))
const command = new Command('select', [db])
command.transformer = selectCallback(this, db)
return this.internalSendCommand(command)
}
Multi.prototype.select = function select(db) {
this._queue.push(new Command('select', [db], null, selectCallback(this._client, db)))
const command = new MultiCommand('select', [db])
command.transformer = selectCallback(this._client, db)
this._queue.push(command)
return this
}
@@ -51,7 +55,9 @@ RedisClient.prototype.monitor = function monitor() {
// be properly processed. If this is not the case, it's not an issue either.
this._monitoring = true
}
return this.internalSendCommand(new Command('monitor', [], callOnWrite))
const command = new Command('monitor', [])
command.callOnWrite = callOnWrite
return this.internalSendCommand(command)
}
// Only works with batch, not in a transaction
@@ -62,7 +68,9 @@ Multi.prototype.monitor = function monitor() {
const callOnWrite = () => {
this._client._monitoring = true
}
this._queue.push(new Command('monitor', [], callOnWrite))
const command = new MultiCommand('monitor', [])
command.callOnWrite = callOnWrite
this._queue.push(command)
return this
}
// Set multi monitoring to indicate the exec that it should abort
@@ -84,9 +92,9 @@ function quitCallback(client) {
// or the offline queue is deactivated and the command was rejected right away
// or the stream is not writable
// or while sending the quit, the connection ended / closed
return 'OK'
return [null, 'OK']
}
return err || res
return [err, res]
}
}
@@ -95,7 +103,9 @@ RedisClient.prototype.quit = function quit() {
//
// Allow the quit command to be fired as soon as possible to prevent it
// landing in the offline queue. this.ready = this.offlineQueue.length === 0;
const backpressureIndicator = this.internalSendCommand(new Command('quit', [], null, quitCallback(this)))
const command = new Command('quit', [])
command.transformer = quitCallback(this)
const backpressureIndicator = this.internalSendCommand(command)
// Calling quit should always end the connection, no matter if there's a connection or not
this._closing = true
this.ready = false
@@ -109,7 +119,10 @@ Multi.prototype.quit = function quit() {
this._client._closing = true
this._client.ready = false
}
this._queue.push(new Command('quit', [], callOnWrite, quitCallback(this._client)))
const command = new MultiCommand('quit', [])
command.callOnWrite = callOnWrite
command.transformer = quitCallback(this._client)
this._queue.push(command)
return this
}
@@ -122,7 +135,7 @@ Multi.prototype.quit = function quit() {
function infoCallback(client) {
return function (err, res) {
if (err) {
return err
return [err, undefined]
}
if (typeof res !== 'string') {
@@ -171,32 +184,36 @@ function infoCallback(client) {
}
client.serverInfo = obj
return res
return [null, res]
}
}
// Store info in this.serverInfo after each call
RedisClient.prototype.info = function info(section) {
const args = section ? [section] : []
return this.internalSendCommand(new Command('info', args, null, infoCallback(this)))
const command = new Command('info', args)
command.transformer = infoCallback(this)
return this.internalSendCommand(command)
}
Multi.prototype.info = function info(section) {
const args = section ? [section] : []
this._queue.push(new Command('info', args, null, infoCallback(this._client)))
const command = new MultiCommand('info', args)
command.transformer = infoCallback(this._client)
this._queue.push(command)
return this
}
function authCallback(client, pass) {
function authCallback(client) {
return function (err, res) {
if (err) {
if (noPasswordIsSet.test(err.message)) {
utils.warn(client, 'Warning: Redis server does not require a password, but a password was supplied.')
return 'OK'
return [null, 'OK']
}
return err
return [err, undefined]
}
return res
return [null, res]
}
}
@@ -207,7 +224,9 @@ RedisClient.prototype.auth = function auth(pass) {
this._options.password = pass
const ready = this.ready
this.ready = ready || this.offlineQueue.length === 0
const tmp = this.internalSendCommand(new Command('auth', [pass], null, authCallback(this, pass)))
const command = new Command('auth', [pass])
command.transformer = authCallback(this)
const tmp = this.internalSendCommand(command)
this.ready = ready
return tmp
}
@@ -218,7 +237,9 @@ Multi.prototype.auth = function auth(pass) {
// Stash auth for connect and reconnect.
this._client._options.password = pass
this._queue.push(new Command('auth', [pass], null, authCallback(this._client)))
const command = new MultiCommand('auth', [pass])
command.transformer = authCallback(this._client)
this._queue.push(command)
return this
}
@@ -233,7 +254,9 @@ RedisClient.prototype.client = function client(...arr) {
}
}
}
return this.internalSendCommand(new Command('client', arr, callOnWrite))
const command = new Command('client', arr)
command.callOnWrite = callOnWrite
return this.internalSendCommand(command)
}
Multi.prototype.client = function client(...arr) {
@@ -247,7 +270,9 @@ Multi.prototype.client = function client(...arr) {
}
}
}
this._queue.push(new Command('client', arr, callOnWrite))
const command = new MultiCommand('client', arr)
command.callOnWrite = callOnWrite
this._queue.push(command)
return this
}
@@ -255,14 +280,18 @@ RedisClient.prototype.subscribe = function subscribe(...arr) {
const callOnWrite = () => {
this._pubSubMode = this._pubSubMode || this.commandQueue.length + 1
}
return this.internalSendCommand(new Command('subscribe', arr, callOnWrite))
const command = new Command('subscribe', arr)
command.callOnWrite = callOnWrite
return this.internalSendCommand(command)
}
Multi.prototype.subscribe = function subscribe(...arr) {
const callOnWrite = () => {
this._client._pubSubMode = this._client._pubSubMode || this._client.commandQueue.length + 1
}
this._queue.push(new Command('subscribe', arr, callOnWrite))
const command = new MultiCommand('subscribe', arr)
command.callOnWrite = callOnWrite
this._queue.push(command)
return this
}
@@ -272,7 +301,9 @@ RedisClient.prototype.unsubscribe = function unsubscribe(...arr) {
// value is manipulated in the callback
this._pubSubMode = this._pubSubMode || this.commandQueue.length + 1
}
return this.internalSendCommand(new Command('unsubscribe', arr, callOnWrite))
const command = new Command('unsubscribe', arr)
command.callOnWrite = callOnWrite
return this.internalSendCommand(command)
}
Multi.prototype.unsubscribe = function unsubscribe(...arr) {
@@ -281,7 +312,9 @@ Multi.prototype.unsubscribe = function unsubscribe(...arr) {
// value is manipulated in the callback
this._client._pubSubMode = this._client._pubSubMode || this._client.commandQueue.length + 1
}
this._queue.push(new Command('unsubscribe', arr, callOnWrite))
const command = new MultiCommand('unsubscribe', arr)
command.callOnWrite = callOnWrite
this._queue.push(command)
return this
}
@@ -289,14 +322,18 @@ RedisClient.prototype.psubscribe = function psubscribe(...arr) {
const callOnWrite = () => {
this._pubSubMode = this._pubSubMode || this.commandQueue.length + 1
}
return this.internalSendCommand(new Command('psubscribe', arr, callOnWrite))
const command = new Command('psubscribe', arr)
command.callOnWrite = callOnWrite
return this.internalSendCommand(command)
}
Multi.prototype.psubscribe = function psubscribe(...arr) {
const callOnWrite = () => {
this._client._pubSubMode = this._client._pubSubMode || this._client.commandQueue.length + 1
}
this._queue.push(new Command('psubscribe', arr, callOnWrite))
const command = new MultiCommand('psubscribe', arr)
command.callOnWrite = callOnWrite
this._queue.push(command)
return this
}
@@ -306,7 +343,9 @@ RedisClient.prototype.punsubscribe = function punsubscribe(...arr) {
// value is manipulated in the callback
this._pubSubMode = this._pubSubMode || this.commandQueue.length + 1
}
return this.internalSendCommand(new Command('punsubscribe', arr, callOnWrite))
const command = new Command('punsubscribe', arr)
command.callOnWrite = callOnWrite
return this.internalSendCommand(command)
}
Multi.prototype.punsubscribe = function punsubscribe(...arr) {
@@ -315,6 +354,8 @@ Multi.prototype.punsubscribe = function punsubscribe(...arr) {
// value is manipulated in the callback
this._client._pubSubMode = this._client._pubSubMode || this._client.commandQueue.length + 1
}
this._queue.push(new Command('punsubscribe', arr, callOnWrite))
const command = new MultiCommand('punsubscribe', arr)
command.callOnWrite = callOnWrite
this._queue.push(command)
return this
}