RedisWrapper.mjs 848 B

1234567891011121314151617181920212223242526
  1. import Settings from '@overleaf/settings'
  2. import redis from '@overleaf/redis-wrapper'
  3. import { addConnectionDrainer } from './GracefulShutdown.mjs'
  4. /**
  5. * A per-feature interface to Redis, looks up the feature in `settings.redis`
  6. * and returns an appropriate client. Necessary because we don't want to
  7. * migrate web over to redis-cluster all at once.
  8. *
  9. * @param feature - one of 'websessions' | 'ratelimiter' | ...
  10. */
  11. function client(feature) {
  12. const redisFeatureSettings = Settings.redis[feature] || Settings.redis.web
  13. const client = redis.createClient(redisFeatureSettings)
  14. addConnectionDrainer(`redis ${feature}`, async () => {
  15. await client.disconnect()
  16. })
  17. return client
  18. }
  19. async function cleanupTestRedis() {
  20. const rclient = client()
  21. await redis.cleanupTestRedis(rclient)
  22. }
  23. export default { client, cleanupTestRedis }