Description
Feature Description
I can wrap this https://github.com/asecurityteam/loadshed/blob/master/wrappers/middleware/middleware.go with Adaptor middleware of Fiber, I am aware of that, but I cannot do ctx.Next() or can't use any private functions of context when wrapping with Adaptor.
So, it would be nice to have load-shedding middleware. Put it at the top of the router order, and when the CPU is higher than %90, return 503 to any requests incoming.
And even better? Put all requests in a queue that came after CPU is higher than %90, and execute them in order.
I could do that if I wasn't too dumb for pull requests.
Additional Context (optional)
why is your code going to %100 CPU even
well, golang is widely used in scraping. and scraping tools are generally cpu hungry if you arent scraping a static-loaded website.
niche request, but yeah. i would be so happy if anyone do this.
Code Snippet (optional)
package main
import (
"github.com/gofiber/fiber/v2"
"github.com/asecurityteam/loadshed"
loadshedmiddleware "github.com/asecurityteam/loadshed/wrappers/middleware"
"github.com/gofiber/adaptor/v2"
"log"
)
const (
lowerThreshold = 0.90
upperThreshold = 0.95
pollingInterval = time.Second
windowSize = 10
)
// current setup of my mine
func main() {
app := fiber.New()
// An example to describe the feature
app.Post("/get", adaptor.HTTPMiddleware(loadshedmiddleware.New(
loadshed.New(loadshed.CPU(lowerThreshold, upperThreshold, pollingInterval, windowSize)),
loadshedmiddleware.Callback(
http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusServiceUnavailable)
jsonResponse := struct {
Message string `json:"message"`
}{
Message: "Server is overloaded, please try again later.",
}
indented, err := json.MarshalIndent(jsonResponse, "", " ")
if err != nil {
w.Write([]byte("cpu usage too high"))
}
w.Write(indented)
}),
),
)))
app.Listen("0.0.0.0:3131")
}
Checklist:
- I agree to follow Fiber's Code of Conduct.
- I have checked for existing issues that describe my suggestion prior to opening this one.
- I understand that improperly formatted feature requests may be closed without explanation.