Skip to content

Commit

Permalink
Merge branch 'extract-redis-to-file' into more-strict-rate-limit-for-…
Browse files Browse the repository at this point in the history
…spammer
  • Loading branch information
wa0x6e committed Oct 31, 2023
2 parents e81463f + 49c0d3e commit 610cd85
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 20 deletions.
1 change: 1 addition & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ PINEAPPLE_URL=https://.../upload # optional
SCORE_API_URL=https://score.snapshot.org
# If you need unlimted access to score-api, use `https://score.snapshot.org?apiKey=...`
RATE_LIMIT_DATABASE_URL= # optional
RATE_LIMIT_KEYS_PREFIX=snapshot-sequencer: #optional
BROVIDER_URL=https://rpc.snapshot.org # optional
# Secret for laser
AUTH_SECRET=1dfd84a695705665668c260222ded178d1f1d62d251d7bee8148428dac6d0487 # optional
1 change: 1 addition & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ jobs:
with:
mysql_database_name: snapshot_sequencer_test
mysql_schema_path: 'test/schema.sql'
redis: true
24 changes: 4 additions & 20 deletions src/helpers/rateLimit.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,7 @@
import rateLimit from 'express-rate-limit';
import RedisStore from 'rate-limit-redis';
import { createClient } from 'redis';
import redisClient from './redis';
import { getIp, sendError, sha256 } from './utils';
import log from './log';

let client;

(async () => {
if (!process.env.RATE_LIMIT_DATABASE_URL) return;

log.info('[redis-rl] Connecting to Redis');
client = createClient({ url: process.env.RATE_LIMIT_DATABASE_URL });
client.on('connect', () => log.info('[redis-rl] Redis connect'));
client.on('ready', () => log.info('[redis-rl] Redis ready'));
client.on('reconnecting', err => log.info('[redis-rl] Redis reconnecting', err));
client.on('error', err => log.info('[redis-rl] Redis error', err));
client.on('end', err => log.info('[redis-rl] Redis end', err));
await client.connect();
})();

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

Expand All @@ -31,10 +15,10 @@ const rateLimitConfig = {
429
);
},
store: client
store: redisClient
? new RedisStore({
sendCommand: (...args: string[]) => client.sendCommand(args),
prefix: 'snapshot-sequencer:'
sendCommand: (...args: string[]) => redisClient.sendCommand(args),
prefix: process.env.RATE_LIMIT_KEYS_PREFIX || 'snapshot-sequencer:'
})
: undefined
};
Expand Down
19 changes: 19 additions & 0 deletions src/helpers/redis.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { createClient } from 'redis';
import log from './log';

let client;

(async () => {
if (!process.env.RATE_LIMIT_DATABASE_URL) return;

log.info('[redis] Connecting to Redis');
client = createClient({ url: process.env.RATE_LIMIT_DATABASE_URL });
client.on('connect', () => log.info('[redis] Redis connect'));
client.on('ready', () => log.info('[redis] Redis ready'));
client.on('reconnecting', err => log.info('[redis] Redis reconnecting', err));
client.on('error', err => log.info('[redis] Redis error', err));
client.on('end', err => log.info('[redis] Redis end', err));
await client.connect();
})();

export default client;
2 changes: 2 additions & 0 deletions test/.env.test
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,5 @@ SEQ_DATABASE_URL=mysql://root:[email protected]:3306/snapshot_sequencer_test
NETWORK=main
RELAYER_PK=01686849e86499c1860ea0afc97f29c11018cbac049abf843df875c60054076e
NODE_ENV=test
RATE_LIMIT_DATABASE_URL=redis://localhost:6379
RATE_LIMIT_KEYS_PREFIX=snapshot-sequencer-test:

0 comments on commit 610cd85

Please sign in to comment.