Skip to content

Instantly share code, notes, and snippets.

@fago
Created September 16, 2025 15:14
Show Gist options
  • Select an option

  • Save fago/fb29c76af3eb52bea080a5b2b6925ec2 to your computer and use it in GitHub Desktop.

Select an option

Save fago/fb29c76af3eb52bea080a5b2b6925ec2 to your computer and use it in GitHub Desktop.
nitro timeouts
// 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