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.

redisWithTimeout

Steven Teyfc7ba124e0fe
diff --git a/apps/web/lib/api/links/cache.ts b/apps/web/lib/api/links/cache.ts
index fc0363637e..af57dda3cd 100644
--- a/apps/web/lib/api/links/cache.ts
+++ b/apps/web/lib/api/links/cache.ts
@@ -1,5 +1,5 @@
import { LinkProps, RedisLinkProps } from "@/lib/types";
-import { formatRedisLink, redis } from "@/lib/upstash";
+import { formatRedisLink, redis, redisWithTimeout } from "@/lib/upstash";
import { decodeKey, isCaseSensitiveDomain } from "./case-sensitivity";
import { ExpandedLink } from "./utils/transform-link";
@@ -51,10 +51,21 @@ class LinkCache {
}
async get({ domain, key }: Pick<LinkProps, "domain" | "key">) {
- // here we use linkcache:${domain}:${key} instead of this._createKey({ domain, key })
- // because the key can either be cached as case-sensitive or case-insensitive depending on the domain
- // so we should get the original key from the cache
- return await redis.get<RedisLinkProps>(`linkcache:${domain}:${key}`);
+ // we're using the special redisWithTimeout client in case Redis times out
+ try {
+ // here we use linkcache:${domain}:${key} instead of this._createKey({ domain, key })
+ // because the key can either be cached as case-sensitive or case-insensitive depending on the domain
+ // so we should get the original key from the cache
+ return await redisWithTimeout.get<RedisLinkProps>(
+ `linkcache:${domain}:${key}`,
+ );
+ } catch (error) {
+ console.error(
+ "[LinkCache]: Timeout getting cached link from Redis:",
+ error,
+ );
+ return null;
+ }
}
async delete({ domain, key }: Pick<LinkProps, "domain" | "key">) {
diff --git a/apps/web/lib/middleware/link.ts b/apps/web/lib/middleware/link.ts
index 9f4703c4a2..d48ca2b020 100644
--- a/apps/web/lib/middleware/link.ts
+++ b/apps/web/lib/middleware/link.ts
@@ -32,7 +32,6 @@ import { recordClickCache } from "../api/links/record-click-cache";
import { getLinkViaEdge } from "../planetscale";
import { getDomainViaEdge } from "../planetscale/get-domain-via-edge";
import { getPartnerAndDiscount } from "../planetscale/get-partner-discount";
-import { RedisLinkProps } from "../types";
import { cacheDeepLinkClickData } from "./utils/cache-deeplink-click-data";
import { crawlBitly } from "./utils/crawl-bitly";
import { isIosAppStoreUrl } from "./utils/is-ios-app-store-url";
@@ -80,21 +79,7 @@ export default async function LinkMiddleware(
});
}
- let cachedLink: RedisLinkProps | null = null;
- try {
- const controller = new AbortController();
- const timeoutId = setTimeout(() => controller.abort(), 1000);
-
- cachedLink = await linkCache.get({ domain, key });
- clearTimeout(timeoutId);
- } catch (error) {
- console.error(
- "[LinkMiddleware] – Timeout getting cached link from Redis:",
- error,
- );
- cachedLink = null;
- }
-
+ let cachedLink = await linkCache.get({ domain, key });
let isPartnerLink = Boolean(cachedLink?.programId && cachedLink?.partnerId);
if (!cachedLink) {
diff --git a/apps/web/lib/upstash/redis.ts b/apps/web/lib/upstash/redis.ts
index 663102445f..44821e5f8d 100644
--- a/apps/web/lib/upstash/redis.ts
+++ b/apps/web/lib/upstash/redis.ts
@@ -5,3 +5,10 @@ export const redis = new Redis({
url: process.env.UPSTASH_REDIS_REST_URL || "",
token: process.env.UPSTASH_REDIS_REST_TOKEN || "",
});
+
+// Special Redis instance with timeout
+export const redisWithTimeout = new Redis({
+ url: process.env.UPSTASH_REDIS_REST_URL || "",
+ token: process.env.UPSTASH_REDIS_REST_TOKEN || "",
+ signal: () => AbortSignal.timeout(1000),
+});
diff --git a/apps/web/package.json b/apps/web/package.json
index 2548507cea..2ebc7024d1 100644
--- a/apps/web/package.json
+++ b/apps/web/package.json
@@ -46,7 +46,7 @@
"@types/buffer-crc32": "0.2.0",
"@upstash/qstash": "^2.8.2",
"@upstash/ratelimit": "^2.0.5",
- "@upstash/redis": "^1.25.1",
+ "@upstash/redis": "^1.35.3",
"@vercel/edge-config": "^0.4.1",
"@vercel/functions": "^1.4.2",
"@vercel/og": "^0.6.5",
diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml
index 58b2ee83bc..7a673bb431 100644
--- a/pnpm-lock.yaml
+++ b/pnpm-lock.yaml
@@ -126,10 +126,10 @@ importers:
version: 2.8.2
'@upstash/ratelimit':
specifier: ^2.0.5
- version: 2.0.5(@upstash/[email protected])
+ version: 2.0.5(@upstash/[email protected])
'@upstash/redis':
- specifier: ^1.25.1
- version: 1.25.1
+ specifier: ^1.35.3
+ version: 1.35.3
'@vercel/edge-config':
specifier: ^0.4.1
version: 0.4.1
@@ -4760,11 +4760,8 @@ packages:
peerDependencies:
'@upstash/redis': ^1.34.3
- '@upstash/[email protected]':
- resolution: {integrity: sha512-ACj0GhJ4qrQyBshwFgPod6XufVEfKX2wcaihsEvSdLYnY+m+pa13kGt1RXm/yTHKf4TQi/Dy2A8z/y6WUEOmlg==}
-
- '@upstash/[email protected]':
- resolution: {integrity: sha512-bkxl2n7qls27hONXNyItM6ccRT0NBJ8c6zl83wx+TlODpYXehgeiUJTkcTaXLp7hk+zy6Mqpzpeo3GX+wZSvBw==}
+ '@upstash/[email protected]':
+ resolution: {integrity: sha512-hSjv66NOuahW3MisRGlSgoszU2uONAY2l5Qo3Sae8OT3/Tng9K+2/cBRuyPBX8egwEGcNNCF9+r0V6grNnhL+w==}
'@vercel/[email protected]':
resolution: {integrity: sha512-NRIBwfcS0bUoUbRWlNGetqjvLSwgYH/BqKqDN7vK1g32p7dN96k0712COgaz6VFizAm9b0g6IG6hR6+hc0KCPg==}
@@ -10043,6 +10040,9 @@ packages:
resolution: {integrity: sha512-61pPlCD9h51VoreyJ0BReideM3MDKMKnh6+V9L08331ipq6Q8OFXZYiqP6n/tbHx4s5I9uRhcye6BrbkizkBDw==}
+ resolution: {integrity: sha512-Ql87qFHB3s/De2ClA9e0gsnS6zXG27SkTiSJwjCc9MebbfapQfuPzumMIUMi38ezPZVNFcHI9sUIepeQfw8J8Q==}
+
resolution: {integrity: sha512-CLjCCLQ6UuMxWnbIylkisbRj31qxHPAurvena/0iwSVbQ2G1VY5/HjV0IRabOEbDHlzZlRdCrD4NhB0JtU40Pg==}
@@ -15339,7 +15339,7 @@ snapshots:
'@upstash/[email protected]':
dependencies:
- '@upstash/redis': 1.30.0
+ '@upstash/redis': 1.35.3
'@upstash/[email protected]':
dependencies:
@@ -15347,18 +15347,14 @@ snapshots:
jose: 5.10.0
neverthrow: 7.2.0
- '@upstash/[email protected](@upstash/[email protected])':
+ '@upstash/[email protected](@upstash/[email protected])':
dependencies:
'@upstash/core-analytics': 0.0.10
- '@upstash/redis': 1.25.1
+ '@upstash/redis': 1.35.3
- '@upstash/[email protected]':
+ '@upstash/[email protected]':
dependencies:
- crypto-js: 4.2.0
-
- '@upstash/[email protected]':
- dependencies:
- crypto-js: 4.2.0
+ uncrypto: 0.1.3
'@vercel/[email protected]': {}
@@ -21970,6 +21966,8 @@ snapshots:
has-symbols: 1.0.3
which-boxed-primitive: 1.0.2
+
dependencies:
base64-js: 1.5.1