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 partner counts from expensive prefixes

Rafi0892e12fe31d
diff --git a/apps/web/lib/api/partners/get-partners-count.ts b/apps/web/lib/api/partners/get-partners-count.ts
index c93221e3c5..da08c28225 100644
--- a/apps/web/lib/api/partners/get-partners-count.ts
+++ b/apps/web/lib/api/partners/get-partners-count.ts
@@ -283,7 +283,11 @@ export async function getPartnersCount<T>(
!databaseOnlyFilters
) {
try {
- return (await searchProvider.countCandidates(candidateQuery)) as T;
+ const total = await searchProvider.countCandidates(candidateQuery);
+
+ if (typeof total === "number") {
+ return total as T;
+ }
} catch (error) {
console.error(
"[Partner Search] Count aggregation failed, falling back to counting candidates.",
diff --git a/apps/web/lib/api/partners/search/providers/turbopuffer.ts b/apps/web/lib/api/partners/search/providers/turbopuffer.ts
index 53a48e0f1a..d45385270c 100644
--- a/apps/web/lib/api/partners/search/providers/turbopuffer.ts
+++ b/apps/web/lib/api/partners/search/providers/turbopuffer.ts
@@ -22,6 +22,18 @@ import { getEmailNgrams, getQueryNgrams } from "./shared";
*/
const NAMESPACE = "partner-search-v3";
const WRITE_BATCH_SIZE = 500;
+
+/**
+ * Below this, the counting aggregation is not worth attempting.
+ *
+ * The final token is a prefix, so a short one expands to every term beginning
+ * with it. Measured against 626K documents: one character takes the count from
+ * ~50ms to ~1.2s, past the query deadline on every attempt, and two characters
+ * are still 2-3x slower than three. Production holds roughly 1.6M documents, so
+ * the margin matters more there than the extra character costs — the first
+ * keystrokes of a search fall back to the database count instead.
+ */
+const MIN_COUNT_PREFIX_LENGTH = 3;
const QUERY_OPERATION_TIMEOUT_MS = 1_000;
/**
@@ -368,6 +380,12 @@ export function createTurbopufferPartnerSearchProvider({
async countCandidates({ programId, query, filters }) {
const normalizedQuery = normalizePartnerSearchQuery(query);
+ const lastToken = normalizedQuery.split(/\s+/u).at(-1) ?? "";
+
+ if (lastToken.length < MIN_COUNT_PREFIX_LENGTH) {
+ return null;
+ }
+
const response = await withQueryDeadline(
() =>
resolvedNamespace.query({
diff --git a/apps/web/lib/api/partners/search/types.ts b/apps/web/lib/api/partners/search/types.ts
index b5d0732fc4..d31a611870 100644
--- a/apps/web/lib/api/partners/search/types.ts
+++ b/apps/web/lib/api/partners/search/types.ts
@@ -105,8 +105,11 @@ export interface PartnerSearchProvider {
searchCandidates(
query: PartnerSearchCandidateQuery,
): Promise<PartnerSearchResult>;
- /** How many documents match, ignoring `limit`. */
- countCandidates(query: PartnerSearchCandidateQuery): Promise<number>;
+ /**
+ * How many documents match, ignoring `limit`. Null when the provider cannot
+ * answer cheaply, which the caller handles by counting another way.
+ */
+ countCandidates(query: PartnerSearchCandidateQuery): Promise<number | null>;
upsert(documents: PartnerSearchDocument[]): Promise<void>;
delete(documentIds: string[]): Promise<void>;
}
diff --git a/apps/web/tests/partners/get-partners-count-search.test.ts b/apps/web/tests/partners/get-partners-count-search.test.ts
index fe7d29c136..74f190ea27 100644
--- a/apps/web/tests/partners/get-partners-count-search.test.ts
+++ b/apps/web/tests/partners/get-partners-count-search.test.ts
@@ -95,6 +95,21 @@ describe("getPartnersCount search", () => {
},
);
+ it("falls back to the database count when the provider declines to count", async () => {
+ const searchProvider = createSearchProvider();
+ (
+ searchProvider.countCandidates as ReturnType<typeof vi.fn>
+ ).mockResolvedValue(null);
+ mocks.count.mockResolvedValue(5);
+
+ const count = await getPartnersCount<number>(
+ { programId: "prog_test", search: "a" },
+ { searchProvider },
+ );
+
+ expect(count).toBe(5);
+ });
+
it("falls back to the database count when the aggregation fails", async () => {
const searchProvider = createSearchProvider();
(
diff --git a/apps/web/tests/partners/turbopuffer-partner-search-provider.test.ts b/apps/web/tests/partners/turbopuffer-partner-search-provider.test.ts
index cb224322cc..c92ce5e499 100644
--- a/apps/web/tests/partners/turbopuffer-partner-search-provider.test.ts
+++ b/apps/web/tests/partners/turbopuffer-partner-search-provider.test.ts
@@ -32,6 +32,7 @@ const document: PartnerSearchDocument = {
const mocks = vi.hoisted(() => ({
write: vi.fn(),
multiQuery: vi.fn(),
+ query: vi.fn(),
deleteAll: vi.fn(),
}));
@@ -39,6 +40,7 @@ function createNamespaceMock(): TurbopufferNamespace {
return {
write: mocks.write,
multiQuery: mocks.multiQuery,
+ query: mocks.query,
deleteAll: mocks.deleteAll,
} as unknown as TurbopufferNamespace;
}
@@ -74,6 +76,7 @@ describe("Turbopuffer partner search provider", () => {
}
mocks.write.mockResolvedValue({ rows_affected: 1 });
mocks.multiQuery.mockResolvedValue({ results: [] });
+ mocks.query.mockResolvedValue({ aggregations: { total: 12_000 } });
});
// In afterEach rather than the test body, so a failed assertion cannot leak
@@ -387,6 +390,44 @@ describe("Turbopuffer partner search provider", () => {
).rejects.toThrow("Partner search candidate limit");
});
+ it("counts matches without the candidate ceiling", async () => {
+ const total = await createProvider().countCandidates({
+ programId: "prog_test",
+ query: "creator",
+ limit: 10,
+ filters: { status: { values: ["approved"] } },
+ });
+
+ expect(total).toBe(12_000);
+
+ const [request] = mocks.query.mock.calls[0];
+ expect(request.aggregate_by).toEqual({ total: ["Count"] });
+ // One clause covers both text branches, since identityText holds a subset
+ // of what searchText holds.
+ const filters = JSON.stringify(request.filters);
+ expect(filters).toContain('["searchText","ContainsAnyToken","creator"');
+ expect(filters).toContain('["status","In",["approved"]]');
+ });
+
+ it.each([
+ ["a single character", "a"],
+ ["a half-typed final token", "steven a"],
+ ])(
+ "declines to count %s, which the prefix expands too far",
+ async (_label, query) => {
+ // Measured against 626K documents: a one-character prefix takes the
+ // aggregation from ~50ms to ~1.2s, past the deadline every time.
+ const total = await createProvider().countCandidates({
+ programId: "prog_test",
+ query,
+ limit: 10,
+ });
+
+ expect(total).toBeNull();
+ expect(mocks.query).not.toHaveBeenCalled();
+ },
+ );
+
it("deletes by document ID", async () => {
await createProvider().delete(["pge_1", "pge_2"]);