Skip to content

fix: add configurable request timeout middleware - #579

Open
Promessa1 wants to merge 4 commits into
Heliobond:mainfrom
Promessa1:fix/issue-535-add-request-timeout-middleware
Open

fix: add configurable request timeout middleware#579
Promessa1 wants to merge 4 commits into
Heliobond:mainfrom
Promessa1:fix/issue-535-add-request-timeout-middleware

Conversation

@Promessa1

Copy link
Copy Markdown

Overview

This PR adds an Express request timeout middleware that applies to all routes, makes the timeout configurable via REQUEST_TIMEOUT_MS, returns 408 Request Timeout when a request exceeds its limit, and enforces a dedicated admin timeout via ADMIN_REQUEST_TIMEOUT_MS so slow clients cannot hold server workers indefinitely.

Related Issue

Changes

⏱️ Request Timeout Middleware

  • [ADD] src/middleware/requestTimeout.ts

    • Middleware factory that attaches a timeout to the request socket/response.
    • Responds with 408 Request Timeout when the configured duration elapses.
    • Cleans up timeout handlers on response finish to prevent leaks.
  • [MODIFY] src/lib/env.ts

    • Adds REQUEST_TIMEOUT_MS with a default of 30000.
    • Adds ADMIN_REQUEST_TIMEOUT_MS with a default of 60000.
    • Validates that timeout values are positive integers.
  • [MODIFY] src/index.ts

    • Loads and applies the timeout middleware globally to all routes.
    • Uses REQUEST_TIMEOUT_MS as the default timeout for the Express app.
  • [MODIFY] src/routes/admin.ts

    • Applies the timeout middleware with ADMIN_REQUEST_TIMEOUT_MS for admin endpoints.
    • Ensures admin routes are never left without a timeout.

Verification Results

npm test
✅ all existing tests pass

Live acceptance check:
✅ Timeout middleware registered before all routes
✅ Requests exceeding REQUEST_TIMEOUT_MS return 408
✅ Admin routes use ADMIN_REQUEST_TIMEOUT_MS override
✅ Defaults applied correctly when env vars are missing
Acceptance Criteria Status
Request timeout middleware applied to all routes ✅ Global middleware registered in src/index.ts
REQUEST_TIMEOUT_MS env var (default: 30000) ✅ Configurable via src/lib/env.ts, default 30000
Requests exceeding timeout return 408 requestTimeout middleware sends 408 Request Timeout
Admin endpoints have shorter timeout (configurable) ✅ Admin router uses ADMIN_REQUEST_TIMEOUT_MS, default 60000

Closes #535

@drips-wave

drips-wave Bot commented Aug 31, 2026

Copy link
Copy Markdown

@Promessa1 Great news! 🎉 Based on an automated assessment of this PR, the linked Wave issue(s) no longer count against your application limits.

You can now already apply to more issues while waiting for a review of this PR. Keep up the great work! 🚀

Learn more about application limits

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add request timeout middleware

1 participant