Skip to content

Add timeout middleware migrator#144

Merged
ReneWerner87 merged 1 commit into
masterfrom
codex/2025-07-20-14-44-29
Jul 20, 2025
Merged

Add timeout middleware migrator#144
ReneWerner87 merged 1 commit into
masterfrom
codex/2025-07-20-14-44-29

Conversation

@ReneWerner87
Copy link
Copy Markdown
Member

@ReneWerner87 ReneWerner87 commented Jul 20, 2025

Summary

  • add migration for timeout middleware usage
  • include new migrator in migration list
  • test timeout migrator

Testing

  • go test ./...

https://chatgpt.com/codex/tasks/task_e_687cf1638b7c8326a36c44e36f7cc698

Summary by CodeRabbit

  • New Features

    • Improved migration process to automatically update timeout middleware configuration to the new format during version upgrades.
  • Tests

    • Added tests to verify correct migration of timeout middleware configuration and confirmation messages during the upgrade process.

@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented Jul 20, 2025

Walkthrough

A new migration function, MigrateTimeoutConfig, has been introduced to automate updating the usage of the timeout middleware to a new configuration style. This migration is now part of the version 3 migration sequence, and a corresponding test verifies its correctness and output.

Changes

File(s) Change Summary
cmd/internal/migrations/lists.go Added MigrateTimeoutConfig to the v3 migration sequence.
cmd/internal/migrations/v3/common.go Implemented MigrateTimeoutConfig to rewrite timeout middleware usage to new config struct format.
cmd/internal/migrations/v3/common_test.go Added a test for MigrateTimeoutConfig verifying code transformation and migration message output.

Sequence Diagram(s)

sequenceDiagram
    participant User
    participant MigrationRunner
    participant MigrateTimeoutConfig

    User->>MigrationRunner: Initiate v3 migrations
    MigrationRunner->>MigrateTimeoutConfig: Run migration on project directory
    MigrateTimeoutConfig->>MigrationRunner: Rewrite timeout.New usages, log message
    MigrationRunner->>User: Migration complete
Loading

Possibly related PRs

Suggested labels

🧹 Updates

Poem

In the warren of code, a timeout anew,
The rabbits have scurried, updating for you.
With configs in tow and migrations in stride,
They’ve burrowed through tests, with nothing to hide.
Now the middleware’s fresh—let’s hop to deploy!
🐇✨


📜 Recent review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between eba83d4 and c3e2239.

📒 Files selected for processing (3)
  • cmd/internal/migrations/lists.go (1 hunks)
  • cmd/internal/migrations/v3/common.go (1 hunks)
  • cmd/internal/migrations/v3/common_test.go (1 hunks)
🧰 Additional context used
🧠 Learnings (2)
📓 Common learnings
Learnt from: ReneWerner87
PR: gofiber/contrib#0
File: :0-0
Timestamp: 2024-07-03T11:59:00.303Z
Learning: The i18n functionality in the gofiber/contrib repository is being refactored from middleware to a global container to improve robustness and performance. The global container will be initialized once before setting up routes and will manage the i18n bundle and localizer map.
Learnt from: ReneWerner87
PR: gofiber/contrib#0
File: :0-0
Timestamp: 2024-10-16T10:04:06.328Z
Learning: The i18n functionality in the gofiber/contrib repository is being refactored from middleware to a global container to improve robustness and performance. The global container will be initialized once before setting up routes and will manage the i18n bundle and localizer map.
cmd/internal/migrations/v3/common_test.go (8)
Learnt from: sixcolors
PR: gofiber/fiber#3016
File: middleware/session/middleware_test.go:190-191
Timestamp: 2024-10-12T10:01:44.206Z
Learning: When testing session `IdleTimeout` expiration, it's acceptable to use `time.Sleep` to simulate the passage of time in tests.
Learnt from: sixcolors
PR: gofiber/fiber#3016
File: middleware/session/middleware_test.go:190-191
Timestamp: 2024-09-25T17:05:06.991Z
Learning: When testing session `IdleTimeout` expiration, it's acceptable to use `time.Sleep` to simulate the passage of time in tests.
Learnt from: sixcolors
PR: gofiber/fiber#3051
File: middleware/session/session.go:215-216
Timestamp: 2024-10-08T19:06:06.583Z
Learning: Parallel tests for `Session.Save` already exist in the `middleware/session/session_test.go` file, specifically in the `Test_Session_Save` and `Test_Session_Save_Expiration` functions.
Learnt from: sixcolors
PR: gofiber/fiber#3051
File: middleware/session/session.go:215-216
Timestamp: 2024-06-30T00:38:06.580Z
Learning: Parallel tests for `Session.Save` already exist in the `middleware/session/session_test.go` file, specifically in the `Test_Session_Save` and `Test_Session_Save_Expiration` functions.
Learnt from: ReneWerner87
PR: gofiber/fiber#3161
File: app.go:923-932
Timestamp: 2024-11-15T07:56:21.623Z
Learning: In the Fiber framework, breaking changes are acceptable when moving from version 2 to version 3, including modifications to method signatures such as in the `Test` method in `app.go`.
Learnt from: efectn
PR: gofiber/fiber#3162
File: app_test.go:893-895
Timestamp: 2024-11-29T12:37:27.581Z
Learning: In the `Test_App_ShutdownWithContext` function in `app_test.go`, the `clientDone` channel is used to synchronize the client's request completion before proceeding, eliminating the need for additional `time.Sleep` calls.
Learnt from: sixcolors
PR: gofiber/fiber#3016
File: middleware/session/middleware_test.go:400-407
Timestamp: 2024-09-25T17:09:03.756Z
Learning: In the `Test_Session_Next` function in `middleware/session/middleware_test.go`, the variable `doNext` is properly synchronized with the `muNext` mutex for both read and write access.
Learnt from: efectn
PR: gofiber/fiber#3162
File: hooks_test.go:228-228
Timestamp: 2024-12-13T08:14:22.851Z
Learning: In Go test files, prefer using the `require` methods from the `testify` package for assertions instead of manual comparisons and calls to `t.Fatal` or `t.Fatalf`.
🧬 Code Graph Analysis (3)
cmd/internal/migrations/lists.go (1)
cmd/internal/migrations/v3/common.go (1)
  • MigrateTimeoutConfig (510-521)
cmd/internal/migrations/v3/common_test.go (1)
cmd/internal/migrations/v3/common.go (1)
  • MigrateTimeoutConfig (510-521)
cmd/internal/migrations/v3/common.go (1)
cmd/internal/helpers.go (1)
  • ChangeFileContent (36-68)
🔇 Additional comments (3)
cmd/internal/migrations/v3/common.go (1)

509-521: Good implementation following established patterns.

The MigrateTimeoutConfig function correctly follows the established migration pattern in this file. The regex pattern and replacement logic appropriately transforms timeout.New(handler, duration) calls to timeout.New(handler, timeout.Config{Timeout: duration}), which aligns with the v3 API changes.

The error handling and logging are consistent with other migration functions.

cmd/internal/migrations/lists.go (1)

57-57: Proper placement in migration sequence.

The MigrateTimeoutConfig migration is correctly positioned in the migration sequence, grouped with other middleware configuration migrations between session config and request header parser migrations.

cmd/internal/migrations/v3/common_test.go (1)

782-804: Comprehensive test following established patterns.

The Test_MigrateTimeoutConfig function properly validates the timeout middleware migration by:

  1. Creating a realistic test case with timeout.New(handler, duration) syntax
  2. Verifying the transformation to the new timeout.Config{Timeout: ...} format
  3. Confirming the migration message is logged correctly

The test structure is consistent with other migration tests in the file and provides adequate coverage for the core functionality.

✨ Finishing Touches
  • 📝 Generate Docstrings
🧪 Generate unit tests
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch codex/2025-07-20-14-44-29

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 auto-generate unit tests to generate unit tests for 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 or @coderabbitai title 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.

@github-actions github-actions Bot added the ✏️ Feature New feature or request label Jul 20, 2025
Copy link
Copy Markdown
Contributor

@gemini-code-assist gemini-code-assist Bot left a comment

Choose a reason for hiding this comment

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

Summary of Changes

Hello @ReneWerner87, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request introduces a new migration tool designed to automatically update the usage of the timeout middleware in Go applications. The primary goal is to adapt existing codebases to a new configuration parameter structure for the timeout.New function, streamlining the upgrade process for users and ensuring compatibility with updated API definitions.

Highlights

  • New Migration Tool: A new MigrateTimeoutConfig function has been added to automatically update timeout.New middleware calls. This migrator transforms the old timeout.New(handler, duration) syntax to the new timeout.New(handler, timeout.Config{Timeout: duration}) format, adapting to a new configuration parameter structure.
  • Migration Integration: The newly created MigrateTimeoutConfig has been integrated into the existing v3 migration list in cmd/internal/migrations/lists.go (line 57), ensuring it runs as part of the standard migration process for GoFiber applications.
  • Test Coverage: A dedicated test case, Test_MigrateTimeoutConfig, has been added in cmd/internal/migrations/v3/common_test.go (lines 782-804) to verify the correct functionality of the new timeout middleware migrator, ensuring it accurately transforms the code as intended.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point in your pull request via creating an issue comment (i.e. comment on the pull request page) using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in issue comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist is currently in preview and may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments to provide feedback.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link
Copy Markdown
Contributor

@gemini-code-assist gemini-code-assist Bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request adds a migration for timeout middleware usage. The regular expression used for the migration is not fully robust and can fail on multi-line function calls, and it is not idempotent. I've suggested an improved regular expression and recommended adding more comprehensive test cases to cover these scenarios.


// MigrateTimeoutConfig updates timeout middleware usage to the new Config parameter
func MigrateTimeoutConfig(cmd *cobra.Command, cwd string, _, _ *semver.Version) error {
re := regexp.MustCompile(`timeout\.New\(\s*([^,\n]+)\s*,\s*([^\n)]+)\)`)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

high

The current regular expression is not robust enough. It doesn't handle multi-line function calls or ensure idempotency. The regex should allow the first argument to span multiple lines and prevent matching already migrated calls by disallowing { and } in the second argument.

Suggested change
re := regexp.MustCompile(`timeout\.New\(\s*([^,\n]+)\s*,\s*([^\n)]+)\)`)
re := regexp.MustCompile(`timeout\.New\((?s)(.*),\s*([^{}\n)]+)\)`)

Comment on lines +782 to +804
func Test_MigrateTimeoutConfig(t *testing.T) {
t.Parallel()

dir, err := os.MkdirTemp("", "mtimeout")
require.NoError(t, err)
defer func() { require.NoError(t, os.RemoveAll(dir)) }()

file := writeTempFile(t, dir, `package main
import (
"github.com/gofiber/fiber/v2"
"github.com/gofiber/fiber/v2/middleware/timeout"
"time"
)
var _ = timeout.New(func(c fiber.Ctx) error { return nil }, 2*time.Second)`)

var buf bytes.Buffer
cmd := newCmd(&buf)
require.NoError(t, v3.MigrateTimeoutConfig(cmd, dir, nil, nil))

content := readFile(t, file)
assert.Contains(t, content, `timeout.New(func(c fiber.Ctx) error { return nil }, timeout.Config{Timeout: 2*time.Second})`)
assert.Contains(t, buf.String(), "Migrating timeout middleware configs")
}
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

medium

The test for MigrateTimeoutConfig only covers a simple, single-line case. It's important to test against various code styles and edge cases, including a test case where the timeout.New call is split across multiple lines and a test case to verify idempotency.

@ReneWerner87 ReneWerner87 merged commit 5752015 into master Jul 20, 2025
13 checks passed
@ReneWerner87 ReneWerner87 deleted the codex/2025-07-20-14-44-29 branch July 20, 2025 21:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

codex ✏️ Feature New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant