Skip to content

Commit 01b2a2a

Browse files
committed
fix(webhooks): eliminate redundant DB queries from webhook execution path
1 parent ff2a152 commit 01b2a2a

File tree

4 files changed

+135
-169
lines changed

4 files changed

+135
-169
lines changed

apps/sim/app/api/webhooks/trigger/[path]/route.test.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -324,7 +324,9 @@ vi.mock('@/lib/webhooks/processor', () => ({
324324
return null
325325
}
326326
),
327-
checkWebhookPreprocessing: vi.fn().mockResolvedValue(null),
327+
checkWebhookPreprocessing: vi
328+
.fn()
329+
.mockResolvedValue({ error: null, actorUserId: 'test-user-id' }),
328330
formatProviderErrorResponse: vi.fn().mockImplementation((_webhook, error, status) => {
329331
const { NextResponse } = require('next/server')
330332
return NextResponse.json({ error }, { status })

apps/sim/app/api/webhooks/trigger/[path]/route.ts

Lines changed: 19 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import { generateRequestId } from '@/lib/core/utils/request'
44
import {
55
checkWebhookPreprocessing,
66
findAllWebhooksForPath,
7-
formatProviderErrorResponse,
87
handlePreDeploymentVerification,
98
handleProviderChallenges,
109
handleProviderReachabilityTest,
@@ -74,57 +73,35 @@ export async function POST(
7473
const responses: NextResponse[] = []
7574

7675
for (const { webhook: foundWebhook, workflow: foundWorkflow } of webhooksForPath) {
77-
const authError = await verifyProviderAuth(
78-
foundWebhook,
79-
foundWorkflow,
80-
request,
81-
rawBody,
82-
requestId
83-
)
76+
// Short-circuit: reachability test is a quick body-only check
77+
const reachabilityResponse = handleProviderReachabilityTest(foundWebhook, body, requestId)
78+
if (reachabilityResponse) {
79+
return reachabilityResponse
80+
}
81+
82+
// Parallelize auth verification with preprocessing — they are independent
83+
// checkWebhookPreprocessing has its own try/catch and always returns WebhookPreprocessingResult
84+
const [authError, preprocessResult] = await Promise.all([
85+
verifyProviderAuth(foundWebhook, foundWorkflow, request, rawBody, requestId),
86+
checkWebhookPreprocessing(foundWorkflow, foundWebhook, requestId),
87+
])
88+
8489
if (authError) {
85-
// For multi-webhook, log and continue to next webhook
8690
if (webhooksForPath.length > 1) {
8791
logger.warn(`[${requestId}] Auth failed for webhook ${foundWebhook.id}, continuing to next`)
8892
continue
8993
}
9094
return authError
9195
}
9296

93-
const reachabilityResponse = handleProviderReachabilityTest(foundWebhook, body, requestId)
94-
if (reachabilityResponse) {
95-
// Reachability test should return immediately for the first webhook
96-
return reachabilityResponse
97-
}
98-
99-
let preprocessError: NextResponse | null = null
100-
try {
101-
preprocessError = await checkWebhookPreprocessing(foundWorkflow, foundWebhook, requestId)
102-
if (preprocessError) {
103-
if (webhooksForPath.length > 1) {
104-
logger.warn(
105-
`[${requestId}] Preprocessing failed for webhook ${foundWebhook.id}, continuing to next`
106-
)
107-
continue
108-
}
109-
return preprocessError
110-
}
111-
} catch (error) {
112-
logger.error(`[${requestId}] Unexpected error during webhook preprocessing`, {
113-
error: error instanceof Error ? error.message : String(error),
114-
stack: error instanceof Error ? error.stack : undefined,
115-
webhookId: foundWebhook.id,
116-
workflowId: foundWorkflow.id,
117-
})
118-
97+
if (preprocessResult.error) {
11998
if (webhooksForPath.length > 1) {
99+
logger.warn(
100+
`[${requestId}] Preprocessing failed for webhook ${foundWebhook.id}, continuing to next`
101+
)
120102
continue
121103
}
122-
123-
return formatProviderErrorResponse(
124-
foundWebhook,
125-
'An unexpected error occurred during preprocessing',
126-
500
127-
)
104+
return preprocessResult.error
128105
}
129106

130107
if (foundWebhook.blockId) {
@@ -152,6 +129,7 @@ export async function POST(
152129
const response = await queueWebhookExecution(foundWebhook, foundWorkflow, body, request, {
153130
requestId,
154131
path,
132+
actorUserId: preprocessResult.actorUserId,
155133
})
156134
responses.push(response)
157135
}

0 commit comments

Comments
 (0)