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

✨ feat: Add support for RebuildTree #3074

Merged
merged 11 commits into from
Jul 18, 2024

Conversation

luk3skyw4lker
Copy link
Contributor

Description

This PR adds the RebuildTree method, which allows for dynamic route registration. It also adds a lot of warnings and comments around the method because it is currently not thread-safe and very performance-intensive.

Fixes #2769

Changes introduced

A new RebuildTree public method was added in the App struct list of methods. It allows the user to dynamically register routes and call it to make the routes available for the server to handle. Tests were added and a warning in the method comment and in the docs about the not implemented thread-safety and performance of the method.

Type of change

Please delete options that are not relevant.

  • New feature (non-breaking change which adds functionality)

Checklist

Before you submit your pull request, please make sure you meet these requirements:

  • Followed the inspiration of the Express.js framework for new functionalities, making them similar in usage.
  • Conducted a self-review of the code and provided comments for complex or critical parts.
  • Updated the documentation in the /docs/ directory for Fiber's documentation.
  • Added or updated unit tests to validate the effectiveness of the changes or new features.
  • Ensured that new and existing unit tests pass locally with the changes.
  • Verified that any new dependencies are essential and have been agreed upon by the maintainers/community.
  • Aimed for optimal performance with minimal allocations in the new code.
  • Provided benchmarks for the new code to analyze and improve upon.

@luk3skyw4lker luk3skyw4lker requested a review from a team as a code owner July 15, 2024 19:33
@luk3skyw4lker luk3skyw4lker requested review from gaby, sixcolors, ReneWerner87 and efectn and removed request for a team July 15, 2024 19:33
Copy link
Contributor

coderabbitai bot commented Jul 15, 2024

Warning

Rate limit exceeded

@luk3skyw4lker has exceeded the limit for the number of commits or files that can be reviewed per hour. Please wait 7 minutes and 33 seconds before requesting another review.

How to resolve this issue?

After the wait time has elapsed, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

We recommend that you space out your commits to avoid hitting the rate limit.

How do rate limits work?

CodeRabbit enforces hourly rate limits for each developer per organization.

Our paid plans have higher rate limits than the trial, open-source and free plans. In all cases, we re-allow further reviews after a brief timeout.

Please see our FAQ for further information.

Commits

Files that changed from the base of the PR and between 141f858 and 9d736d6.

Walkthrough

The changes introduce a new method RebuildTree in the App struct to rebuild the route tree stack dynamically during runtime. This allows for routes to be added post-startup, enhancing flexibility, particularly in development environments. A caution note is added regarding its performance and thread safety. A corresponding test Test_App_Rebuild_Tree is introduced to ensure the functionality works as intended.

Changes

File Change Summary
docs/api/app.md Documented the new RebuildTree method including its usage and caution notes.
router.go Introduced RebuildTree() method in the App struct for dynamic route tree rebuilding.
router_test.go Added Test_App_Rebuild_Tree to test the new RebuildTree method and ensure routes can be dynamically added.
docs/whats_new.md Added a summary of the new RebuildTree method, highlighting its use case and performance considerations.

Sequence Diagram(s)

sequenceDiagram
    participant User
    participant App
    participant Route
    User->>App: Define new route
    App->>Route: Register route
    App->>App: RebuildTree()
    App->>User: Route registered
    User->>App: Make request to new route
    App->>Route: Process request
    Route->>User: Return response
Loading

Assessment against linked issues

Objective Addressed Explanation
Allow rebuilding the route tree stack dynamically (#2769)
Ensure thread safety and performance considerations are documented (#2769)

Poem

Amidst the routes, a rabbit hops,
With RebuildTree, no need for stops.
Dynamically paths align,
In development, it's quite divine.
Performance minds, we must take care,
For in production, use with rare flair.
Hoppity-hop, through the code we thread,
With routes anew, we forge ahead.


Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

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>.
    • 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 generate interesting stats about this repository and render them as a table.
    • @coderabbitai show all the console.log statements in this repository.
    • @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 as 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 resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Additionally, you can add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.

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.

Copy link

codecov bot commented Jul 15, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 83.13%. Comparing base (58d07f0) to head (9d736d6).

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #3074      +/-   ##
==========================================
+ Coverage   83.10%   83.13%   +0.03%     
==========================================
  Files         115      115              
  Lines        8321     8325       +4     
==========================================
+ Hits         6915     6921       +6     
+ Misses       1076     1075       -1     
+ Partials      330      329       -1     
Flag Coverage Δ
unittests 83.13% <100.00%> (+0.03%) ⬆️

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: 3

Outside diff range, codebase verification and nitpick comments (1)
docs/api/app.md (1)

585-585: Ensure files end with a newline character.

This is a minor formatting issue but is important for maintaining POSIX compliance and ensuring compatibility with Unix tools.

+
Tools
Markdownlint

585-585: null
Files should end with a single newline character

(MD047, single-trailing-newline)

GitHub Check: markdownlint

[failure] 585-585: Files should end with a single newline character
docs/api/app.md:585:304 MD047/single-trailing-newline Files should end with a single newline character https://github.com/DavidAnson/markdownlint/blob/v0.34.0/doc/md047.md

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 58d07f0 and 4aa6310.

Files selected for processing (3)
  • docs/api/app.md (1 hunks)
  • router.go (1 hunks)
  • router_test.go (2 hunks)
Additional context used
Markdownlint
docs/api/app.md

585-585: null
Files should end with a single newline character

(MD047, single-trailing-newline)

GitHub Check: markdownlint
docs/api/app.md

[failure] 585-585: Files should end with a single newline character
docs/api/app.md:585:304 MD047/single-trailing-newline Files should end with a single newline character https://github.com/DavidAnson/markdownlint/blob/v0.34.0/doc/md047.md

docs/api/app.md Outdated Show resolved Hide resolved
router.go Show resolved Hide resolved
router_test.go Show resolved Hide resolved
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

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 4aa6310 and 436815c.

Files selected for processing (1)
  • docs/api/app.md (1 hunks)
Files skipped from review as they are similar to previous changes (1)
  • docs/api/app.md

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: 1

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 436815c and d2749c9.

Files selected for processing (1)
  • docs/api/app.md (1 hunks)
Additional context used
Markdownlint
docs/api/app.md

590-590: Column: 3
Hard tabs

(MD010, no-hard-tabs)

GitHub Check: markdownlint
docs/api/app.md

[failure] 590-590: Hard tabs
docs/api/app.md:590:3 MD010/no-hard-tabs Hard tabs [Column: 3] https://github.com/DavidAnson/markdownlint/blob/v0.34.0/doc/md010.md

docs/api/app.md Outdated Show resolved Hide resolved
@gaby
Copy link
Member

gaby commented Jul 15, 2024

@luk3skyw4lker The markdown can be fixed using this:

markdownlint-cli2 "**/*.md" "#vendor"

@luk3skyw4lker
Copy link
Contributor Author

@gaby thanks! I was crazy about it already. I'll fix it in a bit

@gaby gaby changed the title ✨ feat: add rebuild tree method ✨ feat: Add RebuildTree Method Jul 16, 2024
@gaby gaby added the v3 label Jul 16, 2024
Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>
@gaby
Copy link
Member

gaby commented Jul 16, 2024

@luk3skyw4lker Don't worry, CodeRabbit fixed it :-)

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

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between d2749c9 and b4dd5d3.

Files selected for processing (1)
  • docs/api/app.md (1 hunks)
Files skipped from review as they are similar to previous changes (1)
  • docs/api/app.md

@gaby gaby changed the title ✨ feat: Add RebuildTree Method ✨ feat: Add support for RebuildTree Jul 16, 2024
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
Member

@efectn efectn left a comment

Choose a reason for hiding this comment

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

It may worth to add mutex to addRoute. it's not much matter for performance

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: 3

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between b4dd5d3 and eda83aa.

Files selected for processing (2)
  • docs/api/app.md (1 hunks)
  • docs/whats_new.md (1 hunks)
Files skipped from review as they are similar to previous changes (1)
  • docs/api/app.md
Additional context used
LanguageTool
docs/whats_new.md

[uncategorized] ~394-~394: Consider adding a comma here to clarify the structure of the sentence.
Context: ...ee stack to be rebuilt in runtime, with it you can add a route while your applicat...

(PRP_COMMA)

Markdownlint
docs/whats_new.md

414-414: Expected: 0 or 2; Actual: 1
Trailing spaces

(MD009, no-trailing-spaces)

GitHub Check: markdownlint
docs/whats_new.md

[failure] 414-414: Trailing spaces
docs/whats_new.md:414:159 MD009/no-trailing-spaces Trailing spaces [Expected: 0 or 2; Actual: 1] https://github.com/DavidAnson/markdownlint/blob/v0.34.0/doc/md009.md

Additional comments not posted (2)
docs/whats_new.md (2)

394-416: Highlight the performance implications of RebuildTree.

The documentation correctly warns about the performance implications and thread safety of RebuildTree. It's essential that these warnings are clear and prominently displayed to prevent misuse in production environments.

Tools
LanguageTool

[uncategorized] ~394-~394: Consider adding a comma here to clarify the structure of the sentence.
Context: ...ee stack to be rebuilt in runtime, with it you can add a route while your applicat...

(PRP_COMMA)

Markdownlint

414-414: Expected: 0 or 2; Actual: 1
Trailing spaces

(MD009, no-trailing-spaces)

GitHub Check: markdownlint

[failure] 414-414: Trailing spaces
docs/whats_new.md:414:159 MD009/no-trailing-spaces Trailing spaces [Expected: 0 or 2; Actual: 1] https://github.com/DavidAnson/markdownlint/blob/v0.34.0/doc/md009.md


394-416: Verify the new RebuildTree method's documentation for accuracy and completeness.

The documentation for the new RebuildTree method provides a crucial guide for users. It's important to ensure that all details are accurate and the example is clear and correct.

Verification successful

The RebuildTree method in router.go uses a mutex lock, which suggests it might be thread-safe, contrary to what the documentation states. To clarify this discrepancy, let's examine the context in which RebuildTree is used in router_test.go and check for any comments or additional documentation that explain the thread safety aspect.


The RebuildTree method's documentation is accurate and complete.

The documentation correctly states that the RebuildTree method is not thread-safe and should be used sparingly due to its performance-intensive nature. This is supported by comments in the code and the test case provided.

  • The method is implemented with a mutex lock, but the comments clarify that it is not thread-safe in runtime due to performance considerations.
  • The test case in router_test.go verifies the functionality as described in the documentation.
Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify the accuracy of the `RebuildTree` method documentation.
# Test: Search for mentions of `RebuildTree` in the codebase to ensure consistency with the documentation.
rg --type go -A 5 $'RebuildTree'

Length of output: 439


Script:

#!/bin/bash
# Description: Gather context around the usage of `RebuildTree` in `router_test.go` and check for comments in `router.go`.
# Test: Retrieve lines around the usage of `RebuildTree` in `router_test.go` and search for comments in `router.go`.

# Extracting lines around the usage of `RebuildTree` in `router_test.go`
rg --type go -A 10 -B 10 'RebuildTree' router_test.go

# Searching for comments in `router.go` regarding `RebuildTree`
rg --type go -A 5 -B 5 'RebuildTree' router.go | rg '//'

Length of output: 1141

Tools
LanguageTool

[uncategorized] ~394-~394: Consider adding a comma here to clarify the structure of the sentence.
Context: ...ee stack to be rebuilt in runtime, with it you can add a route while your applicat...

(PRP_COMMA)

Markdownlint

414-414: Expected: 0 or 2; Actual: 1
Trailing spaces

(MD009, no-trailing-spaces)

GitHub Check: markdownlint

[failure] 414-414: Trailing spaces
docs/whats_new.md:414:159 MD009/no-trailing-spaces Trailing spaces [Expected: 0 or 2; Actual: 1] https://github.com/DavidAnson/markdownlint/blob/v0.34.0/doc/md009.md

docs/whats_new.md Outdated Show resolved Hide resolved
docs/whats_new.md Outdated Show resolved Hide resolved
docs/whats_new.md Outdated Show resolved Hide resolved
luk3skyw4lker and others added 2 commits July 16, 2024 13:38
Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>
@luk3skyw4lker
Copy link
Contributor Author

@efectn adding the mutex in the addRoute function caused a deadlock. I guess that somehere before the mutex is already locked. And for this specific case I don't think it's needed because the buildTree function never calls addRoute or register, this is called somewhere else.

@efectn
Copy link
Member

efectn commented Jul 16, 2024

Yes you need to remove mutex here https://github.com/gofiber/fiber/blob/main/router.go#L403

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

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between eda83aa and 141f858.

Files selected for processing (1)
  • docs/whats_new.md (1 hunks)
Files skipped from review as they are similar to previous changes (1)
  • docs/whats_new.md

@luk3skyw4lker
Copy link
Contributor Author

My bad @efectn, that slipped right through me, sorry!

@ReneWerner87
Copy link
Member

@gaby @efectn can i merge or is there something missing ?

@ReneWerner87 ReneWerner87 added this to the v3 milestone Jul 18, 2024
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

@ReneWerner87 ReneWerner87 merged commit 9ea7651 into gofiber:main Jul 18, 2024
17 checks passed
gaby added a commit that referenced this pull request Jul 25, 2024
* feat: add rebuild tree method

* docs: add newline at the end of app.md

* docs: add an example of dynamic defined routes

* docs: remove tabs from example code on app.md

* Update docs/api/app.md

Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>

* Update app.md

* docs: add RebuildTree to what's new documentation

* fix: markdown errors in documentation

Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>

* refactor: add mutex lock to the addRoute function

* refactor: remove mutex lock from addRoute

* refactor: fix mutex deadlock in addRoute

---------

Co-authored-by: Juan Calderon-Perez <835733+gaby@users.noreply.github.com>
Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>
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.

🚀 [Feature]: Allow rebuilding route tree stack
4 participants