Skip to content

Commit

Permalink
docs: add loadshedding middleware
Browse files Browse the repository at this point in the history
  • Loading branch information
Erfan Momeni authored and Erfan Momeni committed Jan 3, 2025
1 parent d0607b5 commit f4682bd
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 0 deletions.
1 change: 1 addition & 0 deletions .github/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -688,6 +688,7 @@ Here is a list of middleware that are included within the Fiber framework.
| [idempotency](https://github.com/gofiber/fiber/tree/main/middleware/idempotency) | Allows for fault-tolerant APIs where duplicate requests do not erroneously cause the same action performed multiple times on the server-side. |
| [keyauth](https://github.com/gofiber/fiber/tree/main/middleware/keyauth) | Adds support for key based authentication. |
| [limiter](https://github.com/gofiber/fiber/tree/main/middleware/limiter) | Adds Rate-limiting support to Fiber. Use to limit repeated requests to public APIs and/or endpoints such as password reset. |
| [loadshedding](https://github.com/gofiber/fiber/tree/main/middleware/loadshedding) | Gracefully manages server load by enforcing request timeouts and handling resource-intensive requests during high-traffic periods. |
| [logger](https://github.com/gofiber/fiber/tree/main/middleware/logger) | HTTP request/response logger. |
| [pprof](https://github.com/gofiber/fiber/tree/main/middleware/pprof) | Serves runtime profiling data in pprof format. |
| [proxy](https://github.com/gofiber/fiber/tree/main/middleware/proxy) | Allows you to proxy requests to multiple servers. |
Expand Down
73 changes: 73 additions & 0 deletions docs/middleware/loadshedding.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
---
id: loadshedding
---

# Load Shedding

Load Shedding middleware for [Fiber](https://github.com/gofiber/fiber) is designed to enhance server stability by
enforcing timeouts on request processing. It helps manage server load effectively by gracefully handling requests that
exceed a specified timeout duration. This is especially useful in high-traffic scenarios, where preventing server
overload is critical to maintaining service availability and performance.

### Features

Check failure on line 12 in docs/middleware/loadshedding.md

View workflow job for this annotation

GitHub Actions / markdownlint

Heading levels should only increment by one level at a time

docs/middleware/loadshedding.md:12 MD001/heading-increment Heading levels should only increment by one level at a time [Expected: h2; Actual: h3] https://github.com/DavidAnson/markdownlint/blob/v0.37.2/doc/md001.md

- **Request Timeout Enforcement**: Ensures that no request exceeds the specified processing time.
- **Customizable Response**: Allows you to define a specific response for timed-out requests.
- **Exclusion Criteria**: Provides flexibility to exclude specific requests from load-shedding logic.
- **Improved Stability**: Helps prevent server crashes under heavy load by shedding excess requests.

### Use Cases

- **High-Traffic Scenarios**: Protect critical resources by shedding long-running or resource-intensive requests.
- **Health Check Protection**: Exclude endpoints like `/health` to ensure critical monitoring remains unaffected.
- **Dynamic Load Management**: Use exclusion logic to prioritize or deprioritize requests dynamically.

---

## Signature

```go
func New(timeout time.Duration, loadSheddingHandler fiber.Handler, exclude func (fiber.Ctx) bool) fiber.Handler
```

## Config

| Property | Type | Description | Default |
|:----------------------|:------------------------------------|:-----------------------------------------------------------------------------------------------|:---------|
| `timeout` | `time.Duration` | Maximum allowed processing time for a request. | Required |
| `loadSheddingHandler` | `fiber.Handler` | Handler invoked for requests that exceed the timeout. | Required |
| `exclude` | `func(fiber.Ctx) bool` | Optional function to exclude specific requests from load-shedding logic. | `nil` |

## Example Usage

Check failure on line 41 in docs/middleware/loadshedding.md

View workflow job for this annotation

GitHub Actions / markdownlint

Headings should be surrounded by blank lines

docs/middleware/loadshedding.md:41 MD022/blanks-around-headings Headings should be surrounded by blank lines [Expected: 1; Actual: 0; Below] [Context: "## Example Usage"] https://github.com/DavidAnson/markdownlint/blob/v0.37.2/doc/md022.md
Import the middleware package and integrate it into your Fiber application:

```go
import (
"time"
"github.com/gofiber/fiber/v3"
"github.com/gofiber/fiber/v3/middleware/loadshedding"
)

func main() {
app := fiber.New()

// Basic usage with a 5-second timeout
app.Use(loadshedding.New(5*time.Second, func(c fiber.Ctx) error {
return c.Status(fiber.StatusServiceUnavailable).SendString("Service unavailable due to high load")
}, nil))

// Advanced usage with exclusion logic for specific endpoints
app.Use(loadshedding.New(3*time.Second, func(c fiber.Ctx) error {
return c.Status(fiber.StatusServiceUnavailable).SendString("Request timed out")
}, func(c fiber.Ctx) bool {
return c.Path() == "/health"
}))

app.Get("/", func(c fiber.Ctx) error {
time.Sleep(4 * time.Second) // Simulating a long-running request
return c.SendString("Hello, world!")
})

app.Listen(":3000")
}
```

Check failure on line 73 in docs/middleware/loadshedding.md

View workflow job for this annotation

GitHub Actions / markdownlint

Files should end with a single newline character

docs/middleware/loadshedding.md:73:3 MD047/single-trailing-newline Files should end with a single newline character https://github.com/DavidAnson/markdownlint/blob/v0.37.2/doc/md047.md

0 comments on commit f4682bd

Please sign in to comment.