From 78b6127d885d9c251259f6b27c32129b159410ce Mon Sep 17 00:00:00 2001 From: Nicolas Date: Sun, 13 Oct 2024 22:27:38 -0300 Subject: [PATCH 1/2] Nick: retries for acuc --- apps/api/src/controllers/auth.ts | 62 ++++++++++++++----- .../src/services/billing/credit_billing.ts | 6 +- 2 files changed, 51 insertions(+), 17 deletions(-) diff --git a/apps/api/src/controllers/auth.ts b/apps/api/src/controllers/auth.ts index 0367358f..5546bc17 100644 --- a/apps/api/src/controllers/auth.ts +++ b/apps/api/src/controllers/auth.ts @@ -37,12 +37,17 @@ function normalizedApiIsUuid(potentialUuid: string): boolean { return validate(potentialUuid); } -export async function setCachedACUC(api_key: string, acuc: AuthCreditUsageChunk | ((acuc: AuthCreditUsageChunk) => AuthCreditUsageChunk)) { +export async function setCachedACUC( + api_key: string, + acuc: + | AuthCreditUsageChunk + | ((acuc: AuthCreditUsageChunk) => AuthCreditUsageChunk) +) { const cacheKeyACUC = `acuc_${api_key}`; const redLockKey = `lock_${cacheKeyACUC}`; try { - await redlock.using([redLockKey], 10000, {}, async signal => { + await redlock.using([redLockKey], 10000, {}, async (signal) => { if (typeof acuc === "function") { acuc = acuc(JSON.parse(await getValue(cacheKeyACUC))); @@ -68,7 +73,10 @@ export async function setCachedACUC(api_key: string, acuc: AuthCreditUsageChunk } } -export async function getACUC(api_key: string, cacheOnly = false): Promise { +export async function getACUC( + api_key: string, + cacheOnly = false +): Promise { const cacheKeyACUC = `acuc_${api_key}`; const cachedACUC = await getValue(cacheKeyACUC); @@ -76,18 +84,38 @@ export async function getACUC(api_key: string, cacheOnly = false): Promise setTimeout(resolve, 200)); } - const chunk: AuthCreditUsageChunk | null = data.length === 0 - ? null - : data[0].team_id === null - ? null - : data[0]; + const chunk: AuthCreditUsageChunk | null = + data.length === 0 ? null : data[0].team_id === null ? null : data[0]; // NOTE: Should we cache null chunks? - mogery if (chunk !== null) { @@ -132,7 +160,11 @@ export async function supaAuthenticateUser( plan?: PlanType; chunk?: AuthCreditUsageChunk; }> { - const authHeader = req.headers.authorization ?? (req.headers["sec-websocket-protocol"] ? `Bearer ${req.headers["sec-websocket-protocol"]}` : null); + const authHeader = + req.headers.authorization ?? + (req.headers["sec-websocket-protocol"] + ? `Bearer ${req.headers["sec-websocket-protocol"]}` + : null); if (!authHeader) { return { success: false, error: "Unauthorized", status: 401 }; } @@ -162,7 +194,7 @@ export async function supaAuthenticateUser( rateLimiter = getRateLimiter(RateLimiterMode.CrawlStatus, token); } else { rateLimiter = getRateLimiter(RateLimiterMode.Preview, token); - } + } teamId = "preview"; } else { normalizedApi = parseApi(token); diff --git a/apps/api/src/services/billing/credit_billing.ts b/apps/api/src/services/billing/credit_billing.ts index 3346e291..39e9f15e 100644 --- a/apps/api/src/services/billing/credit_billing.ts +++ b/apps/api/src/services/billing/credit_billing.ts @@ -55,11 +55,13 @@ export async function supaCheckTeamCredits(chunk: AuthCreditUsageChunk, team_id: const creditsWillBeUsed = chunk.adjusted_credits_used + credits; + // In case chunk.price_credits is undefined, set it to a large number to avoid division by zero + const totalPriceCredits = chunk.price_credits ?? 100000000; // Removal of + credits - const creditUsagePercentage = creditsWillBeUsed / chunk.price_credits; + const creditUsagePercentage = creditsWillBeUsed / totalPriceCredits; // Compare the adjusted total credits used with the credits allowed by the plan - if (creditsWillBeUsed > chunk.price_credits) { + if (creditsWillBeUsed > totalPriceCredits) { sendNotification( team_id, NotificationType.LIMIT_REACHED, From 821c62c5750d66e5df895603bb3df874393e2380 Mon Sep 17 00:00:00 2001 From: Nicolas Date: Sun, 13 Oct 2024 22:30:11 -0300 Subject: [PATCH 2/2] Update credit_billing.ts --- apps/api/src/services/billing/credit_billing.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/apps/api/src/services/billing/credit_billing.ts b/apps/api/src/services/billing/credit_billing.ts index 39e9f15e..694d0e5c 100644 --- a/apps/api/src/services/billing/credit_billing.ts +++ b/apps/api/src/services/billing/credit_billing.ts @@ -55,7 +55,7 @@ export async function supaCheckTeamCredits(chunk: AuthCreditUsageChunk, team_id: const creditsWillBeUsed = chunk.adjusted_credits_used + credits; - // In case chunk.price_credits is undefined, set it to a large number to avoid division by zero + // In case chunk.price_credits is undefined, set it to a large number to avoid mistakes const totalPriceCredits = chunk.price_credits ?? 100000000; // Removal of + credits const creditUsagePercentage = creditsWillBeUsed / totalPriceCredits;