Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/shareed2k/fiber_limiter
fiber_limiter using redis as store for rate limit with two algorithms for choosing sliding window, gcra leaky bucket
https://github.com/shareed2k/fiber_limiter
fiber fiber-framework gcra go-redis golang limiter middleware redis sliding-windows
Last synced: about 1 month ago
JSON representation
fiber_limiter using redis as store for rate limit with two algorithms for choosing sliding window, gcra leaky bucket
- Host: GitHub
- URL: https://github.com/shareed2k/fiber_limiter
- Owner: Shareed2k
- Created: 2020-04-03T13:12:21.000Z (over 4 years ago)
- Default Branch: master
- Last Pushed: 2020-05-31T10:45:05.000Z (over 4 years ago)
- Last Synced: 2024-08-04T01:06:25.783Z (4 months ago)
- Topics: fiber, fiber-framework, gcra, go-redis, golang, limiter, middleware, redis, sliding-windows
- Language: Go
- Homepage:
- Size: 24.4 KB
- Stars: 14
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
- awesome-fiber - shareed2k/fiber_limiter - Limiter using redis as store for rate limit with two algorithms for choosing sliding window, gcra leaky bucket. (⚙️ Middlewares / 🌱 Third Party)
README
## fiber_limiter is middleware for fiber framework
fiber_limiter using [redis](https://github.com/go-redis/redis) as store for rate limit with two algorithms for choosing sliding window, gcra [leaky bucket](https://en.wikipedia.org/wiki/Leaky_bucket)
### Install
```
go get -u github.com/gofiber/fiber
go get -u github.com/shareed2k/fiber_limiter
```
### Example
```go
package mainimport (
"log"
"time""github.com/go-redis/redis/v7"
"github.com/gofiber/fiber"
limiter "github.com/shareed2k/fiber_limiter"
)func main() {
app := fiber.New()option, err := redis.ParseURL("redis://127.0.0.1:6379/0")
if err != nil {
log.Fatal(err)
}
client := redis.NewClient(option)
_ = client.FlushDB().Err()// 3 requests per 10 seconds max
cfg := limiter.Config{
Rediser: client,
Max: 3,
Burst: 3,
Period: 10 * time.Second,
Algorithm: limiter.GCRAAlgorithm,
}app.Use(limiter.New(cfg))
app.Get("/", func(c *fiber.Ctx) {
c.Send("Welcome!")
})app.Listen(3000)
}```
### Test
```curl
curl http://localhost:3000
...
< HTTP/1.1 200 OK
< Date: Fri, 03 Apr 2020 13:02:02 GMT
< Content-Type: text/plain; charset=utf-8
< Content-Length: 8
< X-Ratelimit-Limit: 3
< X-Ratelimit-Remaining: 2
< X-Ratelimit-Reset: 1585918925
...curl http://localhost:3000
curl http://localhost:3000
curl http://localhost:3000...
< HTTP/1.1 429 Too Many Requests
< Date: Fri, 03 Apr 2020 13:02:29 GMT
< Content-Type: text/plain; charset=utf-8
< Content-Length: 42
< Retry-After: 1585918951
...
```