{"id":25131939,"url":"https://github.com/robotomize/go-ratelimiter","last_synced_at":"2026-04-28T20:07:36.829Z","repository":{"id":143010439,"uuid":"476748773","full_name":"robotomize/go-ratelimiter","owner":"robotomize","description":"A super easy rate limiting package for Go","archived":false,"fork":false,"pushed_at":"2024-12-19T13:50:15.000Z","size":56,"stargazers_count":1,"open_issues_count":1,"forks_count":0,"subscribers_count":3,"default_branch":"main","last_synced_at":"2025-02-08T14:15:58.928Z","etag":null,"topics":["go","golang","rate-limiter","rate-limiting","redis"],"latest_commit_sha":null,"homepage":"","language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/robotomize.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":"2022-04-01T14:16:08.000Z","updated_at":"2024-06-25T09:55:45.000Z","dependencies_parsed_at":null,"dependency_job_id":"e65cbc0c-a53d-482f-b81f-ec11ddfc01ee","html_url":"https://github.com/robotomize/go-ratelimiter","commit_stats":null,"previous_names":[],"tags_count":2,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/robotomize%2Fgo-ratelimiter","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/robotomize%2Fgo-ratelimiter/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/robotomize%2Fgo-ratelimiter/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/robotomize%2Fgo-ratelimiter/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/robotomize","download_url":"https://codeload.github.com/robotomize/go-ratelimiter/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":246911471,"owners_count":20853657,"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","rate-limiter","rate-limiting","redis"],"created_at":"2025-02-08T14:16:01.732Z","updated_at":"2026-04-28T20:07:36.792Z","avatar_url":"https://github.com/robotomize.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"# go-ratelimiter\n\n[![Go Report](https://goreportcard.com/badge/github.com/robotomize/go-ratelimiter)](https://goreportcard.com/report/github.com/robotomize/gokuu)\n[![codebeat badge](https://codebeat.co/badges/a4a12b24-98e6-4627-b01c-8b124561f2e1)](https://codebeat.co/projects/github-com-robotomize-go-ratelimiter-main)\n[![codecov](https://codecov.io/gh/robotomize/go-ratelimiter/branch/main/graph/badge.svg)](https://codecov.io/gh/robotomize/go-ratelimiter)\n[![Build status](https://github.com/robotomize/go-ratelimiter/actions/workflows/go.yml/badge.svg)](https://github.com/robotomize/go-ratelimiter/actions)\n[![GitHub license](https://img.shields.io/github/license/robotomize/go-ratelimiter.svg)](https://github.com/robotomize/go-ratelimiter/blob/master/LICENSE)\n\nA super easy rate limiting package for Go. Package provide Store interface, for which you can use your own\nimplementations\n\n# Install\n\n```shell\ngo get github.com/robotomize/go-ratelimiter\n```\n\n# Usage\n\nExample of using redis datastore\n\n```go\n\npackage main\n\nimport (\n\t\"context\"\n\t\"fmt\"\n\t\"log\"\n\t\"time\"\n\n\t\"github.com/robotomize/go-ratelimiter\"\n)\n\nfunc main() {\n\t// set a limit of 10 request per 1 seconds per api key\n\tredisStore, err := ratelimiter.DefaultRedisStore(context.Background(), \":6379\", 1*time.Second, 10)\n\tif err != nil {\n\t\tlog.Fatal(err)\n\t}\n\n\t// Retrieve data by the api key in the datastore\n\tlimit, remaining, resetTime, ok, err := redisStore.Take(context.Background(), \"apikey-1\")\n\tif err != nil {\n\t\tlog.Fatal(err)\n\t}\n\n\tif !ok {\n\t\tfmt.Println(\"limit exceeded\")\n\t}\n\n\t// Print the constraints from the datastore\n\tfmt.Printf(\n\t\t\"resource: maximum %d, remaining %d, reset time %s\",\n\t\tlimit, remaining, time.Unix(0, int64(resetTime)).UTC().Format(time.RFC1123),\n\t)\n}\n\n```\n\nTo limit access at the http level, you can use middleware, which can block the request by providing the http code 429\nToo Many Requests\n\nExample of using http middleware with redis datastore\n\n```go\npackage main\n\nimport (\n\t\"context\"\n\t\"errors\"\n\t\"fmt\"\n\t\"log\"\n\t\"net/http\"\n\t\"os\"\n\t\"time\"\n\n\t\"github.com/robotomize/go-ratelimiter\"\n)\n\nfunc main() {\n\t// set a limit of 5 request per 1 seconds per api key\n\tredisStore, err := ratelimiter.DefaultRedisStore(context.Background(), \":6379\", 1*time.Second, 5)\n\tif err != nil {\n\t\tlog.Fatal(err)\n\t}\n\n\tmx := http.NewServeMux()\n\t// Let's create a test handler\n\thealthHandler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {\n\t\tw.Header().Set(\"Content-Type\", \"application/json\")\n\t\tw.WriteHeader(http.StatusOK)\n\t\tw.Write([]byte(`{\"status\": \"ok\"}`))\n\t})\n    \n\tmx.Handle(\n\t\t\"/health\", ratelimiter.LimiterMiddleware(\n\t\t\tredisStore, func(r *http.Request) (string, error) {\n\t\t\t\t// Example key func\n\t\t\t\tctx := r.Context()\n\t\t\t\t// Get key value out of context\n\t\t\t\tctxValue := ctx.Value(\"apikey\")\n\t\t\t\tif key, ok := ctxValue.(string); ok {\n\t\t\t\t\treturn key, nil\n\t\t\t\t}\n\n\t\t\t\treturn \"\", errors.New(\"get api key from ctx\")\n\t\t\t}, ratelimiter.WithSkipper(func() bool {\n\t\t\t\t// set a skipper, skip ratelimiter if DEBUG == 1\n\t\t\t\treturn os.Getenv(\"DEBUG\") == \"1\" \n\t\t\t}, ),\n\t\t)(healthHandler),\n\t)\n\n\t// start listener\n\tif err = http.ListenAndServe(\":8888\", mx); err != nil {\n\t\tlog.Fatal(err)\n\t}\n}\n```\n\n# TODO\n\n* ~add http middleware~\n* ~add redis datastore~\n* improve unit coverage\n* improve redis datastore\n* add tarantool datastore\n* add aerospike datastore\n\n## Contributing\n\n\n## License\n\ngo-ratelimiter is under the Apache 2.0 license. See the [LICENSE](LICENSE) file for details.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frobotomize%2Fgo-ratelimiter","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Frobotomize%2Fgo-ratelimiter","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frobotomize%2Fgo-ratelimiter/lists"}