Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions src/lib/utils/rate-limiter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,13 @@ function getClientIp(req: Request & { ip?: string}): string {
}

export async function rateLimitCheck(req: Request & { ip?: string }, paperId: string) {
if (!redis) {
if (process.env.NODE_ENV === "production") {
throw new CustomError("rate limiting is not configured", 503);
}
return;
}

const ratelimit = new Ratelimit({
redis,
limiter: Ratelimit.slidingWindow(3, "1 h"),//per id - 3 request - per hour
Expand Down
26 changes: 11 additions & 15 deletions src/lib/utils/ratelimit.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,16 @@
import { Ratelimit } from "@upstash/ratelimit";
import { redis } from "./redis";

// Rate limiter for AI Upload (5 requests per 15 minutes)
export const aiUploadRatelimit = new Ratelimit({
redis: redis,
limiter: Ratelimit.slidingWindow(5, "900 s"),
});
type Window = Parameters<typeof Ratelimit.slidingWindow>[1];

// Rate limiter for Paper Requests (5 requests per 15 minutes)
export const paperRequestRatelimit = new Ratelimit({
redis: redis,
limiter: Ratelimit.slidingWindow(5, "900 s"),
});
function allow(requests: number, window: Window) {
return redis
? new Ratelimit({ redis, limiter: Ratelimit.slidingWindow(requests, window) })
: null;
}

// Rate limiter for Subscriptions (3 requests per hour)
export const subscribeRatelimit = new Ratelimit({
redis: redis,
limiter: Ratelimit.slidingWindow(3, "1 h"),
});
export const aiUploadRatelimit = allow(5, "900 s");

export const paperRequestRatelimit = allow(5, "900 s");

export const subscribeRatelimit = allow(3, "1 h");
11 changes: 4 additions & 7 deletions src/lib/utils/redis.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
import { Redis } from "@upstash/redis";

export const redis =
process.env.UPSTASH_REDIS_REST_URL && process.env.UPSTASH_REDIS_REST_TOKEN
? new Redis({
url: process.env.UPSTASH_REDIS_REST_URL,
token: process.env.UPSTASH_REDIS_REST_TOKEN,
})
: ({} as Redis);
const url = process.env.UPSTASH_REDIS_REST_URL;
const token = process.env.UPSTASH_REDIS_REST_TOKEN;

export const redis = url && token ? new Redis({ url, token }) : null;
7 changes: 7 additions & 0 deletions src/middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,13 @@ export const config = {
};

export default async function middleware(request: NextRequest) {
if (!aiUploadRatelimit || !paperRequestRatelimit || !subscribeRatelimit) {
if (process.env.NODE_ENV === "production") {
return failure("rate limiting is not configured", 503);
}
return NextResponse.next();
}

const ip = request.ip ?? "127.0.0.1";
const { pathname } = request.nextUrl;

Expand Down