events.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. 'use strict';
  2. /**
  3. * The base class for all monitoring events published from the connection pool
  4. *
  5. * @property {number} time A timestamp when the event was created
  6. * @property {string} address The address (host/port pair) of the pool
  7. */
  8. class ConnectionPoolMonitoringEvent {
  9. constructor(pool) {
  10. this.time = new Date();
  11. this.address = pool.address;
  12. }
  13. }
  14. /**
  15. * An event published when a connection pool is created
  16. *
  17. * @property {Object} options The options used to create this connection pool
  18. */
  19. class ConnectionPoolCreatedEvent extends ConnectionPoolMonitoringEvent {
  20. constructor(pool) {
  21. super(pool);
  22. this.options = pool.options;
  23. }
  24. }
  25. /**
  26. * An event published when a connection pool is closed
  27. */
  28. class ConnectionPoolClosedEvent extends ConnectionPoolMonitoringEvent {
  29. constructor(pool) {
  30. super(pool);
  31. }
  32. }
  33. /**
  34. * An event published when a connection pool creates a new connection
  35. *
  36. * @property {number} connectionId A monotonically increasing, per-pool id for the newly created connection
  37. */
  38. class ConnectionCreatedEvent extends ConnectionPoolMonitoringEvent {
  39. constructor(pool, connection) {
  40. super(pool);
  41. this.connectionId = connection.id;
  42. }
  43. }
  44. /**
  45. * An event published when a connection is ready for use
  46. *
  47. * @property {number} connectionId The id of the connection
  48. */
  49. class ConnectionReadyEvent extends ConnectionPoolMonitoringEvent {
  50. constructor(pool, connection) {
  51. super(pool);
  52. this.connectionId = connection.id;
  53. }
  54. }
  55. /**
  56. * An event published when a connection is closed
  57. *
  58. * @property {number} connectionId The id of the connection
  59. * @property {string} reason The reason the connection was closed
  60. */
  61. class ConnectionClosedEvent extends ConnectionPoolMonitoringEvent {
  62. constructor(pool, connection, reason) {
  63. super(pool);
  64. this.connectionId = connection.id;
  65. this.reason = reason || 'unknown';
  66. }
  67. }
  68. /**
  69. * An event published when a request to check a connection out begins
  70. */
  71. class ConnectionCheckOutStartedEvent extends ConnectionPoolMonitoringEvent {
  72. constructor(pool) {
  73. super(pool);
  74. }
  75. }
  76. /**
  77. * An event published when a request to check a connection out fails
  78. *
  79. * @property {string} reason The reason the attempt to check out failed
  80. */
  81. class ConnectionCheckOutFailedEvent extends ConnectionPoolMonitoringEvent {
  82. constructor(pool, reason) {
  83. super(pool);
  84. this.reason = reason;
  85. }
  86. }
  87. /**
  88. * An event published when a connection is checked out of the connection pool
  89. *
  90. * @property {number} connectionId The id of the connection
  91. */
  92. class ConnectionCheckedOutEvent extends ConnectionPoolMonitoringEvent {
  93. constructor(pool, connection) {
  94. super(pool);
  95. this.connectionId = connection.id;
  96. }
  97. }
  98. /**
  99. * An event published when a connection is checked into the connection pool
  100. *
  101. * @property {number} connectionId The id of the connection
  102. */
  103. class ConnectionCheckedInEvent extends ConnectionPoolMonitoringEvent {
  104. constructor(pool, connection) {
  105. super(pool);
  106. this.connectionId = connection.id;
  107. }
  108. }
  109. /**
  110. * An event published when a connection pool is cleared
  111. */
  112. class ConnectionPoolClearedEvent extends ConnectionPoolMonitoringEvent {
  113. constructor(pool) {
  114. super(pool);
  115. }
  116. }
  117. const CMAP_EVENT_NAMES = [
  118. 'connectionPoolCreated',
  119. 'connectionPoolClosed',
  120. 'connectionCreated',
  121. 'connectionReady',
  122. 'connectionClosed',
  123. 'connectionCheckOutStarted',
  124. 'connectionCheckOutFailed',
  125. 'connectionCheckedOut',
  126. 'connectionCheckedIn',
  127. 'connectionPoolCleared'
  128. ];
  129. module.exports = {
  130. CMAP_EVENT_NAMES,
  131. ConnectionPoolCreatedEvent,
  132. ConnectionPoolClosedEvent,
  133. ConnectionCreatedEvent,
  134. ConnectionReadyEvent,
  135. ConnectionClosedEvent,
  136. ConnectionCheckOutStartedEvent,
  137. ConnectionCheckOutFailedEvent,
  138. ConnectionCheckedOutEvent,
  139. ConnectionCheckedInEvent,
  140. ConnectionPoolClearedEvent
  141. };