Skip to content

Commit

Permalink
fix: fix leaking redisStore key expiration
Browse files Browse the repository at this point in the history
  • Loading branch information
wa0x6e committed Nov 28, 2023
1 parent 25697ae commit e670324
Showing 1 changed file with 18 additions and 14 deletions.
32 changes: 18 additions & 14 deletions src/helpers/rateLimit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,15 @@ import { getIp, sendError, sha256 } from './utils';

const hashedIp = (req): string => sha256(getIp(req)).slice(0, 7);

function getStore() {
return redisClient
? new RedisStore({
sendCommand: (...args: string[]) => redisClient.sendCommand(args),
prefix: process.env.RATE_LIMIT_KEYS_PREFIX || 'snapshot-sequencer:'
})
: undefined;
}

const rateLimitConfig = {
standardHeaders: true,
legacyHeaders: false,
Expand All @@ -14,27 +23,22 @@ const rateLimitConfig = {
'too many requests, refer to https://docs.snapshot.org/tools/api/api-keys#limits',
429
);
},
store: redisClient
? new RedisStore({
sendCommand: (...args: string[]) => redisClient.sendCommand(args),
prefix: process.env.RATE_LIMIT_KEYS_PREFIX || 'snapshot-sequencer:'
})
: undefined
}
};

const regularRateLimit = rateLimit({
keyGenerator: req => `rl:${hashedIp(req)}`,
windowMs: 60 * 1e3,
max: 100,
...rateLimitConfig
});

const highErroredRateLimit = rateLimit({
keyGenerator: req => `rl-s:${hashedIp(req)}`,
windowMs: 15 * 1e3,
max: 15,
skipSuccessfulRequests: true,
store: getStore(),
...rateLimitConfig
});
const regularRateLimit = rateLimit({
keyGenerator: req => `rl:${hashedIp(req)}`,
windowMs: 60 * 1e3,
max: 100,
store: getStore(),
...rateLimitConfig
});

Expand Down

0 comments on commit e670324

Please sign in to comment.