2020-06-23 13:29:44 -04:00
|
|
|
const logger = require('logger-sharelatex')
|
2020-11-25 06:57:22 -05:00
|
|
|
const metrics = require('@overleaf/metrics')
|
2021-07-12 12:47:18 -04:00
|
|
|
const settings = require('@overleaf/settings')
|
2020-08-20 09:05:50 -04:00
|
|
|
const OError = require('@overleaf/o-error')
|
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-07-07 06:06:02 -04:00
|
|
|
module.exports = {
|
2020-06-23 13:29:44 -04:00
|
|
|
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
|
2020-08-21 07:15:35 -04:00
|
|
|
return rclient
|
|
|
|
.subscribe(channel)
|
|
|
|
.finally(function () {
|
|
|
|
if (clientChannelMap.get(channel) === subscribePromise) {
|
|
|
|
clientChannelMap.delete(channel)
|
|
|
|
}
|
|
|
|
})
|
2020-06-23 13:29:44 -04:00
|
|
|
.then(function () {
|
2021-09-14 04:36:24 -04:00
|
|
|
logger.debug({ channel }, 'subscribed to channel')
|
2020-07-07 06:06:02 -04:00
|
|
|
metrics.inc(`subscribe.${baseChannel}`)
|
2020-06-23 13:29:44 -04:00
|
|
|
})
|
|
|
|
.catch(function (err) {
|
|
|
|
logger.error({ channel, err }, 'failed to subscribe to channel')
|
2020-07-07 06:06:02 -04:00
|
|
|
metrics.inc(`subscribe.failed.${baseChannel}`)
|
2020-08-20 09:05:50 -04:00
|
|
|
// add context for the stack-trace at the call-site
|
2020-08-21 07:15:35 -04:00
|
|
|
throw new OError('failed to subscribe to channel', {
|
2021-07-13 07:04:45 -04:00
|
|
|
channel,
|
2020-08-21 07:15:35 -04:00
|
|
|
}).withCause(err)
|
2020-06-23 13:29:44 -04:00
|
|
|
})
|
|
|
|
}
|
2020-05-15 05:34:07 -04:00
|
|
|
|
2020-06-23 13:29:44 -04:00
|
|
|
const pendingActions = clientChannelMap.get(channel) || Promise.resolve()
|
2020-07-07 06:06:02 -04:00
|
|
|
const subscribePromise = pendingActions.then(
|
|
|
|
actualSubscribe,
|
|
|
|
actualSubscribe
|
|
|
|
)
|
2020-06-23 13:29:44 -04:00
|
|
|
clientChannelMap.set(channel, subscribePromise)
|
2021-09-14 04:36:24 -04:00
|
|
|
logger.debug({ channel }, 'planned to subscribe to channel')
|
2020-06-23 13:29:44 -04:00
|
|
|
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
|
2020-07-07 06:06:02 -04:00
|
|
|
return rclient
|
2020-06-23 13:29:44 -04:00
|
|
|
.unsubscribe(channel)
|
|
|
|
.finally(function () {
|
|
|
|
if (clientChannelMap.get(channel) === unsubscribePromise) {
|
2020-07-07 06:06:02 -04:00
|
|
|
clientChannelMap.delete(channel)
|
2020-06-23 13:29:44 -04:00
|
|
|
}
|
|
|
|
})
|
|
|
|
.then(function () {
|
2021-09-14 04:36:24 -04:00
|
|
|
logger.debug({ channel }, 'unsubscribed from channel')
|
2020-07-07 06:06:02 -04:00
|
|
|
metrics.inc(`unsubscribe.${baseChannel}`)
|
2020-06-23 13:29:44 -04:00
|
|
|
})
|
|
|
|
.catch(function (err) {
|
|
|
|
logger.error({ channel, err }, 'unsubscribed from channel')
|
2020-07-07 06:06:02 -04:00
|
|
|
metrics.inc(`unsubscribe.failed.${baseChannel}`)
|
2020-06-23 13:29:44 -04:00
|
|
|
})
|
|
|
|
}
|
2020-05-15 05:34:07 -04:00
|
|
|
|
2020-06-23 13:29:44 -04:00
|
|
|
const pendingActions = clientChannelMap.get(channel) || Promise.resolve()
|
2020-07-07 06:06:02 -04:00
|
|
|
const unsubscribePromise = pendingActions.then(
|
2020-06-23 13:29:44 -04:00
|
|
|
actualUnsubscribe,
|
|
|
|
actualUnsubscribe
|
|
|
|
)
|
|
|
|
clientChannelMap.set(channel, unsubscribePromise)
|
2021-09-14 04:36:24 -04:00
|
|
|
logger.debug({ channel }, 'planned to unsubscribe from channel')
|
2020-06-23 13:29:44 -04:00
|
|
|
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
|
2020-07-07 06:06:02 -04:00
|
|
|
rclient.publish(channel, data)
|
2021-07-13 07:04:45 -04:00
|
|
|
},
|
2020-06-23 13:29:44 -04:00
|
|
|
}
|