fiber icon indicating copy to clipboard operation
fiber copied to clipboard

🚀 feat: Add support for Timeout middleware configuration

Open gaby opened this issue 7 months ago • 2 comments

Summary

  • add Config struct for timeout middleware.
  • allow per-route timeouts, skip paths and custom timeout handler.
  • document timeout middleware configuration.
  • document timeout changes in what's new.

Fixes #3514

gaby avatar Jun 13 '25 14:06 gaby

Walkthrough

The timeout middleware has been refactored to use a new Config struct for configuration, replacing the previous positional arguments. This change enables per-route timeouts, path exclusions, custom timeout handlers, and more flexible error handling. Documentation and tests have been updated to reflect and verify the new configuration approach.

Changes

File(s) Change Summary
docs/middleware/timeout.md, docs/whats_new.md Updated documentation to describe the new Config struct, usage examples, and configuration options.
middleware/timeout/config.go Added new file defining the Config struct, defaults, and helper for applying defaults.
middleware/timeout/timeout.go Refactored middleware to use Config struct, supporting per-route timeouts, path skipping, etc.
middleware/timeout/timeout_test.go Refactored and extended tests to use the new Config struct and cover new configuration scenarios.

Sequence Diagram(s)

sequenceDiagram
    participant Client
    participant FiberApp
    participant TimeoutMiddleware
    participant Handler

    Client->>FiberApp: Sends HTTP Request
    FiberApp->>TimeoutMiddleware: Passes request
    TimeoutMiddleware->>TimeoutMiddleware: Check SkipPaths, Next, Routes in Config
    alt Path/Next skips timeout
        TimeoutMiddleware->>Handler: Call handler directly
    else Timeout applies
        TimeoutMiddleware->>Handler: Run handler with context timeout
        alt Handler completes in time
            Handler-->>TimeoutMiddleware: Response
            TimeoutMiddleware-->>FiberApp: Pass response
        else Timeout occurs
            TimeoutMiddleware->>TimeoutMiddleware: Call OnTimeout (if set)
            TimeoutMiddleware-->>FiberApp: Return timeout error/response
        end
    end
    FiberApp-->>Client: Responds

Assessment against linked issues

Objective Addressed Explanation
Add configurable timeout middleware with per-route timeouts, path exclusions, custom handler (#3514)
Maintain backward compatibility with existing timeout.New() usage (#3514) The New function signature was changed from positional args to variadic config, which may break existing calls.
Provide thorough tests and documentation for new configuration options (#3514)

Assessment against linked issues: Out-of-scope changes

Code Change Explanation
Refactoring of New function signature in middleware/timeout/timeout.go The linked issue specifies adding NewWithConfig for new config, not changing the existing New.
Removal of old positional-argument-based API in favor of config struct in all usage/tests The issue requests backward compatibility; removing or changing old API may break existing code.

Possibly related PRs

  • gofiber/fiber#3382: Refactors timeout middleware to use Config struct and leverages fiber.Ctx as a context.Context, which is directly related to this PR’s changes.
  • gofiber/fiber#3275: Modifies timeout middleware’s New function and error handling; this PR builds on and expands those changes.

Suggested reviewers

  • sixcolors
  • efectn
  • ReneWerner87

Poem

In the garden where timeouts grow,
A rabbit hops with configs in tow.
Per-route, per-path, it skips with glee,
Custom handlers for all to see!
With tests and docs, the patch is neat—
This middleware’s upgrade is quite the treat!
🐇✨

✨ Finishing Touches
  • [ ] 📝 Generate Docstrings

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Explain this complex logic.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai explain this code block.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and explain its main purpose.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai generate sequence diagram to generate a sequence diagram of the changes in this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

coderabbitai[bot] avatar Jun 13 '25 14:06 coderabbitai[bot]

Codecov Report

Attention: Patch coverage is 90.00000% with 6 lines in your changes missing coverage. Please review.

Project coverage is 83.91%. Comparing base (670fbd5) to head (e6a16fe).

Files with missing lines Patch % Lines
middleware/timeout/config.go 89.65% 2 Missing and 1 partial :warning:
middleware/timeout/timeout.go 90.32% 2 Missing and 1 partial :warning:
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #3518      +/-   ##
==========================================
+ Coverage   83.82%   83.91%   +0.08%     
==========================================
  Files         120      121       +1     
  Lines       12286    12331      +45     
==========================================
+ Hits        10299    10347      +48     
+ Misses       1561     1559       -2     
+ Partials      426      425       -1     
Flag Coverage Δ
unittests 83.91% <90.00%> (+0.08%) :arrow_up:

Flags with carried forward coverage won't be shown. Click here to find out more.

:umbrella: View full report in Codecov by Sentry.
:loudspeaker: Have feedback on the report? Share it here.

:rocket: New features to boost your workflow:
  • :snowflake: Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

codecov[bot] avatar Jun 13 '25 14:06 codecov[bot]

Note: Add support for Next()

gaby avatar Jun 26 '25 14:06 gaby

@gaby can you solve the syntax errors

ReneWerner87 avatar Jun 30 '25 07:06 ReneWerner87

https://github.com/gofiber/fiber/pull/3604

ReneWerner87 avatar Jul 20 '25 12:07 ReneWerner87