Skip to content

Commit

Permalink
Add rate limitting
Browse files Browse the repository at this point in the history
  • Loading branch information
manucorporat committed May 13, 2015
1 parent a721061 commit 964f6e3
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
15 changes: 15 additions & 0 deletions realtime-advanced/limit.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
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)
if value > 1000 {
c.AbortWithStatus(401)
}
}
4 changes: 3 additions & 1 deletion realtime-advanced/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ import (
)

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

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

0 comments on commit 964f6e3

Please sign in to comment.