2020-06-23 13:29:38 -04:00
|
|
|
/* eslint-disable
|
|
|
|
no-unused-vars,
|
|
|
|
*/
|
|
|
|
// TODO: This file was created by bulk-decaffeinate.
|
|
|
|
// Fix any style issues and re-enable lint.
|
2020-06-23 13:29:34 -04:00
|
|
|
/*
|
|
|
|
* decaffeinate suggestions:
|
|
|
|
* DS102: Remove unnecessary code created because of implicit returns
|
|
|
|
* Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
|
|
|
|
*/
|
2020-06-23 13:29:44 -04:00
|
|
|
let ChannelManager
|
|
|
|
const logger = require('logger-sharelatex')
|
|
|
|
const metrics = require('metrics-sharelatex')
|
|
|
|
const settings = require('settings-sharelatex')
|
2019-07-18 06:25:10 -04:00
|
|
|
|
2020-06-23 13:29:44 -04:00
|
|
|
const ClientMap = new Map() // for each redis client, store a Map of subscribed channels (channelname -> subscribe promise)
|
2019-07-18 06:25:10 -04:00
|
|
|
|
2020-06-23 13:29:34 -04:00
|
|
|
// Manage redis pubsub subscriptions for individual projects and docs, ensuring
|
|
|
|
// that we never subscribe to a channel multiple times. The socket.io side is
|
|
|
|
// handled by RoomManager.
|
2019-07-18 06:25:10 -04:00
|
|
|
|
2020-06-23 13:29:44 -04:00
|
|
|
module.exports = ChannelManager = {
|
|
|
|
getClientMapEntry(rclient) {
|
|
|
|
// return the per-client channel map if it exists, otherwise create and
|
|
|
|
// return an empty map for the client.
|
|
|
|
return (
|
|
|
|
ClientMap.get(rclient) || ClientMap.set(rclient, new Map()).get(rclient)
|
|
|
|
)
|
|
|
|
},
|
2019-07-18 06:25:10 -04:00
|
|
|
|
2020-06-23 13:29:44 -04:00
|
|
|
subscribe(rclient, baseChannel, id) {
|
|
|
|
const clientChannelMap = this.getClientMapEntry(rclient)
|
|
|
|
const channel = `${baseChannel}:${id}`
|
|
|
|
const actualSubscribe = function () {
|
|
|
|
// subscribe is happening in the foreground and it should reject
|
|
|
|
const p = rclient.subscribe(channel)
|
|
|
|
p.finally(function () {
|
|
|
|
if (clientChannelMap.get(channel) === subscribePromise) {
|
|
|
|
return clientChannelMap.delete(channel)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.then(function () {
|
|
|
|
logger.log({ channel }, 'subscribed to channel')
|
|
|
|
return metrics.inc(`subscribe.${baseChannel}`)
|
|
|
|
})
|
|
|
|
.catch(function (err) {
|
|
|
|
logger.error({ channel, err }, 'failed to subscribe to channel')
|
|
|
|
return metrics.inc(`subscribe.failed.${baseChannel}`)
|
|
|
|
})
|
|
|
|
return p
|
|
|
|
}
|
2020-05-15 05:34:07 -04:00
|
|
|
|
2020-06-23 13:29:44 -04:00
|
|
|
const pendingActions = clientChannelMap.get(channel) || Promise.resolve()
|
|
|
|
var subscribePromise = pendingActions.then(actualSubscribe, actualSubscribe)
|
|
|
|
clientChannelMap.set(channel, subscribePromise)
|
|
|
|
logger.log({ channel }, 'planned to subscribe to channel')
|
|
|
|
return subscribePromise
|
|
|
|
},
|
2019-07-18 06:25:10 -04:00
|
|
|
|
2020-06-23 13:29:44 -04:00
|
|
|
unsubscribe(rclient, baseChannel, id) {
|
|
|
|
const clientChannelMap = this.getClientMapEntry(rclient)
|
|
|
|
const channel = `${baseChannel}:${id}`
|
|
|
|
const actualUnsubscribe = function () {
|
|
|
|
// unsubscribe is happening in the background, it should not reject
|
|
|
|
const p = rclient
|
|
|
|
.unsubscribe(channel)
|
|
|
|
.finally(function () {
|
|
|
|
if (clientChannelMap.get(channel) === unsubscribePromise) {
|
|
|
|
return clientChannelMap.delete(channel)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.then(function () {
|
|
|
|
logger.log({ channel }, 'unsubscribed from channel')
|
|
|
|
return metrics.inc(`unsubscribe.${baseChannel}`)
|
|
|
|
})
|
|
|
|
.catch(function (err) {
|
|
|
|
logger.error({ channel, err }, 'unsubscribed from channel')
|
|
|
|
return metrics.inc(`unsubscribe.failed.${baseChannel}`)
|
|
|
|
})
|
|
|
|
return p
|
|
|
|
}
|
2020-05-15 05:34:07 -04:00
|
|
|
|
2020-06-23 13:29:44 -04:00
|
|
|
const pendingActions = clientChannelMap.get(channel) || Promise.resolve()
|
|
|
|
var unsubscribePromise = pendingActions.then(
|
|
|
|
actualUnsubscribe,
|
|
|
|
actualUnsubscribe
|
|
|
|
)
|
|
|
|
clientChannelMap.set(channel, unsubscribePromise)
|
|
|
|
logger.log({ channel }, 'planned to unsubscribe from channel')
|
|
|
|
return unsubscribePromise
|
|
|
|
},
|
2019-07-18 06:25:10 -04:00
|
|
|
|
2020-06-23 13:29:44 -04:00
|
|
|
publish(rclient, baseChannel, id, data) {
|
|
|
|
let channel
|
|
|
|
metrics.summary(`redis.publish.${baseChannel}`, data.length)
|
|
|
|
if (id === 'all' || !settings.publishOnIndividualChannels) {
|
|
|
|
channel = baseChannel
|
|
|
|
} else {
|
|
|
|
channel = `${baseChannel}:${id}`
|
2020-06-23 13:29:34 -04:00
|
|
|
}
|
2020-06-23 13:29:44 -04:00
|
|
|
// we publish on a different client to the subscribe, so we can't
|
|
|
|
// check for the channel existing here
|
|
|
|
return rclient.publish(channel, data)
|
|
|
|
}
|
|
|
|
}
|