pool-base.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. 'use strict'
  2. const DispatcherBase = require('./dispatcher-base')
  3. const FixedQueue = require('./node/fixed-queue')
  4. const { kConnected, kSize, kRunning, kPending, kQueued, kBusy, kFree, kUrl, kClose, kDestroy, kDispatch } = require('./core/symbols')
  5. const PoolStats = require('./pool-stats')
  6. const kClients = Symbol('clients')
  7. const kNeedDrain = Symbol('needDrain')
  8. const kQueue = Symbol('queue')
  9. const kClosedResolve = Symbol('closed resolve')
  10. const kOnDrain = Symbol('onDrain')
  11. const kOnConnect = Symbol('onConnect')
  12. const kOnDisconnect = Symbol('onDisconnect')
  13. const kOnConnectionError = Symbol('onConnectionError')
  14. const kGetDispatcher = Symbol('get dispatcher')
  15. const kAddClient = Symbol('add client')
  16. const kRemoveClient = Symbol('remove client')
  17. const kStats = Symbol('stats')
  18. class PoolBase extends DispatcherBase {
  19. constructor () {
  20. super()
  21. this[kQueue] = new FixedQueue()
  22. this[kClients] = []
  23. this[kQueued] = 0
  24. const pool = this
  25. this[kOnDrain] = function onDrain (origin, targets) {
  26. const queue = pool[kQueue]
  27. let needDrain = false
  28. while (!needDrain) {
  29. const item = queue.shift()
  30. if (!item) {
  31. break
  32. }
  33. pool[kQueued]--
  34. needDrain = !this.dispatch(item.opts, item.handler)
  35. }
  36. this[kNeedDrain] = needDrain
  37. if (!this[kNeedDrain] && pool[kNeedDrain]) {
  38. pool[kNeedDrain] = false
  39. pool.emit('drain', origin, [pool, ...targets])
  40. }
  41. if (pool[kClosedResolve] && queue.isEmpty()) {
  42. Promise
  43. .all(pool[kClients].map(c => c.close()))
  44. .then(pool[kClosedResolve])
  45. }
  46. }
  47. this[kOnConnect] = (origin, targets) => {
  48. pool.emit('connect', origin, [pool, ...targets])
  49. }
  50. this[kOnDisconnect] = (origin, targets, err) => {
  51. pool.emit('disconnect', origin, [pool, ...targets], err)
  52. }
  53. this[kOnConnectionError] = (origin, targets, err) => {
  54. pool.emit('connectionError', origin, [pool, ...targets], err)
  55. }
  56. this[kStats] = new PoolStats(this)
  57. }
  58. get [kBusy] () {
  59. return this[kNeedDrain]
  60. }
  61. get [kConnected] () {
  62. return this[kClients].filter(client => client[kConnected]).length
  63. }
  64. get [kFree] () {
  65. return this[kClients].filter(client => client[kConnected] && !client[kNeedDrain]).length
  66. }
  67. get [kPending] () {
  68. let ret = this[kQueued]
  69. for (const { [kPending]: pending } of this[kClients]) {
  70. ret += pending
  71. }
  72. return ret
  73. }
  74. get [kRunning] () {
  75. let ret = 0
  76. for (const { [kRunning]: running } of this[kClients]) {
  77. ret += running
  78. }
  79. return ret
  80. }
  81. get [kSize] () {
  82. let ret = this[kQueued]
  83. for (const { [kSize]: size } of this[kClients]) {
  84. ret += size
  85. }
  86. return ret
  87. }
  88. get stats () {
  89. return this[kStats]
  90. }
  91. async [kClose] () {
  92. if (this[kQueue].isEmpty()) {
  93. return Promise.all(this[kClients].map(c => c.close()))
  94. } else {
  95. return new Promise((resolve) => {
  96. this[kClosedResolve] = resolve
  97. })
  98. }
  99. }
  100. async [kDestroy] (err) {
  101. while (true) {
  102. const item = this[kQueue].shift()
  103. if (!item) {
  104. break
  105. }
  106. item.handler.onError(err)
  107. }
  108. return Promise.all(this[kClients].map(c => c.destroy(err)))
  109. }
  110. [kDispatch] (opts, handler) {
  111. const dispatcher = this[kGetDispatcher]()
  112. if (!dispatcher) {
  113. this[kNeedDrain] = true
  114. this[kQueue].push({ opts, handler })
  115. this[kQueued]++
  116. } else if (!dispatcher.dispatch(opts, handler)) {
  117. dispatcher[kNeedDrain] = true
  118. this[kNeedDrain] = !this[kGetDispatcher]()
  119. }
  120. return !this[kNeedDrain]
  121. }
  122. [kAddClient] (client) {
  123. client
  124. .on('drain', this[kOnDrain])
  125. .on('connect', this[kOnConnect])
  126. .on('disconnect', this[kOnDisconnect])
  127. .on('connectionError', this[kOnConnectionError])
  128. this[kClients].push(client)
  129. if (this[kNeedDrain]) {
  130. process.nextTick(() => {
  131. if (this[kNeedDrain]) {
  132. this[kOnDrain](client[kUrl], [this, client])
  133. }
  134. })
  135. }
  136. return this
  137. }
  138. [kRemoveClient] (client) {
  139. client.close(() => {
  140. const idx = this[kClients].indexOf(client)
  141. if (idx !== -1) {
  142. this[kClients].splice(idx, 1)
  143. }
  144. })
  145. this[kNeedDrain] = this[kClients].some(dispatcher => (
  146. !dispatcher[kNeedDrain] &&
  147. dispatcher.closed !== true &&
  148. dispatcher.destroyed !== true
  149. ))
  150. }
  151. }
  152. module.exports = {
  153. PoolBase,
  154. kClients,
  155. kNeedDrain,
  156. kAddClient,
  157. kRemoveClient,
  158. kGetDispatcher
  159. }