-
Notifications
You must be signed in to change notification settings - Fork 137
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Sync docs for release https://github.com/gofiber/contrib/releases/tag…
- Loading branch information
1 parent
e0c6de4
commit 1bb6eb8
Showing
7 changed files
with
437 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
59 changes: 59 additions & 0 deletions
59
contrib_versioned_docs/version-opafiber_v2.x.x/fgprof/README.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
--- | ||
id: fgprof | ||
--- | ||
|
||
# Fgprof | ||
|
||
 | ||
[](https://gofiber.io/discord) | ||
 | ||
 | ||
 | ||
|
||
[fgprof](https://github.com/felixge/fgprof) support for Fiber. | ||
|
||
**Note: Requires Go 1.19 and above** | ||
|
||
## Install | ||
|
||
This middleware supports Fiber v2. | ||
|
||
Using fgprof to profiling your Fiber app. | ||
|
||
``` | ||
go get -u github.com/gofiber/fiber/v2 | ||
go get -u github.com/gofiber/contrib/fgprof | ||
``` | ||
|
||
## Config | ||
|
||
| Property | Type | Description | Default | | ||
|----------|---------------------------|--------------------------------------------------------------------------------------------------------------------------------------------------|---------| | ||
| Next | `func(c *fiber.Ctx) bool` | A function to skip this middleware when returned `true`. | `nil` | | ||
| Prefix | `string`. | Prefix defines a URL prefix added before "/debug/fgprof". Note that it should start with (but not end with) a slash. Example: "/federated-fiber" | `""` | | ||
|
||
## Example | ||
|
||
```go | ||
package main | ||
|
||
import ( | ||
"log" | ||
|
||
"github.com/gofiber/contrib/fgprof" | ||
"github.com/gofiber/fiber/v2" | ||
) | ||
|
||
func main() { | ||
app := fiber.New() | ||
app.Use(fgprof.New()) | ||
app.Get("/", func(c *fiber.Ctx) error { | ||
return c.SendString("OK") | ||
}) | ||
log.Fatal(app.Listen(":3000")) | ||
} | ||
``` | ||
|
||
```bash | ||
go tool pprof -http=:8080 http://localhost:3000/debug/fgprof | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
121 changes: 121 additions & 0 deletions
121
contrib_versioned_docs/version-opafiber_v2.x.x/loadshed/README.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
--- | ||
id: loadshed | ||
--- | ||
|
||
# LoadShed | ||
|
||
 | ||
[](https://gofiber.io/discord) | ||
 | ||
 | ||
 | ||
|
||
The LoadShed middleware for [Fiber](https://github.com/gofiber/fiber) is designed to help manage server load by shedding requests based on certain load criteria. | ||
|
||
**Note: Requires Go 1.19 and above** | ||
|
||
## Install | ||
|
||
This middleware supports Fiber v2 | ||
|
||
``` | ||
go get -u github.com/gofiber/fiber/v2 | ||
go get -u github.com/gofiber/contrib/loadshed | ||
``` | ||
|
||
## Signatures | ||
|
||
```go | ||
loadshed.New(config ...loadshed.Config) fiber.Handler | ||
``` | ||
|
||
## Examples | ||
|
||
To use the LoadShed middleware in your Fiber application, import it and apply it to your Fiber app. Here's an example: | ||
|
||
```go | ||
package main | ||
|
||
import ( | ||
"github.com/gofiber/fiber/v2" | ||
loadshed "github.com/gofiber/contrib/loadshed" | ||
) | ||
|
||
func main() { | ||
app := fiber.New() | ||
|
||
// Configure and use LoadShed middleware | ||
app.Use(loadshed.New(loadshed.Config{ | ||
Criteria: &loadshed.CPULoadCriteria{ | ||
LowerThreshold: 0.75, // Set your own lower threshold | ||
UpperThreshold: 0.90, // Set your own upper threshold | ||
Interval: 10 * time.Second, | ||
Getter: &loadshed.DefaultCPUPercentGetter{}, | ||
}, | ||
})) | ||
|
||
app.Get("/", func(c *fiber.Ctx) error { | ||
return c.SendString("Welcome!") | ||
}) | ||
|
||
app.Listen(":3000") | ||
} | ||
``` | ||
|
||
## Config | ||
|
||
The LoadShed middleware in Fiber offers various configuration options to tailor the load shedding behavior according to the needs of your application. | ||
|
||
| Property | Type | Description | Default | | ||
| :------- | :---------------------- | :--------------------------------------------------- | :---------------------- | | ||
| Next | `func(*fiber.Ctx) bool` | Function to skip this middleware when returned true. | `nil` | | ||
| Criteria | `LoadCriteria` | Interface for defining load shedding criteria. | `&CPULoadCriteria{...}` | | ||
|
||
## LoadCriteria | ||
|
||
LoadCriteria is an interface in the LoadShed middleware that defines the criteria for determining when to shed load in the system. Different implementations of this interface can use various metrics and algorithms to decide when and how to shed incoming requests to maintain system performance. | ||
|
||
### CPULoadCriteria | ||
|
||
`CPULoadCriteria` is an implementation of the `LoadCriteria` interface, using CPU load as the metric for determining whether to shed requests. | ||
|
||
#### Properties | ||
|
||
| Property | Type | Description | | ||
| :------------- | :----------------- | :------------------------------------------------------------------------------------------------------------------------------------ | | ||
| LowerThreshold | `float64` | The lower CPU usage threshold as a fraction (0.0 to 1.0). Requests are considered for shedding when CPU usage exceeds this threshold. | | ||
| UpperThreshold | `float64` | The upper CPU usage threshold as a fraction (0.0 to 1.0). All requests are shed when CPU usage exceeds this threshold. | | ||
| Interval | `time.Duration` | The time interval over which the CPU usage is averaged for decision making. | | ||
| Getter | `CPUPercentGetter` | Interface to retrieve CPU usage percentages. | | ||
|
||
#### How It Works | ||
|
||
`CPULoadCriteria` determines the load on the system based on CPU usage and decides whether to shed incoming requests. It operates on the following principles: | ||
|
||
- **CPU Usage Measurement**: It measures the CPU usage over a specified interval. | ||
- **Thresholds**: Utilizes `LowerThreshold` and `UpperThreshold` values to decide when to start shedding requests. | ||
- **Proportional Rejection Probability**: | ||
- **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: | ||
```plaintext | ||
rejectionProbability := (cpuUsage - LowerThreshold*100) / (UpperThreshold - LowerThreshold) | ||
``` | ||
- **Above `UpperThreshold`**: All requests are rejected to prevent system overload. | ||
This mechanism ensures that the system can adaptively manage its load, maintaining stability and performance under varying traffic conditions. | ||
## Default Config | ||
This is the default configuration for `LoadCriteria` in the LoadShed middleware. | ||
```go | ||
var ConfigDefault = Config{ | ||
Next: nil, | ||
Criteria: &CPULoadCriteria{ | ||
LowerThreshold: 0.90, // 90% CPU usage as the start point for considering shedding | ||
UpperThreshold: 0.95, // 95% CPU usage as the point where all requests are shed | ||
Interval: 10 * time.Second, // CPU usage is averaged over 10 seconds | ||
Getter: &DefaultCPUPercentGetter{}, // Default method for getting CPU usage | ||
}, | ||
} | ||
``` |
Oops, something went wrong.