gin/examples/realtime-advanced/limit.go

22 lines
336 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 17:44:44 +03:00
"github.com/manucorporat/stats"
2015-05-13 04:45:17 +03:00
)
2015-05-13 04:19:44 +03:00
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))
2015-05-13 17:44:44 +03:00
if value >= 1000 {
if value%1000 == 0 {
2015-05-13 04:45:17 +03:00
log.Printf("BlockedIP:%s Requests:%d\n", ip, value)
}
2015-05-13 04:19:44 +03:00
c.AbortWithStatus(401)
}
}