feat(): add possibility to define custom OnShed handler;#1314
Conversation
|
Important Review skippedReview was skipped due to path filters ⛔ Files ignored due to path filters (1)
CodeRabbit blocks several paths by default. You can override this behavior by explicitly including those paths in the path filters. For example, including You can disable this status message by setting the WalkthroughA new optional OnShed callback was added to the load shedding middleware configuration, enabling custom handling when requests are declined due to high load. The middleware now invokes this callback if set, instead of returning a default 503 error. Comprehensive tests were introduced to verify various OnShed behaviors and responses. The README was updated with usage examples and documentation for the new callback. Changes
Poem
✨ Finishing Touches🧪 Generate Unit Tests
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. 🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
SupportNeed 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)
Other keywords and placeholders
CodeRabbit Configuration File (
|
There was a problem hiding this comment.
Summary of Changes
Hello @nsakharenko, 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 significant enhancement to the load shedding middleware by providing a mechanism for users to define a custom handler for requests that are declined due to high load. This allows for greater control over the response, enabling custom logging, metrics, and tailored error messages or status codes, moving beyond the default 503 Service Unavailable response.
Highlights
- Configuration Extension: I've extended the
Configstruct inloadshed/loadshed.goby adding a newOnShedfield. This field is a functionfunc(c *fiber.Ctx) error, allowing users to define a custom handler that will be executed when a request is declined due to load shedding. - Custom Handler Integration: The core load shedding middleware in
loadshed/loadshed.gohas been updated. Now, if a request is determined to be shed (cfg.Criteria.ShouldShed(metric)returns true) and a customOnShedhandler is provided in the configuration, that custom handler will be invoked instead of the defaultfiber.NewError(fiber.StatusServiceUnavailable). - Comprehensive Test Coverage: I've added a suite of new test cases in
loadshed/loadshed_test.goto thoroughly validate the behavior of the newOnShedhandler. These tests cover various scenarios, including custom status codes, custom responses, handlingnilreturns, custom errors, and JSON responses, ensuring the flexibility and robustness of the new feature.
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
-
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. ↩
There was a problem hiding this comment.
Code Review
This pull request introduces a custom OnShed handler in the load shedding middleware. The implementation is clean, and the tests are thorough. The suggestions focus on clarifying the behavior of the OnShed handler in specific edge cases and ensuring test comments are accurate.
ReneWerner87
left a comment
There was a problem hiding this comment.
thx looks good
@nsakharenko can you add more documentation for this new feature
|
@nsakharenko feature is looking good, just the documentation is missing |
There was a problem hiding this comment.
Actionable comments posted: 1
🔭 Outside diff range comments (1)
loadshed/README.md (1)
70-73: Duplicate missing import in the second exampleSame issue as above – add
timeto the import list.import ( "github.com/gofiber/fiber/v2" loadshed "github.com/gofiber/contrib/loadshed" + "time" )
🧹 Nitpick comments (2)
loadshed/README.md (2)
148-154: Fix list indentation (markdownlint MD007)The bullet list under “Proportional Rejection Probability” is indented with four spaces instead of two, tripping common linters and breaking GitHub’s renderer in some themes.
- - **Below `LowerThreshold`**: No requests are rejected, as the system is considered under acceptable load. - - **Between `LowerThreshold` and `UpperThreshold`**: The probability of rejecting a request increases as the CPU - usage approaches the `UpperThreshold`. This is calculated using the formula: + - **Below `LowerThreshold`**: No requests are rejected, as the system is considered under acceptable load. + - **Between `LowerThreshold` and `UpperThreshold`**: The probability of rejecting a request increases as the CPU + usage approaches the `UpperThreshold`. This is calculated using the formula:
137-137: Hyphenate “decision-making”Small grammar nit:
-... CPU usage is averaged for decision making. +... CPU usage is averaged for decision-making.
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (2)
loadshed/README.md(3 hunks)loadshed/loadshed.go(2 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
- loadshed/loadshed.go
🧰 Additional context used
🪛 LanguageTool
loadshed/README.md
[uncategorized] ~137-~137: The noun “decision-making” (= the process of deciding something) is spelled with a hyphen.
Context: ...ver which the CPU usage is averaged for decision making. ...
(DECISION_MAKING)
🪛 markdownlint-cli2 (0.17.2)
loadshed/README.md
148-148: Unordered list indentation
Expected: 2; Actual: 4
(MD007, ul-indent)
149-149: Unordered list indentation
Expected: 2; Actual: 4
(MD007, ul-indent)
154-154: Unordered list indentation
Expected: 2; Actual: 4
(MD007, ul-indent)
There was a problem hiding this comment.
Actionable comments posted: 0
♻️ Duplicate comments (2)
loadshed/README.md (2)
39-42:timeimport still missing – example will not compileThis has been pointed out previously but is still unresolved.
Add the standard-librarytimepackage to the import block so that10 * time.Secondtype-checks.import ( "github.com/gofiber/fiber/v2" loadshed "github.com/gofiber/contrib/loadshed" + "time" )
70-73: Second snippet repeats the missingtimeimportSame issue as above in the “custom rejection handler” example – copy-pasting will fail to build without:
import ( "github.com/gofiber/fiber/v2" loadshed "github.com/gofiber/contrib/loadshed" + "time" )
🧹 Nitpick comments (1)
loadshed/README.md (1)
136-136: Hyphenate “decision-making” for correctness-... averaged for decision making. +... averaged for decision-making.
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (1)
loadshed/README.md(5 hunks)
🧰 Additional context used
🪛 LanguageTool
loadshed/README.md
[uncategorized] ~136-~136: The noun “decision-making” (= the process of deciding something) is spelled with a hyphen.
Context: ...ver which the CPU usage is averaged for decision making. ...
(DECISION_MAKING)
|
@ReneWerner87 documentation updated, let me know if there is anything else that i can improve. |
Purpose of this PR to add possibility to define a custom
OnShedhandler.This will allow to control (add metrics, log messages etc.), and customize response code/message for a declined requests.
Summary by CodeRabbit