ChannelManager.coffee 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. logger = require 'logger-sharelatex'
  2. metrics = require "metrics-sharelatex"
  3. settings = require "settings-sharelatex"
  4. ClientMap = new Map() # for each redis client, store a Map of subscribed channels (channelname -> subscribe promise)
  5. # Manage redis pubsub subscriptions for individual projects and docs, ensuring
  6. # that we never subscribe to a channel multiple times. The socket.io side is
  7. # handled by RoomManager.
  8. module.exports = ChannelManager =
  9. getClientMapEntry: (rclient) ->
  10. # return the per-client channel map if it exists, otherwise create and
  11. # return an empty map for the client.
  12. ClientMap.get(rclient) || ClientMap.set(rclient, new Map()).get(rclient)
  13. subscribe: (rclient, baseChannel, id) ->
  14. clientChannelMap = @getClientMapEntry(rclient)
  15. channel = "#{baseChannel}:#{id}"
  16. actualSubscribe = () ->
  17. # subscribe is happening in the foreground and it should reject
  18. p = rclient.subscribe(channel)
  19. p.finally () ->
  20. if clientChannelMap.get(channel) is subscribePromise
  21. clientChannelMap.delete(channel)
  22. .then () ->
  23. logger.log {channel}, "subscribed to channel"
  24. metrics.inc "subscribe.#{baseChannel}"
  25. .catch (err) ->
  26. logger.error {channel, err}, "failed to subscribe to channel"
  27. metrics.inc "subscribe.failed.#{baseChannel}"
  28. return p
  29. pendingActions = clientChannelMap.get(channel) || Promise.resolve()
  30. subscribePromise = pendingActions.then(actualSubscribe, actualSubscribe)
  31. clientChannelMap.set(channel, subscribePromise)
  32. logger.log {channel}, "planned to subscribe to channel"
  33. return subscribePromise
  34. unsubscribe: (rclient, baseChannel, id) ->
  35. clientChannelMap = @getClientMapEntry(rclient)
  36. channel = "#{baseChannel}:#{id}"
  37. actualUnsubscribe = () ->
  38. # unsubscribe is happening in the background, it should not reject
  39. p = rclient.unsubscribe(channel)
  40. .finally () ->
  41. if clientChannelMap.get(channel) is unsubscribePromise
  42. clientChannelMap.delete(channel)
  43. .then () ->
  44. logger.log {channel}, "unsubscribed from channel"
  45. metrics.inc "unsubscribe.#{baseChannel}"
  46. .catch (err) ->
  47. logger.error {channel, err}, "unsubscribed from channel"
  48. metrics.inc "unsubscribe.failed.#{baseChannel}"
  49. return p
  50. pendingActions = clientChannelMap.get(channel) || Promise.resolve()
  51. unsubscribePromise = pendingActions.then(actualUnsubscribe, actualUnsubscribe)
  52. clientChannelMap.set(channel, unsubscribePromise)
  53. logger.log {channel}, "planned to unsubscribe from channel"
  54. return unsubscribePromise
  55. publish: (rclient, baseChannel, id, data) ->
  56. metrics.summary "redis.publish.#{baseChannel}", data.length
  57. if id is 'all' or !settings.publishOnIndividualChannels
  58. channel = baseChannel
  59. else
  60. channel = "#{baseChannel}:#{id}"
  61. # we publish on a different client to the subscribe, so we can't
  62. # check for the channel existing here
  63. rclient.publish channel, data