{"id":18310984,"url":"https://github.com/cploutarchou/go-ratelimit","last_synced_at":"2025-10-20T06:41:23.387Z","repository":{"id":152006763,"uuid":"624995105","full_name":"cploutarchou/go-ratelimit","owner":"cploutarchou","description":"Concurrent rate limiting for Go APIs with Redis","archived":false,"fork":false,"pushed_at":"2023-10-11T23:41:20.000Z","size":20,"stargazers_count":0,"open_issues_count":1,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-04-09T12:11:18.299Z","etag":null,"topics":["go","golang","golang-package","rate-limiter","rate-limiting","rest","rest-api","secure-coding","security"],"latest_commit_sha":null,"homepage":"","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/cploutarchou.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":"2023-04-07T19:36:37.000Z","updated_at":"2023-04-08T07:12:39.000Z","dependencies_parsed_at":null,"dependency_job_id":"fadf459f-0dd8-4fb6-885b-33b3bcc12444","html_url":"https://github.com/cploutarchou/go-ratelimit","commit_stats":null,"previous_names":["cploutarchou/go-ratelimit-rest-api"],"tags_count":2,"template":false,"template_full_name":null,"purl":"pkg:github/cploutarchou/go-ratelimit","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cploutarchou%2Fgo-ratelimit","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cploutarchou%2Fgo-ratelimit/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cploutarchou%2Fgo-ratelimit/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cploutarchou%2Fgo-ratelimit/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/cploutarchou","download_url":"https://codeload.github.com/cploutarchou/go-ratelimit/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cploutarchou%2Fgo-ratelimit/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":265904193,"owners_count":23846673,"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":["go","golang","golang-package","rate-limiter","rate-limiting","rest","rest-api","secure-coding","security"],"created_at":"2024-11-05T16:16:05.622Z","updated_at":"2025-10-20T06:41:18.369Z","avatar_url":"https://github.com/cploutarchou.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"### Package ratelimiter provides a rate limiter middleware for HTTP servers using Redis as the backend for storing request rate information.\n_________\n#### Usage\n##### Importing\n```go\nimport (\n\t\"github.com/redis/go-redis/v9\"\n\t\"github.com/cploutarchou/go-ratelimit\"\n)\n```\n##### Creating a Limiter\n```go\nclient := redis.NewClient(\u0026redis.Options{\n\tAddr: \"localhost:6379\",\n})\n\nlimiter := ratelimiter.NewRateLimiter(client, 10, time.Minute)\n```\n\nIn the example above, a new Redis client is created using the `go-redis/redis `library and passed to the `NewRateLimiter` function, along with a maximum request rate of 10 requests per minute.\n\n##### Limiting Requests\n```go\ntestHandler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {\n\tfmt.Fprintln(w, \"test response\")\n})\n\nlimitedHandler := limiter.Limit(testHandler)\n\nhttp.ListenAndServe(\":8080\", limitedHandler)\n```\nThe `Limit` method of the `RateLimiter` struct is used to create a new HTTP handler that limits the rate of incoming requests. In the example above, the testHandler is wrapped in the `limitedHandler`, which limits the rate of incoming requests to 10 requests per minute. The resulting handler can be passed to `http.ListenAndServe` to start serving HTTP traffic.\n\n##### Customizing Rate Limiting\nThe rate at which requests are limited can be customized by passing a different interval to the `NewRateLimiter` function. For example, to limit requests to `10` per second, use:\n\n```go\nlimiter := ratelimiter.NewRateLimiter(client, 10, time.Second)\n```\n##### Customizing Response Headers\nThe rate limiter middleware adds three response headers to each response: `X-RateLimit-Limit`, `X-RateLimit-Remaining`, and `X-RateLimit-Reset`.\n\nThe `X-RateLimit-Limit` header specifies the maximum number of requests that can be made in the given interval.\n\nThe `X-RateLimit-Remaining` header specifies the number of requests remaining in the current interval.\n\nThe `X-RateLimit-Reset` header specifies the time (in seconds) until the current interval resets.\n\nThese headers can be customized by modifying the `http.ResponseWriter` directly in the handler.\n\n##### Example\n```go\npackage main\n\nimport (\n\t\"fmt\"\n\t\"net/http\"\n\t\"time\"\n\n\t\"github.com/redis/go-redis/v9\"\n\t\"github.com/cploutarchou/go-ratelimit\"\n)\n\nfunc main() {\n\tclient := redis.NewClient(\u0026redis.Options{\n\t\tAddr: \"localhost:6379\",\n\t})\n\n\tlimiter := ratelimiter.NewRateLimiter(client, 10, time.Minute)\n\n\ttestHandler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {\n\t\tfmt.Fprintln(w, \"test response\")\n\t})\n\n\tlimitedHandler := limiter.Limit(testHandler)\n\n\thttp.ListenAndServe(\":8080\", limitedHandler)\n}\n```\n##### Testing\nThe package includes a set of unit tests that can be run using the go test command:\n\n```sh\n$ go test .\n```\nThe tests require a running instance of Redis to be available on the default port (6379). If Redis is not available, the tests will fail. A Docker container is used to start a Redis instance during testing. If Docker is not installed, the tests will also fail.","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcploutarchou%2Fgo-ratelimit","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fcploutarchou%2Fgo-ratelimit","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcploutarchou%2Fgo-ratelimit/lists"}