You've already forked node-redis
mirror of
https://github.com/redis/node-redis.git
synced 2025-08-06 02:15:48 +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
|
||||
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.
|
||||
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
|
||||
want to do this:
|
||||
@@ -260,12 +261,15 @@ want to do this:
|
||||
var redis = require("redis"),
|
||||
client = redis.createClient();
|
||||
|
||||
client.set("foo_rand000000000000", "some fantastic value");
|
||||
client.end(); // No further commands will be processed
|
||||
client.get("foo_rand000000000000", function (err, reply) {
|
||||
// This won't be called anymore, since flush has not been set to true!
|
||||
client.set("foo_rand000000000000", "some fantastic value", function (err, reply) {
|
||||
// This will either result in an error (flush parameter is set to true)
|
||||
// or will silently fail and this callback will not be called at all (flush set to false)
|
||||
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!
|
||||
|
18
index.js
18
index.js
@@ -550,7 +550,7 @@ RedisClient.prototype.connection_gone = function (why) {
|
||||
error.code = 'CONNECTION_BROKEN';
|
||||
this.flush_and_error(error);
|
||||
this.emit('error', error);
|
||||
this.end();
|
||||
this.end(false);
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -900,6 +900,17 @@ RedisClient.prototype.pub_sub_command = function (command_obj) {
|
||||
};
|
||||
|
||||
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 = {};
|
||||
|
||||
// Clear retry_timer
|
||||
@@ -909,11 +920,6 @@ RedisClient.prototype.end = function (flush) {
|
||||
}
|
||||
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.ready = false;
|
||||
this.closing = true;
|
||||
|
Reference in New Issue
Block a user