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

Feature/loadshed middleware v0.2 #2706

Conversation

Behzad-Khokher
Copy link
Member

Description

This update enhances the existing load shedding middleware by making it extensible, allowing the integration of various implementation types.

Fixes # 2341

Previous: #2687

Description

I have implemented a load shed middleware. This inspiration originally came from this post: (#2341 )

Loadshed middleware work by intentionally shedding or dropping some of the load (e.g., incoming requests), so the system can maintain acceptable performance and avoid potential failures.

The unique aspect of my implementation is Proportional Request Rejection Based on Probabilistic CPU Load. This means that as the CPU load increases, the probability of rejecting a request rises proportionally.

The formula used for this proportional rejection is: rejectionProbability := (cpuUsage - cfg.LowerThreshold*100) / (cfg.UpperThreshold - cfg.LowerThreshold)

A: cpuUsage - cfg.LowerThreshold * 100: Calculates how much the current CPU usage exceeds the lower threshold B: cfg.UpperThreshold - cfg.LowerThreshold: Calculates range between the lower and upper thresholds C: A/B : division scales the exceeded amount as a fraction of the range between the lower and upper thresholds.

The result is a rejectionProbability. As you can see, keeping everything constant, as the cpuUsage increases, this results in an increase in rejectionProbability. Which can be used to reject request probabilistically.

If the CPU usage is below the LowerThreshold, no requests are rejected. If the CPU usage is above the LowerThreshold, every request has a probability of being rejected depending on how closer the cpu usage is to the UpperThreshold. Once usage exceeds UpperThreshold, all requests are rejected.

This is a draft. Looking forward to feedback from the community. (This is my first PR in open source)

Fixes # 2341

Type of change

Please delete options that are not relevant.

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • This change requires a documentation update

Checklist:

  • For new functionalities I follow the inspiration of the express js framework and built them similar in usage
  • I have performed a self-review of my own code
  • I have commented my code, particularly in hard-to-understand areas
  • I have made corresponding changes to the documentation - /docs/ directory for https://docs.gofiber.io/
  • I have added tests that prove my fix is effective or that my feature works
  • New and existing unit tests pass locally with my changes
  • If new dependencies exist, I have checked that they are really necessary and agreed with the maintainers/community (we want to have as few dependencies as possible)
  • I tried to make my code as fast as possible with as few allocations as possible
  • For new code I have written benchmarks so that they can be analyzed and improved

Commit formatting:

Use emojis on commit messages so it provides an easy way of identifying the purpose or intention of a commit. Check out the emoji cheatsheet here: https://gitmoji.carloscuesta.me/

This commit introduces a prototype for the load shedding middleware for the gofiber library. The middleware checks CPU usage and determines how to handle incoming requests based on the provided thresholds.

Key components:
- Config struct for middleware customization.
- Uses gopsutil to fetch CPU percentages.
- Responses are based on CPU usage thresholds.

Note: The implementation is not complete. Feedback is needed on how to manage and execute queued requests effectively.
@ReneWerner87 ReneWerner87 linked an issue Nov 6, 2023 that may be closed by this pull request
3 tasks
@ReneWerner87
Copy link
Member

@Behzad-Khokher why a new pull request? the other one ( #2687 )for the implementation is not yet merged?

@Behzad-Khokher
Copy link
Member Author

My apologies, will fix it.

@efectn
Copy link
Member

efectn commented Nov 6, 2023

What about making this middleware a part of contrib? We plan to remove gopsutil on v3 and it would be better if we don't add new middleware to core that has gopsutil dependency

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

🚀 [Feature]: Load shedding middleware?
3 participants