-
Notifications
You must be signed in to change notification settings - Fork 0
⚡ Bolt: Parallelize ZimaOS gateway calls in Dashboard APIs #70
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
bobdivx
wants to merge
1
commit into
dev
Choose a base branch
from
bolt-dashboard-api-parallelization-4885795820239263845
base: dev
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| ## 2025-05-28 - Parallelizing Gateway Calls and DB Queries | ||
| **Learning:** In Astro API routes serving frontend dashboards, sequentially executing slow external checks (like `fetchZimaOSSessionsPayload` and `getPrimaryDevServerStatus`) after database operations acts as a severe performance bottleneck because the network requests wait idly during DB I/O. | ||
| **Action:** Always initiate slow external network or gateway calls at the very beginning of the request handler (`const fetchPromise = fetchSomething().catch(e => e)`) so they execute concurrently with local Drizzle ORM queries (`Promise.all([db.select()...])`), and `await` the external response only at the end before returning the payload. Additionally, when processing independent I/O checks across a list of database results (like scanning projects), map the logic into an array of Promises and sequentially `await` them inside a `for` loop to preserve order while maximizing concurrency. | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -31,6 +31,9 @@ function mapSessionToRunState(raw: Record<string, unknown>): { running: boolean | |||||||||||
| export const GET: APIRoute = async ({ locals }) => { | ||||||||||||
| const email = locals.user?.email as string | undefined; | ||||||||||||
|
|
||||||||||||
| // Démarre la requête gateway ZimaOS le plus tôt possible pour qu'elle s'exécute en parallèle | ||||||||||||
| const zimaosFetchPromise = fetchZimaOSSessionsPayload(email).catch((e) => e); | ||||||||||||
|
|
||||||||||||
| const payload: { | ||||||||||||
| projects: Array<{ | ||||||||||||
| id: number; | ||||||||||||
|
|
@@ -69,9 +72,16 @@ export const GET: APIRoute = async ({ locals }) => { | |||||||||||
|
|
||||||||||||
| try { | ||||||||||||
| const { db, Project, AgentTask } = await loadAstroDb(); | ||||||||||||
| const projects = await db.select().from(Project).orderBy(desc(Project.updatedAt)).limit(12); | ||||||||||||
|
|
||||||||||||
| const tasksAll = await db.select().from(AgentTask).limit(500); | ||||||||||||
| // Parallélise les requêtes DB (Project et AgentTask) et getWorkSystemStatus | ||||||||||||
| const [projectsRes, tasksAll, workScheduler] = await Promise.all([ | ||||||||||||
| db.select().from(Project).orderBy(desc(Project.updatedAt)).limit(12), | ||||||||||||
| db.select().from(AgentTask).limit(500), | ||||||||||||
| getWorkSystemStatus() | ||||||||||||
| ]); | ||||||||||||
|
|
||||||||||||
| const projects = projectsRes as ProjectRow[]; | ||||||||||||
| payload.swarm.workScheduler = workScheduler; | ||||||||||||
|
|
||||||||||||
| const countForProject = (pid: number | null | undefined) => { | ||||||||||||
| const pend = tasksAll.filter( | ||||||||||||
|
|
@@ -83,7 +93,8 @@ export const GET: APIRoute = async ({ locals }) => { | |||||||||||
| return { pendingOrRunning: pend.length, running }; | ||||||||||||
| }; | ||||||||||||
|
|
||||||||||||
| for (const p of projects as ProjectRow[]) { | ||||||||||||
| // Prépare les promesses pour la résolution I/O des projets en parallèle | ||||||||||||
| const projectPromises = projects.map(async (p) => { | ||||||||||||
| let dev: { | ||||||||||||
| ok: boolean; | ||||||||||||
| running?: boolean; | ||||||||||||
|
|
@@ -131,16 +142,20 @@ export const GET: APIRoute = async ({ locals }) => { | |||||||||||
| dev.hint = 'Erreur lecture disque'; | ||||||||||||
| } | ||||||||||||
|
|
||||||||||||
| payload.projects.push({ | ||||||||||||
| return { | ||||||||||||
| id: p.id, | ||||||||||||
| name: p.name, | ||||||||||||
| swarmEnabled: Number(p.swarmEnabled) === 1, | ||||||||||||
| devServer: dev, | ||||||||||||
| tasks: countForProject(p.id), | ||||||||||||
| }); | ||||||||||||
| }; | ||||||||||||
| }); | ||||||||||||
|
|
||||||||||||
| // Attendre les résultats dans le même ordre pour préserver l'ordre du tri DB (desc(Project.updatedAt)) | ||||||||||||
| for (const promise of projectPromises) { | ||||||||||||
| payload.projects.push(await promise); | ||||||||||||
| } | ||||||||||||
|
Comment on lines
+154
to
157
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Using
Suggested change
|
||||||||||||
|
|
||||||||||||
| payload.swarm.workScheduler = await getWorkSystemStatus(); | ||||||||||||
| } catch (e) { | ||||||||||||
| payload.dbError = e instanceof Error ? e.message : String(e); | ||||||||||||
| return new Response(JSON.stringify(payload), { | ||||||||||||
|
|
@@ -150,7 +165,10 @@ export const GET: APIRoute = async ({ locals }) => { | |||||||||||
| } | ||||||||||||
|
|
||||||||||||
| try { | ||||||||||||
| const oc = await fetchZimaOSSessionsPayload(email); | ||||||||||||
| const oc = await zimaosFetchPromise; | ||||||||||||
| if (oc instanceof Error) { | ||||||||||||
| throw oc; | ||||||||||||
| } | ||||||||||||
| payload.swarm.zimaosOk = oc.ok; | ||||||||||||
| const sessions = oc.ok | ||||||||||||
| ? (normalizeZimaOSSessions(oc.data) as Record<string, unknown>[]) | ||||||||||||
|
|
||||||||||||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Update the documentation to recommend using
Promise.all()instead of sequentially awaiting promises in aforloop, asPromise.all()is the standard and idiomatic way to handle concurrent promises while preserving their order.