gin/examples/realtime-advanced/limit.go

22 lines
336 B
Go
Raw Normal View History

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