backpressure_drain.js 810 B

123456789101112131415161718192021222324252627282930313233
  1. var redis = require("../index"),
  2. client = redis.createClient(null, null, {
  3. command_queue_high_water: 5,
  4. command_queue_low_water: 1
  5. }),
  6. remaining_ops = 100000, paused = false;
  7. function op() {
  8. if (remaining_ops <= 0) {
  9. console.error("Finished.");
  10. process.exit(0);
  11. }
  12. remaining_ops--;
  13. if (client.hset("test hash", "val " + remaining_ops, remaining_ops) === false) {
  14. console.log("Pausing at " + remaining_ops);
  15. paused = true;
  16. } else {
  17. process.nextTick(op);
  18. }
  19. }
  20. client.on("drain", function () {
  21. if (paused) {
  22. console.log("Resuming at " + remaining_ops);
  23. paused = false;
  24. process.nextTick(op);
  25. } else {
  26. console.log("Got drain while not paused at " + remaining_ops);
  27. }
  28. });
  29. op();