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 withDeadline helper and refactor timeouts

Rafiec4526f3343f
diff --git a/apps/web/lib/api/partners/search/providers/resilience.ts b/apps/web/lib/api/partners/search/providers/resilience.ts
index 0b4595c696..28f65e0a33 100644
--- a/apps/web/lib/api/partners/search/providers/resilience.ts
+++ b/apps/web/lib/api/partners/search/providers/resilience.ts
@@ -41,29 +41,37 @@ export async function withTransientRetry<T>(
throw new Error("Partner search operation failed.");
}
-export async function withQueryDeadline<T>(
+/**
+ * Rejects if `operation` has not settled within `timeoutMs`. The operation is
+ * not cancelled — `Promise.race` cannot do that — so only pass work that
+ * settles on its own.
+ */
+export async function withDeadline<T>(
operation: () => Promise<T>,
timeoutMs: number,
+ message: string,
): Promise<T> {
let timeoutId: ReturnType<typeof setTimeout> | undefined;
const timeout = new Promise<never>((_, reject) => {
- timeoutId = setTimeout(
- () =>
- reject(
- new Error(`Partner search query timed out after ${timeoutMs}ms.`),
- ),
- timeoutMs,
- );
+ timeoutId = setTimeout(() => reject(new Error(message)), timeoutMs);
});
try {
- // A request timeout consumes nearly the full SLA budget, so only retry
- // transient failures such as rate limits and 503s that return quickly.
- return await Promise.race([
- withTransientRetry(operation, { retryTimeouts: false }),
- timeout,
- ]);
+ return await Promise.race([operation(), timeout]);
} finally {
clearTimeout(timeoutId);
}
}
+
+export async function withQueryDeadline<T>(
+ operation: () => Promise<T>,
+ timeoutMs: number,
+): Promise<T> {
+ return withDeadline(
+ // A request timeout consumes nearly the full SLA budget, so only retry
+ // transient failures such as rate limits and 503s that return quickly.
+ () => withTransientRetry(operation, { retryTimeouts: false }),
+ timeoutMs,
+ `Partner search query timed out after ${timeoutMs}ms.`,
+ );
+}
diff --git a/apps/web/lib/api/partners/search/providers/upstash-redis.ts b/apps/web/lib/api/partners/search/providers/upstash-redis.ts
index 196a5a3126..dae4bec770 100644
--- a/apps/web/lib/api/partners/search/providers/upstash-redis.ts
+++ b/apps/web/lib/api/partners/search/providers/upstash-redis.ts
@@ -15,7 +15,11 @@ import type {
PartnerSearchProvider,
} from "../types";
import { validatePartnerSearchCandidateLimit } from "../types";
-import { withQueryDeadline, withTransientRetry } from "./resilience";
+import {
+ withDeadline,
+ withQueryDeadline,
+ withTransientRetry,
+} from "./resilience";
import {
getEmailNgrams,
getQueryNgrams,
@@ -307,28 +311,11 @@ export function createUpstashRedisPartnerSearchProvider({
searchCandidates: findCandidates,
async waitForIndexing() {
- let timeoutId: ReturnType<typeof setTimeout> | undefined;
- const timeout = new Promise<never>((_, reject) => {
- timeoutId = setTimeout(
- () =>
- reject(
- new Error(
- `Partner search waitForIndexing timed out after ${WAIT_FOR_INDEXING_TIMEOUT_MS}ms.`,
- ),
- ),
- WAIT_FOR_INDEXING_TIMEOUT_MS,
- );
- });
-
- let result: number;
- try {
- result = await Promise.race([
- withTransientRetry(() => writeIndex.waitIndexing()),
- timeout,
- ]);
- } finally {
- clearTimeout(timeoutId);
- }
+ const result = await withDeadline(
+ () => withTransientRetry(() => writeIndex.waitIndexing()),
+ WAIT_FOR_INDEXING_TIMEOUT_MS,
+ `Partner search waitForIndexing timed out after ${WAIT_FOR_INDEXING_TIMEOUT_MS}ms.`,
+ );
if (result === 0) {
throw new Error(