Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/entrlcom/go-rate-limit
Limits and Quotas on API Requests with HTTP headers based on IETF draft.
https://github.com/entrlcom/go-rate-limit
api-rate-limit api-rate-limiter go golang ietf ietf-rfcs quota rate-limit rate-limit-redis rate-limiter redis redis-rate-limiter retry-after security token-bucket token-bucket-algorithm
Last synced: 9 days ago
JSON representation
Limits and Quotas on API Requests with HTTP headers based on IETF draft.
- Host: GitHub
- URL: https://github.com/entrlcom/go-rate-limit
- Owner: entrlcom
- License: mit
- Created: 2024-06-17T15:40:07.000Z (7 months ago)
- Default Branch: main
- Last Pushed: 2024-06-19T19:45:36.000Z (7 months ago)
- Last Synced: 2024-11-13T17:46:19.726Z (2 months ago)
- Topics: api-rate-limit, api-rate-limiter, go, golang, ietf, ietf-rfcs, quota, rate-limit, rate-limit-redis, rate-limiter, redis, redis-rate-limiter, retry-after, security, token-bucket, token-bucket-algorithm
- Language: Go
- Homepage: https://www.ietf.org/archive/id/draft-polli-ratelimit-headers-02.html
- Size: 19.5 KB
- Stars: 1
- Watchers: 0
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Rate Limit
## Table of Content
- [Examples](#examples)
- [License](#license)## Examples
```go
package mainimport (
"errors"
"net/http"
"time""entrlcom.dev/rate-limit"
"entrlcom.dev/rate-limit/algorithm/token-bucket/redis"
"entrlcom.dev/rate-limit/ietf"
"github.com/redis/rueidis"
)func main() {
opt, err := rueidis.ParseURL("redis://:[email protected]:6379?protocol=3")
if err != nil {
// TODO: Handle error.
return
}client, err := rueidis.NewClient(opt)
if err != nil {
// TODO: Handle error.
return
}quotaPolicy := ietf.NewQuotaPolicy(ietf.NewRequestQuota(250), time.Second)
rateLimit := redis_token_bucket_rate_limit.NewRedisTokenBucketRateLimit(client, quotaPolicy)handler := NewHandler(&rateLimit)
// ...
}type Handler struct {
rateLimit rate_limit.RateLimit
}func (x *Handler) Handle(w http.ResponseWriter, r *http.Request) {
if tokens, err := x.rateLimit.Take(r.Context(), r.RemoteAddr, 5); err != nil {
if errors.Is(err, rate_limit.ErrResourceExhausted) {
headers := x.rateLimit.QuotaPolicy().Headers(time.Now().UTC(), tokens, 5)
if err := headers.Write(w); err != nil {
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
return
}http.Error(w, http.StatusText(http.StatusTooManyRequests), http.StatusTooManyRequests)
return
}http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
return
}// ...
}func NewHandler(rateLimit rate_limit.RateLimit) Handler {
return Handler{rateLimit: rateLimit}
}```
## License
[MIT](https://choosealicense.com/licenses/mit/)