ChannelManager.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. /* eslint-disable
  2. no-unused-vars,
  3. */
  4. // TODO: This file was created by bulk-decaffeinate.
  5. // Fix any style issues and re-enable lint.
  6. /*
  7. * decaffeinate suggestions:
  8. * DS102: Remove unnecessary code created because of implicit returns
  9. * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
  10. */
  11. let ChannelManager
  12. const logger = require('logger-sharelatex')
  13. const metrics = require('metrics-sharelatex')
  14. const settings = require('settings-sharelatex')
  15. const ClientMap = new Map() // for each redis client, store a Map of subscribed channels (channelname -> subscribe promise)
  16. // Manage redis pubsub subscriptions for individual projects and docs, ensuring
  17. // that we never subscribe to a channel multiple times. The socket.io side is
  18. // handled by RoomManager.
  19. module.exports = ChannelManager = {
  20. getClientMapEntry(rclient) {
  21. // return the per-client channel map if it exists, otherwise create and
  22. // return an empty map for the client.
  23. return (
  24. ClientMap.get(rclient) || ClientMap.set(rclient, new Map()).get(rclient)
  25. )
  26. },
  27. subscribe(rclient, baseChannel, id) {
  28. const clientChannelMap = this.getClientMapEntry(rclient)
  29. const channel = `${baseChannel}:${id}`
  30. const actualSubscribe = function () {
  31. // subscribe is happening in the foreground and it should reject
  32. const p = rclient.subscribe(channel)
  33. p.finally(function () {
  34. if (clientChannelMap.get(channel) === subscribePromise) {
  35. return clientChannelMap.delete(channel)
  36. }
  37. })
  38. .then(function () {
  39. logger.log({ channel }, 'subscribed to channel')
  40. return metrics.inc(`subscribe.${baseChannel}`)
  41. })
  42. .catch(function (err) {
  43. logger.error({ channel, err }, 'failed to subscribe to channel')
  44. return metrics.inc(`subscribe.failed.${baseChannel}`)
  45. })
  46. return p
  47. }
  48. const pendingActions = clientChannelMap.get(channel) || Promise.resolve()
  49. var subscribePromise = pendingActions.then(actualSubscribe, actualSubscribe)
  50. clientChannelMap.set(channel, subscribePromise)
  51. logger.log({ channel }, 'planned to subscribe to channel')
  52. return subscribePromise
  53. },
  54. unsubscribe(rclient, baseChannel, id) {
  55. const clientChannelMap = this.getClientMapEntry(rclient)
  56. const channel = `${baseChannel}:${id}`
  57. const actualUnsubscribe = function () {
  58. // unsubscribe is happening in the background, it should not reject
  59. const p = rclient
  60. .unsubscribe(channel)
  61. .finally(function () {
  62. if (clientChannelMap.get(channel) === unsubscribePromise) {
  63. return clientChannelMap.delete(channel)
  64. }
  65. })
  66. .then(function () {
  67. logger.log({ channel }, 'unsubscribed from channel')
  68. return metrics.inc(`unsubscribe.${baseChannel}`)
  69. })
  70. .catch(function (err) {
  71. logger.error({ channel, err }, 'unsubscribed from channel')
  72. return metrics.inc(`unsubscribe.failed.${baseChannel}`)
  73. })
  74. return p
  75. }
  76. const pendingActions = clientChannelMap.get(channel) || Promise.resolve()
  77. var unsubscribePromise = pendingActions.then(
  78. actualUnsubscribe,
  79. actualUnsubscribe
  80. )
  81. clientChannelMap.set(channel, unsubscribePromise)
  82. logger.log({ channel }, 'planned to unsubscribe from channel')
  83. return unsubscribePromise
  84. },
  85. publish(rclient, baseChannel, id, data) {
  86. let channel
  87. metrics.summary(`redis.publish.${baseChannel}`, data.length)
  88. if (id === 'all' || !settings.publishOnIndividualChannels) {
  89. channel = baseChannel
  90. } else {
  91. channel = `${baseChannel}:${id}`
  92. }
  93. // we publish on a different client to the subscribe, so we can't
  94. // check for the channel existing here
  95. return rclient.publish(channel, data)
  96. }
  97. }