You've already forked node-redis
mirror of
https://github.com/redis/node-redis.git
synced 2025-08-07 13:22:56 +03:00
Deprecate .end() by making the flush parameter mandatory and fix the docs
This commit is contained in:
16
README.md
16
README.md
@@ -246,12 +246,13 @@ NOTE: Your call to `client.auth()` should not be inside the ready handler. If
|
|||||||
you are doing this wrong, `client` will emit an error that looks
|
you are doing this wrong, `client` will emit an error that looks
|
||||||
something like this `Error: Ready check failed: ERR operation not permitted`.
|
something like this `Error: Ready check failed: ERR operation not permitted`.
|
||||||
|
|
||||||
## client.end([flush])
|
## client.end(flush)
|
||||||
|
|
||||||
Forcibly close the connection to the Redis server. Note that this does not wait until all replies have been parsed.
|
Forcibly close the connection to the Redis server. Note that this does not wait until all replies have been parsed.
|
||||||
If you want to exit cleanly, call `client.quit()` to send the `QUIT` command after you have handled all replies.
|
If you want to exit cleanly, call `client.quit()` to send the `QUIT` command after you have handled all replies.
|
||||||
|
|
||||||
If flush is set to true, all still running commands will be rejected instead of ignored after using `.end`.
|
You should set flush to true, if you are not absolutly sure you do not care about any other commands.
|
||||||
|
If you set flush to false all still running commands will silently fail.
|
||||||
|
|
||||||
This example closes the connection to the Redis server before the replies have been read. You probably don't
|
This example closes the connection to the Redis server before the replies have been read. You probably don't
|
||||||
want to do this:
|
want to do this:
|
||||||
@@ -260,12 +261,15 @@ want to do this:
|
|||||||
var redis = require("redis"),
|
var redis = require("redis"),
|
||||||
client = redis.createClient();
|
client = redis.createClient();
|
||||||
|
|
||||||
client.set("foo_rand000000000000", "some fantastic value");
|
client.set("foo_rand000000000000", "some fantastic value", function (err, reply) {
|
||||||
client.end(); // No further commands will be processed
|
// This will either result in an error (flush parameter is set to true)
|
||||||
client.get("foo_rand000000000000", function (err, reply) {
|
// or will silently fail and this callback will not be called at all (flush set to false)
|
||||||
// This won't be called anymore, since flush has not been set to true!
|
|
||||||
console.log(err);
|
console.log(err);
|
||||||
});
|
});
|
||||||
|
client.end(true); // No further commands will be processed
|
||||||
|
client.get("foo_rand000000000000", function (err, reply) {
|
||||||
|
console.log(err); // => 'The connection has already been closed.'
|
||||||
|
});
|
||||||
```
|
```
|
||||||
|
|
||||||
`client.end()` without the flush parameter should not be used in production!
|
`client.end()` without the flush parameter should not be used in production!
|
||||||
|
18
index.js
18
index.js
@@ -550,7 +550,7 @@ RedisClient.prototype.connection_gone = function (why) {
|
|||||||
error.code = 'CONNECTION_BROKEN';
|
error.code = 'CONNECTION_BROKEN';
|
||||||
this.flush_and_error(error);
|
this.flush_and_error(error);
|
||||||
this.emit('error', error);
|
this.emit('error', error);
|
||||||
this.end();
|
this.end(false);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -900,6 +900,17 @@ RedisClient.prototype.pub_sub_command = function (command_obj) {
|
|||||||
};
|
};
|
||||||
|
|
||||||
RedisClient.prototype.end = function (flush) {
|
RedisClient.prototype.end = function (flush) {
|
||||||
|
// Flush queue if wanted
|
||||||
|
if (flush) {
|
||||||
|
this.flush_and_error(new Error("The command can't be processed. The connection has already been closed."));
|
||||||
|
} else if (flush === undefined) {
|
||||||
|
console.warn(
|
||||||
|
'node_redis: Using .end() without the flush parameter is deprecated. ' +
|
||||||
|
'Please check the doku (https://github.com/NodeRedis/node_redis) and explictly use flush.\n' +
|
||||||
|
'This will throw from v.3.0.0 on.'
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
this.stream._events = {};
|
this.stream._events = {};
|
||||||
|
|
||||||
// Clear retry_timer
|
// Clear retry_timer
|
||||||
@@ -909,11 +920,6 @@ RedisClient.prototype.end = function (flush) {
|
|||||||
}
|
}
|
||||||
this.stream.on('error', noop);
|
this.stream.on('error', noop);
|
||||||
|
|
||||||
// Flush queue if wanted
|
|
||||||
if (flush) {
|
|
||||||
this.flush_and_error(new Error("The command can't be processed. The connection has already been closed."));
|
|
||||||
}
|
|
||||||
|
|
||||||
this.connected = false;
|
this.connected = false;
|
||||||
this.ready = false;
|
this.ready = false;
|
||||||
this.closing = true;
|
this.closing = true;
|
||||||
|
Reference in New Issue
Block a user