{"id":16773833,"url":"https://github.com/ziflex/throttle","last_synced_at":"2025-03-16T17:21:32.138Z","repository":{"id":237773162,"uuid":"795197639","full_name":"ziflex/throttle","owner":"ziflex","description":"Client-side throttler based on fixed window counter algorithm.","archived":false,"fork":false,"pushed_at":"2024-06-29T22:05:01.000Z","size":20,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-01-23T04:14:45.886Z","etag":null,"topics":[],"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/ziflex.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-05-02T19:19:17.000Z","updated_at":"2025-01-18T20:35:46.000Z","dependencies_parsed_at":"2024-11-22T16:49:51.307Z","dependency_job_id":"7740abe8-5180-4a8a-860c-20cbe8e40685","html_url":"https://github.com/ziflex/throttle","commit_stats":null,"previous_names":["ziflex/throttle"],"tags_count":6,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ziflex%2Fthrottle","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ziflex%2Fthrottle/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ziflex%2Fthrottle/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ziflex%2Fthrottle/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/ziflex","download_url":"https://codeload.github.com/ziflex/throttle/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":243903167,"owners_count":20366434,"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":[],"created_at":"2024-10-13T06:47:09.228Z","updated_at":"2025-03-16T17:21:32.109Z","avatar_url":"https://github.com/ziflex.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Throttle\n\u003e Dead-simple thread-safe throttler.\n\n``throttle`` provides a thread-safe mechanism to throttle function calls, ensuring that the execution rate does not exceed a specified limit.    \nEach instance operates independently, making it possible to control various functions or processes with different rate limits concurrently.\n\n## Install\n```shell\ngo get github.com/ziflex/throttle\n```\n\n## Quick start\n\n```go\npackage myapp\n\nimport (\n    \"context\"\n    \"net/http\"\n    \"github.com/ziflex/throttle\"\n)\n\ntype ApiClient struct {\n    transport *http.Client\n    throttler *throttle.Throttler\n}\n\nfunc NewApiClient(rps uint64) *ApiClient {\n    return \u0026ApiClient{\n        transport: \u0026http.Client{},\n        throttler: throttle.New(rps),\n    }\n}\n\nfunc (c *ApiClient) Do(ctx context.Context, req *http.Request) (*http.Response, error) {\n\tc.throttler.Acquire()\n\t\n\tselect {\n\tcase \u003c-ctx.Done():\n\t\treturn nil, ctx.Err()\n\tdefault:\n\t\treturn c.transport.Do(req)\n\t}\n}\n```\n\n## Options\n\n### Clock\n`Clock` type is an interface that allows you to provide custom clock mechanism that's different from the system one.   \nIt has just 2 methods: ``Now()`` and ``Sleep(time.Duration)``.   \nIt might be useful to use a custom implementation to provide a more nuanced timing mechanism. \n\n```go\npackage myapp\n\nimport (\n    \"time\"\n    \"github.com/ziflex/throttle\"\n)\n\ntype MyClock struct {\n    offset time.Duration\n}\n\nfunc (c *MyClock) Now() time.Time {\n    return time.Now().Add(c.offset)\n}\n\nfunc (c *MyClock) Sleep(dur time.Duration) {\n    time.Sleep(dur + c.offset)\n}\n\nfunc main() {\n    throttler := throttle.New(10, throttle.WithClock(\u0026MyClock{time.Millisecond * 250}))\n}\n```\n\n## Helpers\n### RoundTripper\nThe package contains a helper that wraps the standard `http.RoundTripper` interface and provides a throttling mechanism.\n\n```go\npackage myapp\n\nimport (\n    \"context\"\n    \"net/http\"\n    \"github.com/ziflex/throttle\"\n)\n\nfunc main() {\n    transport := \u0026http.Transport{}\n    client := \u0026http.Client{\n        Transport: throttle.NewRoundTripper(transport, 10),\n    }\n\n    req, _ := http.NewRequest(http.MethodGet, \"https://example.com\", nil)\n    client.Do(req)\n}\n```","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fziflex%2Fthrottle","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fziflex%2Fthrottle","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fziflex%2Fthrottle/lists"}