RedisWrapper.js 931 B

1234567891011121314151617181920212223242526272829
  1. const Settings = require('@overleaf/settings')
  2. const redis = require('@overleaf/redis-wrapper')
  3. const { addConnectionDrainer } = require('./GracefulShutdown')
  4. if (
  5. typeof global.beforeEach === 'function' &&
  6. process.argv.join(' ').match(/unit/)
  7. ) {
  8. throw new Error(
  9. 'It looks like unit tests are running, but you are connecting to Redis. Missing a stub?'
  10. )
  11. }
  12. // A per-feature interface to Redis,
  13. // looks up the feature in `settings.redis`
  14. // and returns an appropriate client.
  15. // Necessary because we don't want to migrate web over
  16. // to redis-cluster all at once.
  17. module.exports = {
  18. // feature = 'websessions' | 'ratelimiter' | ...
  19. client(feature) {
  20. const redisFeatureSettings = Settings.redis[feature] || Settings.redis.web
  21. const client = redis.createClient(redisFeatureSettings)
  22. addConnectionDrainer(`redis ${feature}`, async () => {
  23. await client.disconnect()
  24. })
  25. return client
  26. },
  27. }