Skip to content

Latest commit

 

History

History
35 lines (28 loc) · 1.17 KB

readme.md

File metadata and controls

35 lines (28 loc) · 1.17 KB

Gorillimiter

What is this

Gorilla Mux compatible rate limiter middleware with some opinions

Thanks to https://github.com/hashicorp/golang-lru and https://www.alexedwards.net/blog/how-to-rate-limit-http-requests for inspiration.

Usage

mux := http.NewServeMux()
mux.HandleFunc("/", yourHandler)
...
log.Println("Listening on :5000...")
// Attach listener here with prefs for window
http.ListenAndServe(":5000", gorillimiter.Limiter(mux, 10, time.Second))

Demo

$ cd src/demo
$ go build && ./demo
2020/03/31 16:57:53 Listening on :5000...
... # At this point, smash it with curl -i localhost:5000
2020/03/31 16:54:01 User [::1] is over rate limit, denying for now, current count [13]
2020/03/31 16:54:01 User [::1] is over rate limit, denying for now, current count [14]
2020/03/31 16:54:01 User [::1] is over rate limit, denying for now, current count [15]
2020/03/31 16:54:01 User [::1] is over rate limit, denying for now, current count [16]
2020/03/31 16:54:01 User [::1] is over rate limit, denying for now, current count [17]
2020/03/31 16:54:01 User [::1] is over rate limit, denying for now, current count [18]