-
Notifications
You must be signed in to change notification settings - Fork 3.4k
fix: add pagination to GET /api/workflows to prevent OOM on large workspaces #3578
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
base: main
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,7 @@ | ||
| import { db } from '@sim/db' | ||
| import { permissions, workflow, workflowFolder } from '@sim/db/schema' | ||
| import { createLogger } from '@sim/logger' | ||
| import { and, asc, eq, inArray, isNull, min } from 'drizzle-orm' | ||
| import { and, asc, eq, gt, inArray, isNull, min } from 'drizzle-orm' | ||
| import { type NextRequest, NextResponse } from 'next/server' | ||
| import { z } from 'zod' | ||
| import { AuditAction, AuditResourceType, recordAudit } from '@/lib/audit/log' | ||
|
|
@@ -27,6 +27,9 @@ export async function GET(request: NextRequest) { | |
| const startTime = Date.now() | ||
| const url = new URL(request.url) | ||
| const workspaceId = url.searchParams.get('workspaceId') | ||
| const cursor = url.searchParams.get('cursor') | ||
cursor[bot] marked this conversation as resolved.
Show resolved
Hide resolved
Contributor
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.
The The To properly implement cursor-based offset, the WHERE clause needs to be extended, e.g.: // In the workspaceId branch
.where(
cursor
? and(eq(workflow.workspaceId, workspaceId), gt(workflow.id, cursor))
: eq(workflow.workspaceId, workspaceId)
)Note also that using |
||
| const limitParam = url.searchParams.get('limit') | ||
| const limit = Math.min(Math.max(parseInt(limitParam || '100', 10) || 100, 1), 500) | ||
|
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. Silent data truncation for callers expecting all workflowsHigh Severity The new default |
||
|
|
||
| try { | ||
| const auth = await checkSessionOrInternalAuth(request, { requireWorkflowId: false }) | ||
|
|
@@ -66,12 +69,16 @@ export async function GET(request: NextRequest) { | |
|
|
||
| const orderByClause = [asc(workflow.sortOrder), asc(workflow.createdAt), asc(workflow.id)] | ||
|
|
||
| // Fetch limit+1 to detect if there are more pages | ||
| const fetchLimit = limit + 1 | ||
|
|
||
| if (workspaceId) { | ||
| workflows = await db | ||
| .select() | ||
| .from(workflow) | ||
| .where(eq(workflow.workspaceId, workspaceId)) | ||
| .orderBy(...orderByClause) | ||
| .limit(fetchLimit) | ||
| } else { | ||
| const workspacePermissionRows = await db | ||
| .select({ workspaceId: permissions.entityId }) | ||
|
|
@@ -86,9 +93,15 @@ export async function GET(request: NextRequest) { | |
| .from(workflow) | ||
| .where(inArray(workflow.workspaceId, workspaceIds)) | ||
| .orderBy(...orderByClause) | ||
| .limit(fetchLimit) | ||
| } | ||
|
|
||
| return NextResponse.json({ data: workflows }, { status: 200 }) | ||
| // Determine if there are more results and set cursor | ||
| const hasMore = workflows.length > limit | ||
| const data = hasMore ? workflows.slice(0, limit) : workflows | ||
| const nextCursor = hasMore ? data[data.length - 1]?.id : null | ||
|
||
|
|
||
| return NextResponse.json({ data, nextCursor }, { status: 200 }) | ||
| } catch (error: any) { | ||
| const elapsed = Date.now() - startTime | ||
| logger.error(`[${requestId}] Workflow fetch error after ${elapsed}ms`, error) | ||
|
|
||


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.
Unused
gtimportgtis imported fromdrizzle-ormbut is never referenced in the file. This will produce a lint/compile warning and should be removed until the cursor WHERE clause is actually implemented.