Created
September 16, 2025 15:14
-
-
Save fago/fb29c76af3eb52bea080a5b2b6925ec2 to your computer and use it in GitHub Desktop.
nitro timeouts
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
| // server/plugins/alb-timeout-handler.ts | |
| export default defineNitroPlugin((nitroApp) => { | |
| // If ALB timeout is 60s, set app timeout to 58s | |
| const ALB_TIMEOUT = 60000 | |
| const APP_TIMEOUT = ALB_TIMEOUT - 2000 // 2s buffer | |
| nitroApp.hooks.hook('request', async (event) => { | |
| const timeout = setTimeout(() => { | |
| // Check if response was already sent | |
| if (!event.node.res.headersSent) { | |
| // Send response BEFORE ALB times out | |
| event.node.res.statusCode = 503 | |
| event.node.res.end(JSON.stringify({ | |
| error: 'Request timeout - processing took too long' | |
| })) | |
| } | |
| // Clean up any ongoing operations | |
| event.context.abortController?.abort() | |
| }, APP_TIMEOUT) | |
| // Clear timeout on response | |
| event.node.res.on('finish', () => clearTimeout(timeout)) | |
| // Store abort controller in context for cleanup | |
| event.context.abortController = new AbortController() | |
| }) | |
| }) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment