gin/examples/realtime-advanced/limit.go

23 lines
341 B
Go
Raw Normal View History

2015-05-13 04:19:44 +03:00
package main
2015-05-13 04:45:17 +03:00
import (
"log"
"github.com/gin-gonic/gin"
)
2015-05-13 04:19:44 +03:00
import "github.com/manucorporat/stats"
var ips = stats.New()
func ratelimit(c *gin.Context) {
ip := c.ClientIP()
2015-05-13 04:45:17 +03:00
value := uint64(ips.Add(ip, 1))
if value >= 400 {
if value%400 == 0 {
log.Printf("BlockedIP:%s Requests:%d\n", ip, value)
}
2015-05-13 04:19:44 +03:00
c.AbortWithStatus(401)
}
}