Implement rate limiting

- 60 requests per minute for requests without a valid API token
- 2000 requests per minute per user for requests with a valid API token
This commit is contained in:
Howl
2016-07-06 16:33:58 +02:00
parent 0a870ee742
commit faf948b037
4 changed files with 97 additions and 2 deletions
+5 -2
View File
@@ -19,11 +19,12 @@ func Method(f func(md common.MethodData) common.CodeMessager, privilegesNeeded .
}
func initialCaretaker(c *gin.Context, f func(md common.MethodData) common.CodeMessager, privilegesNeeded ...int) {
rateLimiter()
data, err := ioutil.ReadAll(c.Request.Body)
if err != nil {
c.Error(err)
}
c.Request.Body.Close()
token := ""
switch {
@@ -50,6 +51,8 @@ func initialCaretaker(c *gin.Context, f func(md common.MethodData) common.CodeMe
}
}
perUserRequestLimiter(md.ID(), c.Request.Header.Get("X-Real-IP"))
missingPrivileges := 0
for _, privilege := range privilegesNeeded {
if int(md.User.Privileges)&privilege == 0 {
@@ -96,7 +99,7 @@ func mkjson(c *gin.Context, data interface{}) {
exported, err := json.MarshalIndent(data, "", "\t")
if err != nil {
c.Error(err)
exported = []byte(`{ "code": 500, "message": "something has gone really really really really really really wrong.", "data": null }`)
exported = []byte(`{ "code": 500, "message": "something has gone really really really really really really wrong." }`)
}
cb := c.Query("callback")
willcb := cb != "" &&