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.

Guard in recordClickCache (#3567)

Steven Teyf587f190fe9f
diff --git a/apps/web/lib/api/links/cache.ts b/apps/web/lib/api/links/cache.ts
index aa0e7f8be6..c5a54cdf73 100644
--- a/apps/web/lib/api/links/cache.ts
+++ b/apps/web/lib/api/links/cache.ts
@@ -1,3 +1,4 @@
+import { getLinkViaEdge } from "@/lib/planetscale";
import { LinkProps, RedisLinkProps } from "@/lib/types";
import {
formatRedisLink,
@@ -44,10 +45,7 @@ class LinkCache {
return await pipeline.exec();
}
- async set(
- link: ExpandedLink,
- { setVercelCache }: { setVercelCache?: boolean } = {},
- ) {
+ async set(link: ExpandedLink) {
const redisLink = formatRedisLink(link);
const cacheKey = this._createKey({ domain: link.domain, key: link.key });
@@ -58,9 +56,7 @@ class LinkCache {
redisGlobal.set(cacheKey, redisLink, {
ex: REDIS_CACHE_EXPIRATION,
}),
- setVercelCache
- ? vercelCache.set(cacheKey, redisLink, { ttl: VERCEL_CACHE_EXPIRATION })
- : this._invalidateVercelCache(cacheKey),
+ this._invalidateVercelCache(cacheKey),
]);
}
@@ -106,7 +102,25 @@ class LinkCache {
}
console.log(`[Vercel Cache MISS] ${cacheKey} - Falling back to MySQL...`);
- return null;
+
+ const linkData = await getLinkViaEdge({
+ domain,
+ key,
+ });
+ // no link found, throw a 404 error
+ if (!linkData) {
+ throw new Error("Link not found.");
+ }
+ // else, format the link and cache it both in LRU cache and Vercel cache
+ cachedLink = formatRedisLink(linkData as any);
+ console.log(`Setting both LRU cache and Vercel cache for ${cacheKey}`);
+ linkLRUCache.set(cacheKey, cachedLink);
+ waitUntil(
+ vercelCache.set(cacheKey, cachedLink, {
+ ttl: VERCEL_CACHE_EXPIRATION,
+ }),
+ );
+ return cachedLink;
}
}
diff --git a/apps/web/lib/api/links/record-click-cache.ts b/apps/web/lib/api/links/record-click-cache.ts
index 3ef028df16..6c2ac1a627 100644
--- a/apps/web/lib/api/links/record-click-cache.ts
+++ b/apps/web/lib/api/links/record-click-cache.ts
@@ -1,4 +1,4 @@
-import { redisGlobal } from "@/lib/upstash";
+import { redisGlobal, redisGlobalWithTimeout } from "@/lib/upstash";
// Cache the click ID in Redis for 1 hour
const CACHE_EXPIRATION = 60 * 60;
@@ -26,7 +26,7 @@ class RecordClickCache {
}
async get({ domain, key, identityHash }: KeyProps) {
- return await redisGlobal.get<string>(
+ return await redisGlobalWithTimeout.get<string>(
this._createKey({ domain, key, identityHash }),
);
}
diff --git a/apps/web/lib/middleware/link.ts b/apps/web/lib/middleware/link.ts
index 476bab5481..178ee9c48d 100644
--- a/apps/web/lib/middleware/link.ts
+++ b/apps/web/lib/middleware/link.ts
@@ -99,7 +99,7 @@ export async function LinkMiddleware(req: NextRequest, ev: NextFetchEvent) {
ev.waitUntil(
(async () => {
if (!isPartnerLink) {
- await linkCache.set(linkData as any, { setVercelCache: true });
+ await linkCache.set(linkData as any);
return;
}
@@ -109,14 +109,11 @@ export async function LinkMiddleware(req: NextRequest, ev: NextFetchEvent) {
});
// we'll use this data on /track/click
- await linkCache.set(
- {
- ...(linkData as any),
- ...(partner && { partner }),
- ...(discount && { discount }),
- },
- { setVercelCache: true },
- );
+ await linkCache.set({
+ ...(linkData as any),
+ ...(partner && { partner }),
+ ...(discount && { discount }),
+ });
})(),
);
}
@@ -240,8 +237,9 @@ export async function LinkMiddleware(req: NextRequest, ev: NextFetchEvent) {
if (shouldCacheClickId) {
const identityHash = await getIdentityHash(req);
clickId =
- (await recordClickCache.get({ domain, key, identityHash })) ||
- undefined;
+ (await recordClickCache
+ .get({ domain, key, identityHash })
+ .catch(() => undefined)) || undefined;
}
// if there's still no clickId, generate a new one
diff --git a/apps/web/lib/tinybird/record-click.ts b/apps/web/lib/tinybird/record-click.ts
index 8107b02772..5831141f96 100644
--- a/apps/web/lib/tinybird/record-click.ts
+++ b/apps/web/lib/tinybird/record-click.ts
@@ -93,13 +93,19 @@ export async function recordClick({
// by default, we deduplicate clicks for a domain + key pair from the same IP address – only record 1 click per hour
// we only need to do these if skipRatelimit is not true (we skip it in /api/track/:path endpoints)
if (!skipRatelimit) {
- // here, we check if the clickId is cached in Redis within the last hour
- const cachedClickId = await recordClickCache.get({
- domain,
- key,
- identityHash,
- });
- if (cachedClickId) {
+ try {
+ // here, we check if the clickId is cached in Redis within the last hour
+ const cachedClickId = await recordClickCache.get({
+ domain,
+ key,
+ identityHash,
+ });
+ if (cachedClickId) {
+ return null;
+ }
+ } catch (error) {
+ console.error(`[recordClickCache error]: ${error}`);
+ // if redis fails, return null so we don't overwhelm TB/MySQL
return null;
}
}