20 lines
361 B
Go
20 lines
361 B
Go
package middleware
|
|
|
|
import (
|
|
"github.com/gin-gonic/gin"
|
|
"golang.org/x/time/rate"
|
|
"net/http"
|
|
)
|
|
|
|
func RateLimit(limit rate.Limit, burst int) gin.HandlerFunc {
|
|
limiter := rate.NewLimiter(limit, burst)
|
|
return func(c *gin.Context) {
|
|
if !limiter.Allow() {
|
|
c.String(http.StatusTooManyRequests, "too many requests")
|
|
c.Abort()
|
|
return
|
|
}
|
|
c.Next()
|
|
}
|
|
}
|