modulepackage
1.0.2
Repository: https://github.com/nelsonken/ratelimit-go.git
Documentation: pkg.go.dev
# README
retelimit by leaky bucket
Install
go get -u github.com/nelsonken/ratelimit-go
Ability
package limit
import "time"
type Limiter interface {
// Take return can access or not immediately
Take() bool
// SpinTake spin while can't access with timeout
SpinTake(timeout time.Duration) bool
}
Naked Usage
package main
import (
"fmt"
"github.com/nelsonken/ratelimit-go"
"time"
)
func main() {
rate := 8
maxBurst := 2
duration := 10 * time.Second
// every 10 s only allow 8 times access with evenly divided time
// after long time no access, The earliest 2 requests will be allowed at the same time
limiter := ratelimit.New(rate, maxBurst, duration)
// Take
if limiter.Take() {
fmt.Println("allowed")
}else {
fmt.Println("deny")
}
if limiter.SpinTake(1*time.Second) {
fmt.Println("allowed")
}else {
fmt.Println("deny")
}
}
Usage in labstack echo middleware
package main
import (
"net/http"
"time"
"github.com/nelsonken/ratelimit-go"
"github.com/labstack/echo/v4"
)
func main() {
e := echo.New()
// 10 qps
m := ratelimit.NewRateLimitMiddleware(10, 2, time.Second)
e.Use(m)
e.GET("/", func(c echo.Context) error {
return c.String(http.StatusOK, "Hello, World!")
})
e.Logger.Fatal(e.Start(":1323"))
}
# Functions
No description provided by the author
NewRateLimitMiddleware for echo middleware.
NewSpinRateLimitMiddleware for echo middleware.
# Interfaces
No description provided by the author