v0.13.0
Pre-release
Pre-release
Full Changelog: v0.13.0...v0.13.0
Add support to rate-limit by custom key from HTTP handler (e.g. by request payload fields)
// Rate-limiter for login endpoint.
loginRateLimiter := httprate.NewRateLimiter(5, time.Minute)
r.Post("/login", func(w http.ResponseWriter, r *http.Request) {
var payload struct {
Username string `json:"username"`
Password string `json:"password"`
}
err := json.NewDecoder(r.Body).Decode(&payload)
if err != nil || payload.Username == "" || payload.Password == "" {
w.WriteHeader(400)
return
}
// Rate-limit login at 5 req/min.
if loginRateLimiter.OnLimit(w, r, payload.Username) {
return
}
w.Write([]byte("login at 5 req/min\n"))
})