Skip to content

armgit / dub

Public Imported from GitHub
0

The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.

add ratelimit

Steven Tey086afc5f9c9c
diff --git a/apps/web/app/api/track/lead/route.ts b/apps/web/app/api/track/lead/route.ts
index 5244800e7b..24c539d7f0 100644
--- a/apps/web/app/api/track/lead/route.ts
+++ b/apps/web/app/api/track/lead/route.ts
@@ -4,6 +4,7 @@ import { withWorkspaceEdge } from "@/lib/auth/workspace-edge";
import { generateRandomName } from "@/lib/names";
import { prismaEdge } from "@/lib/prisma/edge";
import { getClickEvent, recordCustomer, recordLead } from "@/lib/tinybird";
+import { ratelimit } from "@/lib/upstash";
import { clickEventSchemaTB } from "@/lib/zod/schemas/clicks";
import {
trackLeadRequestSchema,
@@ -27,6 +28,19 @@ export const POST = withWorkspaceEdge(
metadata,
} = trackLeadRequestSchema.parse(await parseRequestBody(req));
+ // if in production / preview env, deduplicate lead events – only record 1 event per hour
+ if (process.env.VERCEL === "1") {
+ const { success } = await ratelimit(2, "1 h").limit(
+ `recordLead:${externalId}:${eventName.toLowerCase().replace(" ", "-")}`,
+ );
+ if (!success) {
+ throw new DubApiError({
+ code: "rate_limit_exceeded",
+ message: `Rate limit exceeded for customer ${externalId}: ${eventName}`,
+ });
+ }
+ }
+
// Find click event
const clickEvent = await getClickEvent({ clickId });