Supercharge your AI agents with data from the web and beyond. Building the library for superintelligence. 🔥
TypeScript aiai-agentsai-crawlerai-scrapingai-searchcrawlerdata-extractionhtml-to-markdownllmmarkdownscraperscrapingweb-crawlerweb-dataweb-data-extractionweb-scraperweb-scrapingweb-searchwebscraping
(feat/interact) Share the same number of concurrent browser (#3223)
Nicolas6ddf223b9523
diff --git a/apps/api/src/controllers/v2/scrape-browser.ts b/apps/api/src/controllers/v2/scrape-browser.tsindex 1af1f9d01..bd7cb604b 100644--- a/apps/api/src/controllers/v2/scrape-browser.ts+++ b/apps/api/src/controllers/v2/scrape-browser.ts@@ -11,14 +11,17 @@ import {updateBrowserSessionCreditsUsed,updateBrowserSessionScrapeId,claimBrowserSessionDestroyed,- getActiveBrowserSessionCount,invalidateActiveBrowserSessionCount,- MAX_ACTIVE_BROWSER_SESSIONS_PER_TEAM,getBrowserSessionFromScrape,markBrowserSessionUsedPrompt,didBrowserSessionUsePrompt,clearBrowserSessionPromptFlag,} from "../../lib/browser-sessions";+import {+ getConcurrencyLimitActiveJobsCount,+ pushConcurrencyLimitActiveJob,+ removeConcurrencyLimitActiveJob,+} from "../../lib/concurrency-limit";import {browserServiceRequest,BrowserServiceError,@@ -350,6 +353,13 @@ export async function scrapeStopInteractiveBrowserController(const claimed = await claimBrowserSessionDestroyed(session.id);invalidateActiveBrowserSessionCount(session.team_id).catch(() => {});+ removeConcurrencyLimitActiveJob(session.team_id, session.id).catch(error => {+ logger.error("Failed to remove concurrency limiter entry for browser session", {+ error,+ sessionId: session.id,+ teamId: session.team_id,+ });+ });if (!claimed) {logger.info("Session already destroyed by another path, skipping billing", {@@ -463,14 +473,17 @@ async function createSessionForScrape(};}- // Active session limit- const activeCount = await getActiveBrowserSessionCount(req.auth.team_id);- if (activeCount >= MAX_ACTIVE_BROWSER_SESSIONS_PER_TEAM) {+ // Active session limit — uses the same concurrency pool as scrape/crawl+ const concurrencyLimit = req.acuc?.concurrency ?? 2;+ const activeCount = await getConcurrencyLimitActiveJobsCount(+ req.auth.team_id,+ );+ if (activeCount >= concurrencyLimit) {return {status: 429,body: {success: false,- error: `You have reached the maximum number of active browser sessions (${MAX_ACTIVE_BROWSER_SESSIONS_PER_TEAM}). Please destroy existing sessions before creating new ones.`,+ error: `You have reached the maximum number of concurrent jobs (${concurrencyLimit}). Please wait for existing jobs to complete or destroy browser sessions before creating new ones.`,},error: true,};@@ -665,6 +678,14 @@ async function createSessionForScrape(invalidateActiveBrowserSessionCount(req.auth.team_id).catch(() => {});+ // Register in the shared concurrency limiter so this session counts+ // against the team's concurrent job limit while it's active.+ pushConcurrencyLimitActiveJob(+ req.auth.team_id,+ sessionId,+ ttl * 1000,+ ).catch(() => {});+return { session };} catch (err) {logger.error("Failed to persist browser session, cleaning up", {