https://github.com/hedzr/rate
rate limit library in golang
https://github.com/hedzr/rate
cmdr gin-middleware rate-limit rate-limiter rate-limiting
Last synced: 3 months ago
JSON representation
rate limit library in golang
- Host: GitHub
- URL: https://github.com/hedzr/rate
- Owner: hedzr
- License: mit
- Created: 2021-06-05T08:08:41.000Z (almost 4 years ago)
- Default Branch: master
- Last Pushed: 2023-06-07T09:38:09.000Z (almost 2 years ago)
- Last Synced: 2025-01-15T01:08:09.406Z (4 months ago)
- Topics: cmdr, gin-middleware, rate-limit, rate-limiter, rate-limiting
- Language: Go
- Homepage: https://hedzr.com/golang/algorithm/rate-limit-1/
- Size: 72.3 KB
- Stars: 3
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Contributing: CONTRIBUTING.md
- Funding: .github/FUNDING.yml
- License: LICENSE
- Code of conduct: CODE_OF_CONDUCT.md
- Codeowners: .github/CODEOWNERS
- Support: supports/main.go
Awesome Lists containing this project
README
# go-rate

[](https://github.com/hedzr/rate/releases)
[](https://pkg.go.dev/github.com/hedzr/rate)
[](https://godoc.org/github.com/hedzr/rate) [](https://goreportcard.com/report/github.com/hedzr/rate)
[](https://coveralls.io/github/hedzr/rate?branch=master)`go-rate` provides the rate limiters generally.
## Features
## History
- v0.5.0
- BREAK: To decrease unecessary dependants, we removed `middleware` subpackage. It has been moved into `supports/` and taged with `ignore`.
- You must copy its codes to use it.
- the codes reviewed
- no any third-party deps now, even from mine.- v0.1.x
- as is## Usages
### Simple
```go
package mainimport (
"fmt"
"github.com/hedzr/rate"
"time"
)func main() {
l := rate.New(rate.LeakyBucket, 100, time.Second)
for i := 0; i < 120; i++ {
ok := l.Take(1)
if !ok {
fmt.Printf("#%d Take() returns not ok, counter: %v\n", i, rate.CountOf(l))
time.Sleep(50 * time.Millisecond)
}
}
}
```### As a gin middleware
```go
package mainimport (
"github.com/gin-gonic/gin"
"github.com/hedzr/rate"
"github.com/hedzr/rate/middleware"
"time"
)func webserver() {
r := engine()
r.Run(":3000")
}func engine() *gin.Engine {
config := &middleware.Config{
Name: "...",
Description: "...",
Algorithm: string(rate.TokenBucket),
Interval: time.Second,
MaxRequests: 1000,
HeaderKeyName: "X-API-TOKEN",
ExceptionKeys: nil,
Routes: nil,
}
r := gin.Default()
r.Use(middleware.ForGin(config))
return r
}
```### Load limit config with cmdr Option Store
While integrated with [hedzr/cmdr](https://github.com/hedzr/cmdr), the short loading is available:
```go
import "github.com/hedzr/rate/middleware"func BuildRoutes(rg *gin.Engine) *gin.Engine {
buildRoutes(rg.Group("/prefix"), rg)
}
func buildRoutes(rg Router, root *gin.Engine) {
middleware.LoadConfigForGin("server.rate-limits", rg)
rg.Get("/echo/*action", echoHandler)
}
func echoGinHandler(c *gin.Context) {
action := c.Param("action")
if action == "" || action == "/" {
action = ""
}
_, _ = io.WriteString(c.Writer, fmt.Sprintf("action: %v\n", action))
}
```A config file (eg. `rate-limit.yml`) should be put in cmdr-standard `conf.d` directory, so it can be loaded automatically:
```yaml
app:
your-app: # <- replace it with your app name, the further KB in cmdr docs.
server:
rate-limits:
- name: by-api-key
interval: 1ms
max-requests: 30
header-key-name: X-API-KEY
exception-keys: [voxr-apps-test-api-key-fndsfjn]
```## License
MIT