Skip to content

Commit

Permalink
Better rate limiting
Browse files Browse the repository at this point in the history
  • Loading branch information
manucorporat committed May 13, 2015
1 parent e8897d7 commit c2a089a
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 4 deletions.
13 changes: 10 additions & 3 deletions realtime-advanced/limit.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,22 @@
package main

import "github.com/gin-gonic/gin"
import (
"log"

"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)
if value > 400 {
value := uint64(ips.Add(ip, 1))
if value >= 400 {
if value%400 == 0 {
log.Printf("BlockedIP:%s Requests:%d\n", ip, value)
}
c.AbortWithStatus(401)
}
}
2 changes: 1 addition & 1 deletion realtime-advanced/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (

func main() {
router := gin.New()
router.Use(gin.Logger(), ratelimit)
router.Use(ratelimit, gin.Recovery(), gin.Logger())

router.LoadHTMLGlob("resources/*.templ.html")
router.Static("/static", "resources/static")
Expand Down

0 comments on commit c2a089a

Please sign in to comment.