heartbeat.js 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. //
  2. //
  3. //
  4. // Heartbeats. In AMQP both clients and servers may expect a heartbeat
  5. // frame if there is no activity on the connection for a negotiated
  6. // period of time. If there's no activity for two such intervals, the
  7. // server or client is allowed to close the connection on the
  8. // presumption that the other party is dead.
  9. //
  10. // The client has two jobs here: the first is to send a heartbeat
  11. // frame if it's not sent any frames for a while, so that the server
  12. // doesn't think it's dead; the second is to check periodically that
  13. // it's seen activity from the server, and to advise if there doesn't
  14. // appear to have been any for over two intervals.
  15. //
  16. // Node.JS timers are a bit unreliable, in that they endeavour only to
  17. // fire at some indeterminate point *after* the given time (rather
  18. // gives the lie to 'realtime', dunnit). Because the scheduler is just
  19. // an event loop, it's quite easy to delay timers indefinitely by
  20. // reacting to some I/O with a lot of computation.
  21. //
  22. // To mitigate this I need a bit of creative interpretation:
  23. //
  24. // - I'll schedule a server activity check for every `interval`, and
  25. // check just how much time has passed. It will overshoot by at
  26. // least a small margin; modulo missing timer deadlines, it'll
  27. // notice between two and three intervals after activity actually
  28. // stops (otherwise, at some point after two intervals).
  29. //
  30. // - Every `interval / 2` I'll check that we've sent something since
  31. // the last check, and if not, send a heartbeat frame. If we're
  32. // really too busy to even run the check for two whole heartbeat
  33. // intervals, there must be a lot of I (but not O, at least not on
  34. // the connection), or computation, in which case perhaps it's best
  35. // the server cuts us off anyway. Why `interval / 2`? Because the
  36. // edge case is that the client sent a frame just after a
  37. // heartbeat, which would mean I only send one after almost two
  38. // intervals. (NB a heartbeat counts as a send, so it'll be checked
  39. // at least twice before sending another)
  40. //
  41. // This design is based largely on RabbitMQ's heartbeating:
  42. // https://github.com/rabbitmq/rabbitmq-common/blob/master/src/rabbit_heartbeat.erl
  43. // %% Yes, I could apply the same 'actually passage of time' thing to
  44. // %% send as well as to recv.
  45. 'use strict';
  46. var EventEmitter = require('events');
  47. // Exported so that we can mess with it in tests
  48. module.exports.UNITS_TO_MS = 1000;
  49. class Heart extends EventEmitter {
  50. constructor (interval, checkSend, checkRecv) {
  51. super();
  52. this.interval = interval;
  53. var intervalMs = interval * module.exports.UNITS_TO_MS;
  54. // Function#bind is my new best friend
  55. var beat = this.emit.bind(this, 'beat');
  56. var timeout = this.emit.bind(this, 'timeout');
  57. this.sendTimer = setInterval(
  58. this.runHeartbeat.bind(this, checkSend, beat), intervalMs / 2);
  59. // A timeout occurs if I see nothing for *two consecutive* intervals
  60. var recvMissed = 0;
  61. function missedTwo () {
  62. if (!checkRecv())
  63. return (++recvMissed < 2);
  64. else { recvMissed = 0; return true; }
  65. }
  66. this.recvTimer = setInterval(
  67. this.runHeartbeat.bind(this, missedTwo, timeout), intervalMs);
  68. }
  69. clear () {
  70. clearInterval(this.sendTimer);
  71. clearInterval(this.recvTimer);
  72. }
  73. runHeartbeat (check, fail) {
  74. // Have we seen activity?
  75. if (!check())
  76. fail();
  77. }
  78. }
  79. module.exports.Heart = Heart;