HTTP ratelimit middleware for Go.
go get -u github.com/go-http-utils/ratelimit
API documentation can be found here: https://godoc.org/github.com/go-http-utils/ratelimit
import (
"github.com/go-http-utils/ratelimit"
)
getIDByReq := func(req *http.Request) string {
return req.RemoteAddr
}
m := http.NewServeMux()
m.HandleFunc("/", func(res http.ResponseWriter, req *http.Request) {
res.WriteHeader(http.StatusOK)
res.Write([]byte("Hello Worlkd"))
})
http.ListenAndServe(":8080", ratelimit.Handler(m, ratelimit.Options{
GetID: getIDByReq,
Duration: 1 * time.Second,
Count: 1000,
}))