Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(timeout): unify and enhance timeout middleware #3275

Merged
merged 7 commits into from
Jan 8, 2025

Conversation

ReneWerner87
Copy link
Member

@ReneWerner87 ReneWerner87 commented Jan 7, 2025

  • Enhance error handling for deadline and custom errors
  • Update comments for clarity and maintainability

- Combine classic context-based timeout with a Goroutine + channel approach
- Support custom error list without additional parameters
- Return fiber.ErrRequestTimeout for timeouts or listed errors
@ReneWerner87 ReneWerner87 added this to the v3 milestone Jan 7, 2025
@ReneWerner87 ReneWerner87 requested a review from a team as a code owner January 7, 2025 18:43
@ReneWerner87 ReneWerner87 requested review from gaby, sixcolors and efectn and removed request for a team January 7, 2025 18:43
Copy link
Contributor

coderabbitai bot commented Jan 7, 2025

Walkthrough

The timeout middleware in the timeout.go file has been refactored to enhance its timeout handling logic. The parameter t has been renamed to timeout for clarity. The function now utilizes a context with a specified timeout, which is canceled if the operation exceeds the deadline. It checks for timeout conditions after executing the handler and introduces a new helper function for error checking against custom errors. The return value has been adjusted to appropriately reflect the handler's error or a timeout error.

Changes

File Change Summary
middleware/timeout/timeout.go - Renamed parameter t to timeout for clarity
- Updated function comments
- Restructured internal logic to use context for timeout
- Added runHandler and isCustomError helper functions for error checking
- Adjusted return value to reflect handler's error or timeout error
middleware/timeout/timeout_test.go - Removed Test_WithContextTimeout function
- Added TestTimeout_Success, TestTimeout_Exceeded, TestTimeout_CustomError, TestTimeout_UnmatchedError, TestTimeout_ZeroDuration functions
- Modified sleepWithContext function for cleanup and error handling

Poem

🐰 Timeout Tango, a Rabbit's Delight
Goroutines dance, errors take flight
Middleware magic, context so bright
Timeout whispers, handler's might
Fiber's embrace, errors set right!

🕒✨


🪧 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.
    • Generate unit testing code for this file.
    • 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 generate unit testing code for this file.
    • @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 generate unit testing code.
    • @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.

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. (Beta)
  • @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.

- Combine classic context-based timeout with a Goroutine + channel approach
- Support custom error list without additional parameters
- Return fiber.ErrRequestTimeout for timeouts or listed errors
Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 2

🧹 Nitpick comments (3)
middleware/timeout/timeout.go (3)

11-13: Enhance documentation with examples and warnings.

While the documentation clearly describes the basic functionality, consider adding:

  • Example usage showing how to use custom errors
  • Warning about potential memory leaks with long-running handlers that exceed the timeout
  • Description of the tErrs parameter's purpose and behavior

32-39: Clarify context cancellation handling.

The comment about "other context cancellations" needs clarification. Consider handling context.Canceled differently from context.DeadlineExceeded as they represent different scenarios.

 if errors.Is(timeoutContext.Err(), context.DeadlineExceeded) {
     return fiber.ErrRequestTimeout
 }
-// For other context cancellations, we can still treat them the same
-return fiber.ErrRequestTimeout
+// Handle explicit cancellation differently
+if errors.Is(timeoutContext.Err(), context.Canceled) {
+    return fiber.ErrRequestCanceled
+}
+return fiber.ErrRequestTimeout

47-52: Optimize custom error checking.

For better performance with a large number of custom errors, consider using a map for O(1) lookup instead of iterating through the slice.

+// Convert error slice to map at middleware creation
+errorMap := make(map[error]struct{}, len(tErrs))
+for _, err := range tErrs {
+    errorMap[err] = struct{}{}
+}

-// Check against any custom errors in the list
-for _, timeoutErr := range tErrs {
-    if errors.Is(err, timeoutErr) {
-        return fiber.ErrRequestTimeout
-    }
-}
+// O(1) lookup
+if _, exists := errorMap[err]; exists {
+    return fiber.ErrRequestTimeout
+}
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 86d72bb and 88458c5.

📒 Files selected for processing (1)
  • middleware/timeout/timeout.go (1 hunks)
⏰ Context from checks skipped due to timeout of 90000ms (2)
  • GitHub Check: repeated
  • GitHub Check: Compare
🔇 Additional comments (1)
middleware/timeout/timeout.go (1)

15-21: LGTM! Clean context setup with proper cleanup.

The implementation follows Go best practices for context handling with proper cleanup via deferred cancellation.

middleware/timeout/timeout.go Outdated Show resolved Hide resolved
middleware/timeout/timeout.go Outdated Show resolved Hide resolved
…tion

- Switch to a synchronous approach to avoid data races with fasthttp context
- Enhance error handling for deadline and custom errors
- Update comments for clarity and maintainability
@ReneWerner87 ReneWerner87 changed the title feat(timeout): unify and enhance timeout middleware refactor(timeout): unify and enhance timeout middleware Jan 7, 2025
Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 0

♻️ Duplicate comments (1)
middleware/timeout/timeout.go (1)

23-39: ⚠️ Potential issue

Add panic recovery and consider asynchronous execution.

The current implementation lacks panic recovery and might not handle high concurrency efficiently.

Consider this enhanced implementation:

-// Execute the wrapped handler synchronously.
-err := h(ctx)
+// Execute handler with panic recovery
+done := make(chan error, 1)
+go func() {
+    defer func() {
+        if r := recover(); r != nil {
+            done <- fiber.NewError(fiber.StatusInternalServerError, "panic recovered")
+        }
+    }()
+    done <- h(ctx)
+}()
+
+// Wait for either timeout or handler completion
+select {
+case err := <-done:
+    if err != nil {
+        if errors.Is(err, context.DeadlineExceeded) || isCustomError(err, tErrs) {
+            return fiber.ErrRequestTimeout
+        }
+        return err
+    }
+case <-timeoutContext.Done():
+    return fiber.ErrRequestTimeout
+}
🧹 Nitpick comments (2)
middleware/timeout/timeout.go (2)

11-13: Enhance documentation with concurrency safety notes.

While the documentation clearly describes the timeout behavior, it should also document whether the fiber.Ctx is safe for concurrent access, as this is critical for middleware implementers.

Add the following to the documentation:

 // New enforces a timeout for each incoming request. If the timeout expires or
 // any of the specified errors occur, fiber.ErrRequestTimeout is returned.
+//
+// Note: This middleware assumes that fiber.Ctx is safe for concurrent access
+// between the main goroutine and timeout goroutine. Exercise caution when
+// modifying the context from multiple goroutines.

42-49: Document and optimize error checking.

The helper function works correctly but could benefit from optimization for large error lists and better documentation.

Consider these improvements:

-// isCustomError checks whether err matches any error in errList using errors.Is.
+// isCustomError checks whether err matches any error in errList using errors.Is.
+// Time complexity is O(n) where n is the length of errList. For large error lists,
+// consider using a more efficient data structure if performance is critical.
 func isCustomError(err error, errList []error) bool {
+    if len(errList) == 0 {
+        return false
+    }
     for _, e := range errList {
         if errors.Is(err, e) {
             return true
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 88458c5 and 32f1e68.

📒 Files selected for processing (1)
  • middleware/timeout/timeout.go (1 hunks)
⏰ Context from checks skipped due to timeout of 90000ms (3)
  • GitHub Check: unit (1.23.x, windows-latest)
  • GitHub Check: repeated
  • GitHub Check: Compare
🔇 Additional comments (2)
middleware/timeout/timeout.go (2)

11-49: Add comprehensive test coverage.

The timeout middleware requires thorough testing to ensure reliability across various scenarios.

Please add tests in middleware/timeout/timeout_test.go for:

  1. Successful completion within timeout
  2. Timeout scenarios
  3. Custom error handling
  4. Context cancellation
  5. Panic recovery (once implemented)

Run this script to verify test coverage:

#!/bin/bash
# Check test coverage for the timeout middleware

# Look for timeout-related test scenarios
rg "context\.WithTimeout|fiber\.ErrRequestTimeout" --type go test -A 5

# Check if panic recovery is tested
rg -i "TestTimeout.*Panic" --type go test

15-22: Consider race conditions in context management.

The current implementation might have a race condition if the timeout occurs between creating the context and attaching it to the Fiber context. Also, ensure proper cleanup of the Fiber context in error scenarios.

Consider this approach:

 timeoutContext, cancel := context.WithTimeout(ctx.Context(), timeout)
 defer cancel()
+
+// Store the original context to restore it in case of errors
+originalContext := ctx.Context()
+defer func() {
+    // Restore the original context to prevent leaks
+    ctx.SetContext(originalContext)
+}()
 
 // Attach the timeout-bound context to the current Fiber context.
 ctx.SetContext(timeoutContext)

Copy link
Member

@gaby gaby left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 LGTM

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 0

♻️ Duplicate comments (1)
middleware/timeout/timeout.go (1)

28-29: ⚠️ Potential issue

Add panic recovery for handler execution.

The handler execution should be wrapped in a panic recovery to prevent crashes.

 // Execute the wrapped handler synchronously.
+err := func() (err error) {
+    defer func() {
+        if r := recover(); r != nil {
+            err = fiber.NewError(fiber.StatusInternalServerError, "panic recovered")
+        }
+    }()
+    return h(ctx)
+}()
-err := h(ctx)
🧹 Nitpick comments (3)
middleware/timeout/timeout.go (1)

11-13: Enhance documentation for custom error handling.

While the documentation is clear about the basic timeout functionality, it could be more descriptive about the tErrs parameter and how custom errors are handled.

 // New enforces a timeout for each incoming request. If the timeout expires or
-// any of the specified errors occur, fiber.ErrRequestTimeout is returned.
+// any of the specified errors occur, fiber.ErrRequestTimeout is returned.
+// Custom errors (tErrs) allow treating specific errors as timeout conditions,
+// useful for scenarios where operations may fail with domain-specific timeout errors.
 func New(h fiber.Handler, timeout time.Duration, tErrs ...error) fiber.Handler {
middleware/timeout/timeout_test.go (2)

36-70: Enhance test coverage with additional assertions.

While the basic scenarios are covered, consider adding:

  1. Response body assertions
  2. Edge cases (e.g., timeout-1ms)
  3. Headers verification

110-124: Add test for negative timeout.

Consider adding a test case for negative timeout duration to ensure complete coverage of edge cases.

func TestTimeout_NegativeDuration(t *testing.T) {
    app := fiber.New()
    app.Get("/negative", New(func(c fiber.Ctx) error {
        time.Sleep(50 * time.Millisecond)
        return c.SendString("No timeout used")
    }, -1*time.Millisecond))
    
    req := httptest.NewRequest(fiber.MethodGet, "/negative", nil)
    resp, err := app.Test(req)
    require.NoError(t, err)
    require.Equal(t, fiber.StatusOK, resp.StatusCode)
}
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 32f1e68 and 803c5b9.

📒 Files selected for processing (2)
  • middleware/timeout/timeout.go (1 hunks)
  • middleware/timeout/timeout_test.go (1 hunks)
⏰ Context from checks skipped due to timeout of 90000ms (3)
  • GitHub Check: unit (1.23.x, windows-latest)
  • GitHub Check: repeated
  • GitHub Check: Compare
🔇 Additional comments (5)
middleware/timeout/timeout.go (2)

15-18: LGTM! Efficient handling of zero timeout case.

Good optimization to skip context creation when timeout is zero or negative.


47-64: Add unit tests for helper functions.

While the helper functions are well-implemented, they should have dedicated unit tests to verify their behavior in isolation.

Run this script to check current test coverage:

middleware/timeout/timeout_test.go (3)

15-21: LGTM! Well-structured test error declarations.

Clear separation and documentation of test error cases.


23-34: LGTM! Well-implemented test helper.

The sleepWithContext function is well-designed with proper timer cleanup and clear error handling.


73-108: LGTM! Comprehensive error handling tests.

Excellent coverage of both custom timeout errors and unmatched errors, including wrapped errors.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 0

🧹 Nitpick comments (2)
middleware/timeout/timeout_test.go (2)

75-94: Consider adding error message verification.

While the test effectively verifies custom error handling, it could be enhanced by checking the error message in the response body to ensure proper error wrapping.

 	resp, err := app.Test(req)
 	require.NoError(t, err, "app.Test(req) should not fail")
 	require.Equal(t, fiber.StatusRequestTimeout, resp.StatusCode, "Expected 408 for custom timeout error")
+	body, _ := io.ReadAll(resp.Body)
+	require.Contains(t, string(body), "wrapped: custom timeout error", "Expected wrapped error message in response")

114-129: Consider adding duration verification.

While the test verifies the zero timeout case, it could be enhanced by ensuring the full duration was actually consumed.

 	app.Get("/zero", New(func(c fiber.Ctx) error {
+		start := time.Now()
 		// Sleep 50ms, but there's no real 'deadline' since zero-timeout.
 		time.Sleep(50 * time.Millisecond)
+		duration := time.Since(start)
+		require.GreaterOrEqual(t, duration, 50*time.Millisecond, "Expected full sleep duration")
 		return c.SendString("No timeout used")
 	}, 0))
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 803c5b9 and 649488d.

📒 Files selected for processing (1)
  • middleware/timeout/timeout_test.go (1 hunks)
⏰ Context from checks skipped due to timeout of 90000ms (2)
  • GitHub Check: repeated
  • GitHub Check: Compare
🔇 Additional comments (6)
middleware/timeout/timeout_test.go (6)

15-21: LGTM! Well-structured error definitions.

The error variables are well-documented and clearly separate timeout-related errors from unrelated ones, which is crucial for testing different error scenarios.


23-34: LGTM! Clean implementation of the helper function.

The sleepWithContext function is well-implemented with:

  • Proper timer cleanup using defer
  • Clear error propagation
  • Effective use of select for context cancellation

36-54: LGTM! Well-structured success test case.

The test effectively verifies the happy path with:

  • Clear timing constraints (10ms operation vs 50ms timeout)
  • Proper error handling
  • Explicit status code verification

56-73: LGTM! Comprehensive timeout test.

The test properly verifies timeout behavior with:

  • Clear timing setup (200ms operation vs 100ms timeout)
  • Appropriate status code verification (408)

96-112: LGTM! Good error differentiation test.

The test effectively verifies that unmatched errors result in 500 status, maintaining proper error handling hierarchy.


36-129: LGTM! Comprehensive test coverage.

The test suite effectively covers all critical scenarios:

  • Success cases
  • Timeout handling
  • Custom error handling
  • Error differentiation
  • Edge cases (zero duration)

The consistent use of t.Parallel() and clear documentation makes the tests both efficient and maintainable.

Copy link

codecov bot commented Jan 7, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 84.15%. Comparing base (86d72bb) to head (376d0d9).
Report is 1 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #3275      +/-   ##
==========================================
+ Coverage   84.04%   84.15%   +0.10%     
==========================================
  Files         116      116              
  Lines       11522    11541      +19     
==========================================
+ Hits         9684     9712      +28     
+ Misses       1407     1400       -7     
+ Partials      431      429       -2     
Flag Coverage Δ
unittests 84.15% <100.00%> (+0.10%) ⬆️

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

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 0

♻️ Duplicate comments (1)
middleware/timeout/timeout.go (1)

28-35: ⚠️ Potential issue

Add panic recovery in the handler execution.

The handler execution should include panic recovery to prevent crashes.

This is a duplicate of a previous review comment. The concern about panic recovery is still valid and should be addressed.

🧹 Nitpick comments (2)
middleware/timeout/timeout.go (2)

11-13: Enhance function documentation with examples and parameter details.

While the documentation is clear, it would be more helpful with:

  • Parameter descriptions (timeout duration, custom errors)
  • Usage examples
  • Return value documentation

Add the following to the documentation:

 // New enforces a timeout for each incoming request. If the timeout expires or
 // any of the specified errors occur, fiber.ErrRequestTimeout is returned.
+//
+// Parameters:
+//   - h: The handler to be executed with timeout enforcement
+//   - timeout: Maximum duration to wait for handler completion. Use <= 0 to disable timeout
+//   - tErrs: Optional list of custom errors that should be treated as timeout errors
+//
+// Returns:
+//   fiber.Handler that enforces the specified timeout
+//
+// Example:
+//
+//	app.Get("/", timeout.New(handler, 5*time.Second))
+//
+//	// With custom timeout errors
+//	app.Get("/", timeout.New(handler, 5*time.Second, ErrDatabaseTimeout, ErrRedisTimeout))

51-58: Consider optimizing error list lookup for large error sets.

While the current implementation is clean and correct, for cases with many custom errors, consider using a more efficient lookup method.

For large error lists, you could use a map-based approach:

+// isCustomError checks whether err matches any error in errList using errors.Is.
+// For large error lists, it uses a map for efficient lookup.
 func isCustomError(err error, errList []error) bool {
+    if len(errList) > 10 { // threshold for switching to map-based lookup
+        errorMap := make(map[error]struct{}, len(errList))
+        for _, e := range errList {
+            errorMap[e] = struct{}{}
+        }
+        for e := range errorMap {
+            if errors.Is(err, e) {
+                return true
+            }
+        }
+        return false
+    }
     for _, e := range errList {
         if errors.Is(err, e) {
             return true
         }
     }
     return false
 }
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 649488d and 1077ab4.

📒 Files selected for processing (1)
  • middleware/timeout/timeout.go (1 hunks)
⏰ Context from checks skipped due to timeout of 90000ms (3)
  • GitHub Check: unit (1.23.x, windows-latest)
  • GitHub Check: Compare
  • GitHub Check: repeated
🔇 Additional comments (2)
middleware/timeout/timeout.go (2)

39-49: Add test coverage for helper functions.

The new helper functions need comprehensive test coverage to verify behavior with:

  • Various error scenarios
  • Different combinations of custom errors
  • Edge cases

This is a duplicate of a previous review comment about test coverage. The need for tests is even more important now with the addition of these helper functions.


25-26: Review concurrent context modification safety.

The SetContext call modifies the Fiber context, which might not be safe for concurrent access. Consider documenting thread-safety guarantees or potential limitations.

Let's check the Fiber documentation and codebase for context safety:

@ReneWerner87 ReneWerner87 merged commit bc37f20 into main Jan 8, 2025
14 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Status: Done
Development

Successfully merging this pull request may close these issues.

2 participants