Skip to content

Commit c57a679

Browse files
committed
fix(coding-plans): bound inventory credential validation concurrency
Replace the serial validation loop in uploadKeysToInventory with a p-limit(10) fan-out so large inventory uploads finish well within the request budget without firing one unbounded burst at the MiniMax API. Behavior is unchanged: malformed entries fail before validation, any invalid credential aborts the upload, and nothing is inserted on failure.
1 parent 6814511 commit c57a679

1 file changed

Lines changed: 14 additions & 6 deletions

File tree

apps/web/src/lib/coding-plans/index.ts

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import 'server-only';
33
import { createHash, createHmac } from 'node:crypto';
44
import { addDays } from 'date-fns';
55
import { and, eq, inArray, sql } from 'drizzle-orm';
6+
import pLimit from 'p-limit';
67

78
import { decryptApiKey, encryptApiKey } from '@/lib/ai-gateway/byok/encryption';
89
import { BYOK_ENCRYPTION_KEY } from '@/lib/config.server';
@@ -23,6 +24,11 @@ import {
2324
const logInfo = sentryLogger('coding-plans', 'info');
2425
const logError = sentryLogger('coding-plans', 'error');
2526

27+
// Credential validation calls the live MiniMax API per key. Bound the fan-out so
28+
// large inventory uploads finish within the request budget without overwhelming
29+
// the upstream provider with one unbounded burst of requests.
30+
const INVENTORY_VALIDATION_CONCURRENCY = 10;
31+
2632
type CancellationReason =
2733
| 'user_canceled'
2834
| 'insufficient_credits'
@@ -383,12 +389,14 @@ export async function uploadKeysToInventory(
383389

384390
const entries = rawEntries.map(parseInventoryCredentialEntry);
385391
const validateCredential = options.validateCredential ?? validateTokenPlanPlusCredential;
386-
for (const entry of entries) {
387-
if (!(await validateCredential(entry.apiKey))) {
388-
throw new Error(
389-
'One or more MiniMax credentials failed validation. Confirm plan access and supported model behavior, then try again.'
390-
);
391-
}
392+
const limit = pLimit(INVENTORY_VALIDATION_CONCURRENCY);
393+
const validationResults = await Promise.all(
394+
entries.map(entry => limit(() => validateCredential(entry.apiKey)))
395+
);
396+
if (validationResults.some(isValid => !isValid)) {
397+
throw new Error(
398+
'One or more MiniMax credentials failed validation. Confirm plan access and supported model behavior, then try again.'
399+
);
392400
}
393401

394402
const inserted = await db.transaction(async tx => {

0 commit comments

Comments
 (0)