2021-07-07 05:38:56 -04:00
|
|
|
const Settings = require('@overleaf/settings')
|
2020-11-12 07:52:32 -05:00
|
|
|
const redis = require('@overleaf/redis-wrapper')
|
2022-08-04 04:16:38 -04:00
|
|
|
const { addConnectionDrainer } = require('./GracefulShutdown')
|
2019-05-29 05:21:06 -04:00
|
|
|
|
2020-02-12 10:13:09 -05:00
|
|
|
if (
|
|
|
|
typeof global.beforeEach === 'function' &&
|
|
|
|
process.argv.join(' ').match(/unit/)
|
|
|
|
) {
|
|
|
|
throw new Error(
|
|
|
|
'It looks like unit tests are running, but you are connecting to Redis. Missing a stub?'
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2019-05-29 05:21:06 -04:00
|
|
|
// A per-feature interface to Redis,
|
|
|
|
// looks up the feature in `settings.redis`
|
|
|
|
// and returns an appropriate client.
|
|
|
|
// Necessary because we don't want to migrate web over
|
|
|
|
// to redis-cluster all at once.
|
2020-02-12 10:13:09 -05:00
|
|
|
module.exports = {
|
2019-05-29 05:21:06 -04:00
|
|
|
// feature = 'websessions' | 'ratelimiter' | ...
|
|
|
|
client(feature) {
|
|
|
|
const redisFeatureSettings = Settings.redis[feature] || Settings.redis.web
|
2022-08-04 04:16:38 -04:00
|
|
|
const client = redis.createClient(redisFeatureSettings)
|
|
|
|
addConnectionDrainer(`redis ${feature}`, async () => {
|
|
|
|
await client.disconnect()
|
|
|
|
})
|
|
|
|
return client
|
2021-04-27 03:52:58 -04:00
|
|
|
},
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|