gin/examples/realtime-advanced/limit.go

16 lines
238 B
Go
Raw Normal View History

2015-05-13 04:19:44 +03:00
package main
import "github.com/gin-gonic/gin"
import "github.com/manucorporat/stats"
var ips = stats.New()
func ratelimit(c *gin.Context) {
ip := c.ClientIP()
value := ips.Add(ip, 1)
2015-05-13 04:21:10 +03:00
if value > 400 {
2015-05-13 04:19:44 +03:00
c.AbortWithStatus(401)
}
}