Releases: go-chi/httprate
Releases · go-chi/httprate
v0.14.0
What's Changed
- Implement rate-limiting from HTTP handler (e.g. by request payload) by @VojtechVitek in #42
- Export http.RateLimiter type by @VojtechVitek in #43
- Introduce RespondOnLimit() vs. OnLimit() methods by @VojtechVitek in #44
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.RespondOnLimit(w, r, payload.Username) {
return
}
w.Write([]byte("login at 5 req/min\n"))
})
Full Changelog: v0.12.1...v0.14.0
v0.13.1
What's Changed
- Export http.RateLimiter type by @VojtechVitek in #43 so users can pass
*http.RateLimiter
(or save in their server struct) and use the new.OnLimit()
feature from #42.
Full Changelog: v0.13.0...v0.13.1
v0.13.0
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"))
})
v0.12.1
What's Changed
- Implement httprate.WithErrorHandler() by @VojtechVitek in #41
Full Changelog: v0.12.0...v0.12.1
v0.12.0
What's Changed
- Remove unused func argument by @VojtechVitek in #36
- Improve example, add go module by @VojtechVitek in #37
- Upgrade to actions/setup-go@v5, improve README by @VojtechVitek in #38
- Export in-memory counter for external use by @VojtechVitek in #39
- Local counter: Don't re-allocate maps in Go 1.21+ by @VojtechVitek in #40
Full Changelog: v0.11.0...v0.12.0
v0.11.0: Improve performance of in-memory rate-limit counter
- Improves throughput by 2.6x 🚀
- Reduces memory footprint and allocations by 2x 📉
$ benchstat old.txt new.txt
goos: darwin
goarch: arm64
pkg: github.com/go-chi/httprate
│ old.txt │ new.txt │
│ sec/op │ sec/op vs base │
LocalCounter-8 34.65m ± 3% 13.50m ± 2% -61.04% (p=0.000 n=10)
│ old.txt │ new.txt │
│ B/op │ B/op vs base │
LocalCounter-8 4.384Mi ± 0% 2.846Mi ± 0% -35.07% (p=0.000 n=10)
│ old.txt │ new.txt │
│ allocs/op │ allocs/op vs base │
LocalCounter-8 253.9k ± 0% 121.6k ± 0% -52.12% (p=0.000 n=10)