mirror of
https://github.com/directus/directus.git
synced 2026-04-25 03:00:53 -04:00
error trap if redis is down
This commit is contained in:
@@ -9,12 +9,7 @@
|
||||
{{ database }}
|
||||
|
||||
####################################################################################################
|
||||
# REDIS Server
|
||||
|
||||
{{ redisServer }}
|
||||
|
||||
####################################################################################################
|
||||
# Rate Limiting and caching
|
||||
# Rate Limiting
|
||||
|
||||
{{ rateLimits}}
|
||||
####################################################################################################
|
||||
|
||||
@@ -25,12 +25,10 @@ const defaults = {
|
||||
STORAGE_LOCAL_DRIVER: 'local',
|
||||
STORAGE_LOCAL_ROOT: './uploads',
|
||||
},
|
||||
redisServer: {
|
||||
SREDIS_HOST: '127.0.0.1',
|
||||
SREDIS_PORT: '6379',
|
||||
SREDIS_PASSWORD: null,
|
||||
},
|
||||
rateLimits: {
|
||||
REDIS_HOST: '127.0.0.1',
|
||||
REDIS_PORT: '6379',
|
||||
REDIS_PASSWORD: null,
|
||||
RATE_LIMIT_TYPE: 'redis',
|
||||
CONSUMED_POINTS_LIMIT: 5,
|
||||
CONSUMED_RESET_DURATION: 1,
|
||||
|
||||
@@ -7,12 +7,16 @@
|
||||
import { RequestHandler } from 'express';
|
||||
import asyncHandler from 'express-async-handler';
|
||||
import { HitRateLimitException } from '../exceptions';
|
||||
import { RedisNotFoundException } from '../exceptions';
|
||||
import rateLimiterConfig from '../get-rate-limiter-config';
|
||||
|
||||
const rateLimiter: RequestHandler = asyncHandler(async (req, res, next) => {
|
||||
try {
|
||||
await rateLimiterConfig.consume(req.ip);
|
||||
} catch (rejRes) {
|
||||
if (rejRes instanceof Error) {
|
||||
throw new RedisNotFoundException('Redis is having some trouble connecting');
|
||||
}
|
||||
// If there is no error, rateLimiterRedis promise rejected with number of ms before next request allowed
|
||||
const secs = Math.round(rejRes.msBeforeNext / 1000) || 1;
|
||||
res.set('Retry-After', String(secs));
|
||||
|
||||
Reference in New Issue
Block a user