Skip to content

Commit 306df7e

Browse files
matt-aitkenericallam
authored andcommitted
Trying to use the @upstash/ratelimit package with ioredis…
1 parent 9ecf077 commit 306df7e

File tree

3 files changed

+132
-6
lines changed

3 files changed

+132
-6
lines changed
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import Redis, { RedisOptions } from "ioredis";
2+
import { Ratelimit } from "@upstash/ratelimit";
3+
import { LoaderFunction, LoaderFunctionArgs } from "@remix-run/server-runtime";
4+
import { env } from "~/env.server";
5+
6+
export function createRedisRateLimitClient(): ConstructorParameters<typeof Ratelimit>[0]["redis"] {
7+
const redis = new Redis({
8+
port: env.REDIS_PORT,
9+
host: env.REDIS_HOST,
10+
username: env.REDIS_USERNAME,
11+
password: env.REDIS_PASSWORD,
12+
enableAutoPipelining: true,
13+
...(env.REDIS_TLS_DISABLED === "true" ? {} : { tls: {} }),
14+
});
15+
16+
return {
17+
sadd: async <TData>(key: string, ...members: TData[]): Promise<number> => {
18+
return redis.sadd(key, members as (string | Buffer | number)[]);
19+
},
20+
eval: <TArgs extends unknown[], TData = unknown>(
21+
...args: [script: string, keys: string[], args: TArgs]
22+
): Promise<TData> => {
23+
return redis.eval(...args) as TData;
24+
},
25+
};
26+
}
27+
28+
const ratelimitter = new Ratelimit({
29+
redis: createRedisRateLimitClient(),
30+
limiter: Ratelimit.cachedFixedWindow(10, "10s"),
31+
ephemeralCache: new Map(),
32+
analytics: true,
33+
});
34+
35+
export const rateLimit = {
36+
loader: (args: LoaderFunctionArgs): ReturnType<LoaderFunction> => {},
37+
};

apps/webapp/package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@
9696
"@trigger.dev/yalt": "workspace:*",
9797
"@types/pg": "8.6.6",
9898
"@uiw/react-codemirror": "^4.19.5",
99+
"@upstash/ratelimit": "^1.0.1",
99100
"@whatwg-node/fetch": "^0.9.14",
100101
"class-variance-authority": "^0.5.2",
101102
"clsx": "^1.2.1",
@@ -136,6 +137,7 @@
136137
"react-stately": "^3.29.1",
137138
"react-use": "^17.4.0",
138139
"recharts": "^2.8.0",
140+
"redis": "^4.6.13",
139141
"remix-auth": "^3.6.0",
140142
"remix-auth-email-link": "2.0.2",
141143
"remix-auth-github": "^1.6.0",

pnpm-lock.yaml

Lines changed: 93 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)