| 12345678910111213141516171819202122232425262728293031323334353637 |
- const Settings = require('@overleaf/settings')
- const redis = require('@overleaf/redis-wrapper')
- const { addConnectionDrainer } = require('./GracefulShutdown')
- /**
- * 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.
- *
- * @param feature - one of 'websessions' | 'ratelimiter' | ...
- */
- function client(feature) {
- const redisFeatureSettings = Settings.redis[feature] || Settings.redis.web
- const client = redis.createClient(redisFeatureSettings)
- addConnectionDrainer(`redis ${feature}`, async () => {
- await client.disconnect()
- })
- return client
- }
- async function cleanupTestRedis() {
- const rclient = client()
- ensureTestRedis(rclient)
- await rclient.flushall()
- }
- function ensureTestRedis(rclient) {
- const host = rclient.options.host
- const env = process.env.NODE_ENV
- if (host !== 'redis_test' || env !== 'test') {
- throw new Error(
- `Refusing to clear Redis instance '${host}' in environment '${env}'`
- )
- }
- }
- module.exports = { client, cleanupTestRedis }
|