Skip to content

Commit

Permalink
Update docs
Browse files Browse the repository at this point in the history
  • Loading branch information
gaby authored Jan 5, 2025
1 parent a192bd1 commit 4c8a17e
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 37 deletions.
65 changes: 36 additions & 29 deletions docs/middleware/loadshedding.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,43 +4,40 @@ 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.
The **Load Shedding** middleware for [Fiber](https://github.com/gofiber/fiber) helps maintain server stability by applying request-processing timeouts. It prevents resource exhaustion by gracefully rejecting requests that exceed a specified time limit. This is particularly beneficial in high-traffic scenarios, where preventing overload is crucial to sustaining service availability and performance.

## Features

- **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.
- **Request Timeout Enforcement**: Automatically terminates any request that exceeds the configured processing time.
- **Customizable Response**: Enables you to define a specialized response for timed-out requests.
- **Exclusion Logic**: Lets you skip load-shedding for specific requests, such as health checks or other critical endpoints.
- **Enhanced Stability**: Helps avoid server crashes or sluggish performance 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.
- **High-Traffic Scenarios**: Safeguard critical resources by rejecting overly long or resource-intensive requests.
- **Health Check Protection**: Exclude monitoring endpoints (e.g., `/health`) to ensure uninterrupted external checks.
- **Dynamic Load Management**: Utilize exclusion logic to adjust load-shedding behavior for specific routes or request types.

---

## Signature

```go
func New(timeout time.Duration, loadSheddingHandler fiber.Handler, exclude func (fiber.Ctx) bool) fiber.Handler
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` |
| Property | Type | Description | Default |
|-----------------------|--------------------|----------------------------------------------------------------------------------|-----------|
| `timeout` | `time.Duration` | The maximum allowed processing time for a request. | Required |
| `loadSheddingHandler`| `fiber.Handler` | The handler invoked for requests that exceed the `timeout`. | Required |
| `exclude` | `func(fiber.Ctx) bool` | Optional function to exclude certain requests from load-shedding logic. | `nil` |

## Example Usage

Import the middleware package and integrate it into your Fiber application:
Import the middleware and configure it within your Fiber application:

```go
import (
Expand All @@ -53,19 +50,29 @@ 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.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 an exclusion function 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 {
// Exclude /health from load-shedding
return c.Path() == "/health"
},
))

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

Expand Down
14 changes: 6 additions & 8 deletions docs/whats_new.md
Original file line number Diff line number Diff line change
Expand Up @@ -811,19 +811,17 @@ The Healthcheck middleware has been enhanced to support more than two routes, wi

Refer to the [healthcheck middleware migration guide](./middleware/healthcheck.md) or the [general migration guide](#-migration-guide) to review the changes.

### Load shedding
### Load Shedding

We've added **Load Shedding Middleware**.It ensures system stability under high load by enforcing timeouts on request processing. This mechanism allows the application to shed excessive load gracefully and maintain responsiveness.
Weve introduced the **Load Shedding Middleware** to keep your system stable under heavy load. It automatically terminates requests that exceed a specified processing time, enabling the application to gracefully shed excessive load while maintaining responsiveness.

#### Functionality

- **Timeout Enforcement**: Automatically terminates requests exceeding a specified processing time.
- **Timeout Enforcement**: Automatically terminates requests that exceed the defined maximum processing time.
- **Customizable Response**: Supports a configurable load-shedding handler to define the response for timed-out requests.
- **Exclusion Logic**: Allows certain requests or routes to bypass the load-shedding mechanism based on defined rules.

- **Custom Response**: Uses a configurable load-shedding handler to define the response for shed requests.

- **Request Exclusion**: Allows certain requests to bypass load-shedding logic through an exclusion filter.

This middleware is designed to enhance server resilience and improve the user experience during periods of high traffic.
By applying timeouts and shedding excess load, this middleware helps your server remain resilient and ensures a smoother user experience during high-traffic periods.

## 📋 Migration guide

Expand Down

0 comments on commit 4c8a17e

Please sign in to comment.