{"id":20102564,"url":"https://github.com/entrlcom/go-rate-limit","last_synced_at":"2025-03-02T17:21:02.764Z","repository":{"id":245179413,"uuid":"816365060","full_name":"entrlcom/go-rate-limit","owner":"entrlcom","description":"Limits and Quotas on API Requests with HTTP headers based on IETF draft.","archived":false,"fork":false,"pushed_at":"2024-06-19T19:45:36.000Z","size":20,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":0,"default_branch":"main","last_synced_at":"2025-01-13T04:29:30.612Z","etag":null,"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"],"latest_commit_sha":null,"homepage":"https://www.ietf.org/archive/id/draft-polli-ratelimit-headers-02.html","language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/entrlcom.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2024-06-17T15:40:07.000Z","updated_at":"2024-06-19T19:56:12.000Z","dependencies_parsed_at":"2024-06-20T09:55:50.582Z","dependency_job_id":"3ab904de-bbaf-4be4-be00-41aeeebea432","html_url":"https://github.com/entrlcom/go-rate-limit","commit_stats":null,"previous_names":["entrlcom/go-rate-limit"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/entrlcom%2Fgo-rate-limit","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/entrlcom%2Fgo-rate-limit/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/entrlcom%2Fgo-rate-limit/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/entrlcom%2Fgo-rate-limit/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/entrlcom","download_url":"https://codeload.github.com/entrlcom/go-rate-limit/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":241542130,"owners_count":19979258,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["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"],"created_at":"2024-11-13T17:31:46.655Z","updated_at":"2025-03-02T17:21:02.629Z","avatar_url":"https://github.com/entrlcom.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Rate Limit\n\n## Table of Content\n\n- [Examples](#examples)\n- [License](#license)\n\n## Examples\n\n```go\npackage main\n\nimport (\n\t\"errors\"\n\t\"net/http\"\n\t\"time\"\n\n\t\"entrlcom.dev/rate-limit\"\n\t\"entrlcom.dev/rate-limit/algorithm/token-bucket/redis\"\n\t\"entrlcom.dev/rate-limit/ietf\"\n\t\"github.com/redis/rueidis\"\n)\n\nfunc main() {\n\topt, err := rueidis.ParseURL(\"redis://:password@127.0.0.1:6379?protocol=3\")\n\tif err != nil {\n\t\t// TODO: Handle error.\n\t\treturn\n\t}\n\n\tclient, err := rueidis.NewClient(opt)\n\tif err != nil {\n\t\t// TODO: Handle error.\n\t\treturn\n\t}\n\n\tquotaPolicy := ietf.NewQuotaPolicy(ietf.NewRequestQuota(250), time.Second)\n\trateLimit := redis_token_bucket_rate_limit.NewRedisTokenBucketRateLimit(client, quotaPolicy)\n\n\thandler := NewHandler(\u0026rateLimit)\n\n\t// ...\n}\n\ntype Handler struct {\n\trateLimit rate_limit.RateLimit\n}\n\nfunc (x *Handler) Handle(w http.ResponseWriter, r *http.Request) {\n\tif tokens, err := x.rateLimit.Take(r.Context(), r.RemoteAddr, 5); err != nil {\n\t\tif errors.Is(err, rate_limit.ErrResourceExhausted) {\n\t\t\theaders := x.rateLimit.QuotaPolicy().Headers(time.Now().UTC(), tokens, 5)\n\t\t\tif err := headers.Write(w); err != nil {\n\t\t\t\thttp.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)\n\t\t\t\treturn\n\t\t\t}\n\n\t\t\thttp.Error(w, http.StatusText(http.StatusTooManyRequests), http.StatusTooManyRequests)\n\t\t\treturn\n\t\t}\n\n\t\thttp.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)\n\t\treturn\n\t}\n\n\t// ...\n}\n\nfunc NewHandler(rateLimit rate_limit.RateLimit) Handler {\n\treturn Handler{rateLimit: rateLimit}\n}\n\n```\n\n## License\n\n[MIT](https://choosealicense.com/licenses/mit/)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fentrlcom%2Fgo-rate-limit","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fentrlcom%2Fgo-rate-limit","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fentrlcom%2Fgo-rate-limit/lists"}