diff --git a/README.md b/README.md index 7ecaaaaad3..597f817ebf 100644 --- a/README.md +++ b/README.md @@ -154,11 +154,11 @@ So please attach the error listener to node_redis. `client` will emit `drain` when the TCP connection to the Redis server has been buffering, but is now writable. This event can be used to stream commands in to Redis and adapt to backpressure. -All commands return a boolean if the stream had to buffer or not. If false is returned the stream had to buffer. +If the stream is buffering `client.should_buffer` is set to true. Otherwise the variable is always set to false. That way you can decide when to reduce your send rate and resume sending commands when you get `drain`. -You can manually control the low water and high water marks by passing ommand_queue_high_water` and `command_queue_low_water` to the client options. -Check the [Node.js streams API](https://nodejs.org/api/stream.html) for further info. +You can also check the return value of each command as it will also return the backpressure indicator. +If false is returned the stream had to buffer. ### "idle" diff --git a/changelog.md b/changelog.md index a77ba9082c..533088d624 100644 --- a/changelog.md +++ b/changelog.md @@ -10,6 +10,9 @@ Features - using .multi / .batch (up to +50% / on Node.js 0.10.x +300%) - saving small buffers - Increased coverage to 99% ([@BridgeAR](https://github.com/BridgeAR)) +- Refactored manual backpressure control ([@BridgeAR](https://github.com/BridgeAR)) + - Removed the high water mark and low water mark. Such a mechanism should be implemented by a user instead + - The `drain` event is from now on only emitted if the stream really had to buffer Bugfixes diff --git a/examples/backpressure_drain.js b/examples/backpressure_drain.js index 12cd24107a..f9ab961c33 100644 --- a/examples/backpressure_drain.js +++ b/examples/backpressure_drain.js @@ -1,10 +1,7 @@ 'use strict'; var redis = require('../index'), - client = redis.createClient(null, null, { - command_queue_high_water: 5, - command_queue_low_water: 1 - }), + client = redis.createClient(), remaining_ops = 100000, paused = false; function op() { @@ -14,11 +11,12 @@ function op() { } remaining_ops--; - if (client.hset('test hash', 'val ' + remaining_ops, remaining_ops) === false) { + client.hset('test hash', 'val ' + remaining_ops, remaining_ops); + if (client.should_buffer === true) { console.log('Pausing at ' + remaining_ops); paused = true; } else { - process.nextTick(op); + setTimeout(op, 1); } } diff --git a/examples/pub_sub.js b/examples/pub_sub.js index 006c730839..8d691126da 100644 --- a/examples/pub_sub.js +++ b/examples/pub_sub.js @@ -4,7 +4,7 @@ var redis = require('redis'), client1 = redis.createClient(), msg_count = 0, client2 = redis.createClient(); -// Most clients probably don't do much on 'subscribe'. This example uses it to coordinate things within one program. +// Most clients probably don't do much on 'subscribe'. This example uses it to coordinate things within one program. client1.on('subscribe', function (channel, count) { console.log('client1 subscribed to ' + channel + ', ' + count + ' total subscriptions'); if (count === 2) { diff --git a/index.js b/index.js index 197ad54282..d7bd5245f5 100644 --- a/index.js +++ b/index.js @@ -82,8 +82,6 @@ function RedisClient(stream, options) { options.detect_buffers = false; } this.should_buffer = false; - this.command_queue_high_water = +options.command_queue_high_water || 1000; - this.command_queue_low_water = options.command_queue_low_water | 0; this.max_attempts = options.max_attempts | 0; this.command_queue = new Queue(); // Holds sent commands to de-pipeline them this.offline_queue = new Queue(); // Holds commands issued but not able to be sent diff --git a/test/commands/keys.spec.js b/test/commands/keys.spec.js index 6437aeb0a8..5cb8422805 100644 --- a/test/commands/keys.spec.js +++ b/test/commands/keys.spec.js @@ -15,8 +15,6 @@ describe("The 'keys' method", function () { beforeEach(function (done) { args = args || {}; - // This is going to test if the high water is also respected - args.command_queue_high_water = 100; client = redis.createClient.apply(redis.createClient, args); client.once("ready", function () { client.flushdb(done);