{"id":18684840,"url":"https://github.com/j-sephb-lt-n/rate-limiter-token-bucket-algorithm","last_synced_at":"2026-04-15T06:31:41.730Z","repository":{"id":205178684,"uuid":"713563466","full_name":"J-sephB-lt-n/rate-limiter-token-bucket-algorithm","owner":"J-sephB-lt-n","description":"An example implementation of the token-bucket algorithm for API rating limiting using python, redis and FastAPI","archived":false,"fork":false,"pushed_at":"2023-11-05T10:27:27.000Z","size":28,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2024-12-28T00:21:17.643Z","etag":null,"topics":["api","fastapi","rate-limiting","redis","token-bucket"],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"gpl-3.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/J-sephB-lt-n.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-11-02T19:21:08.000Z","updated_at":"2024-07-18T07:41:05.000Z","dependencies_parsed_at":null,"dependency_job_id":"93bc3310-f4e0-47ac-9ce6-bfabc2b01be7","html_url":"https://github.com/J-sephB-lt-n/rate-limiter-token-bucket-algorithm","commit_stats":null,"previous_names":["j-sephb-lt-n/rate-limiter-token-bucket-algorithm"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/J-sephB-lt-n%2Frate-limiter-token-bucket-algorithm","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/J-sephB-lt-n%2Frate-limiter-token-bucket-algorithm/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/J-sephB-lt-n%2Frate-limiter-token-bucket-algorithm/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/J-sephB-lt-n%2Frate-limiter-token-bucket-algorithm/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/J-sephB-lt-n","download_url":"https://codeload.github.com/J-sephB-lt-n/rate-limiter-token-bucket-algorithm/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":239539795,"owners_count":19655881,"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","fastapi","rate-limiting","redis","token-bucket"],"created_at":"2024-11-07T10:19:30.850Z","updated_at":"2026-04-15T06:31:36.698Z","avatar_url":"https://github.com/J-sephB-lt-n.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"# rate-limiter-token-bucket-algorithm\nAn example implementation of the [token-bucket algorithm]() for API rating-limiting, using python, redis and FastAPI.\n\nThe concept is very simple: Users acquire tokens at a fixed rate (up to a maximum number of tokens) and then consume a single token with each API request. If a user has no tokens at request time, then they receive a \n\n**429: Too Many Requests** \n\nresponse to their request. This algorithm could be adapted to allow for different request sizes to consume different numbers of tokens (or fractions of tokens). \n\nStart redis database and fastAPI servers in the background:\n\n```bash\nredis-server --daemonize yes\nnohup uvicorn main:app --port 8000 \u0026\n```\n\nRun a simple test case: \n\n```bash\npython run_random_test_case.py\n```\n```\nuser_id=joe request@21:02:33 response_status_code: 200 response: OK\nuser_id=joe request@21:02:33 response_status_code: 200 response: OK\nuser_id=joe request@21:02:34 response_status_code: 200 response: OK\nuser_id=joe request@21:02:35 response_status_code: 200 response: OK\nuser_id=joe request@21:02:36 response_status_code: 200 response: OK\nuser_id=joe request@21:02:37 response_status_code: 200 response: OK\nuser_id=joe request@21:02:37 response_status_code: 429 response: Too Many Requests\nuser_id=joe request@21:02:38 response_status_code: 429 response: Too Many Requests\nuser_id=joe request@21:02:39 response_status_code: 200 response: OK\nuser_id=joe request@21:02:40 response_status_code: 429 response: Too Many Requests\nuser_id=joe request@21:02:41 response_status_code: 429 response: Too Many Requests\nuser_id=joe request@21:02:43 response_status_code: 200 response: OK\nuser_id=joe request@21:02:44 response_status_code: 429 response: Too Many Requests\nuser_id=joe request@21:02:45 response_status_code: 200 response: OK\nuser_id=joe request@21:02:47 response_status_code: 429 response: Too Many Requests\nuser_id=joe request@21:02:48 response_status_code: 200 response: OK\nuser_id=joe request@21:02:49 response_status_code: 429 response: Too Many Requests\nuser_id=joe request@21:02:50 response_status_code: 429 response: Too Many Requests\nuser_id=joe request@21:02:52 response_status_code: 200 response: OK\nuser_id=joe request@21:02:53 response_status_code: 429 response: Too Many Requests\n```\n\nStop the redis database and fastAPI servers, and clean up:\n\n```bash\nredis-cli shutdown\nrm dump.rdb\nUVICORN_PID=$(pgrep -f \"uvicorn main:app\")\nkill -15 $UVICORN_PID\nrm nohup.out\n```","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fj-sephb-lt-n%2Frate-limiter-token-bucket-algorithm","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fj-sephb-lt-n%2Frate-limiter-token-bucket-algorithm","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fj-sephb-lt-n%2Frate-limiter-token-bucket-algorithm/lists"}