https://github.com/shareed2k/echo_limiter
echo_limiter using redis as store for rate limit with two algorithms for choosing sliding window, gcra leaky bucket
https://github.com/shareed2k/echo_limiter
echo echo-framework gcra golang rate-limiting redis sliding-window
Last synced: 5 months ago
JSON representation
echo_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/echo_limiter
- Owner: Shareed2k
- Created: 2020-04-03T17:09:55.000Z (almost 6 years ago)
- Default Branch: master
- Last Pushed: 2023-08-31T08:12:28.000Z (over 2 years ago)
- Last Synced: 2025-04-12T08:18:41.057Z (10 months ago)
- Topics: echo, echo-framework, gcra, golang, rate-limiting, redis, sliding-window
- Language: Go
- Homepage:
- Size: 17.6 KB
- Stars: 7
- Watchers: 1
- Forks: 3
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
## echo_limiter is middleware for echo framework
echo_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 github.com/shareed2k/echo_limiter
```
### Example
```go
package main
import (
"log"
"net/http"
"time"
"github.com/go-redis/redis/v7"
"github.com/labstack/echo/v4"
limiter "github.com/shareed2k/echo_limiter"
)
func main() {
e := echo.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,
}
e.Use(limiter.NewWithConfig(cfg))
e.GET("/", func(c echo.Context) error {
return c.String(http.StatusOK, "Hello, World!")
})
e.Logger.Fatal(e.Start(":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
...
```