// Limit creates a new rate limiter middleware handler.
Limit creates a new rate limiter middleware handler.
// Limit creates a new rate limiter middleware handler .
func Limit(rps int, burst int, ttl time.Duration) gin.HandlerFunc {
	l := newRateLimiter(rps, burst, ttl)
	go l.cleanupVisitors()
	return func(c *gin.Context) {
		ip, _, err := net.SplitHostPort(c.Request.RemoteAddr)
		if err != nil {
			logger.Error(err)
			c.AbortWithStatus(http.StatusInternalServerError)
			return
		}
		if !l.getVisitor(ip).Allow() {
			c.AbortWithStatus(http.StatusTooManyRequests)
			return
		}
		c.Next()
	}
}
func Limit ( rps int , burst int , ttl time . Duration ) gin . HandlerFunc { l := newRateLimiter ( rps , burst , ttl ) go l . cleanupVisitors ( ) return func ( c * gin . Context ) { ip , _ , err := net . SplitHostPort ( c . Request . RemoteAddr ) if err != nil { logger . Error ( err ) c . AbortWithStatus ( http . StatusInternalServerError ) return } if ! l . getVisitor ( ip ) . Allow ( ) { c . AbortWithStatus ( http . StatusTooManyRequests ) return } c . Next ( ) } }
Limit creates a new rate limiter middleware handler.
Limit creates a new rate limiter middleware handler .
// run a background worker to clean up old entries