{"id":13414128,"url":"https://github.com/reugn/equalizer","last_synced_at":"2025-07-06T20:37:59.230Z","repository":{"id":55012487,"uuid":"191914807","full_name":"reugn/equalizer","owner":"reugn","description":"A set of performant rate limiters for Go","archived":false,"fork":false,"pushed_at":"2024-03-14T08:46:32.000Z","size":38,"stargazers_count":90,"open_issues_count":0,"forks_count":3,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-04-15T04:39:06.574Z","etag":null,"topics":["api","quota","quotas","rate","rate-limit","rate-limiter","rate-limiting","ratelimit","ratelimiter","sliding-window","throttle","throttler","throttling","tokenbucket"],"latest_commit_sha":null,"homepage":"https://pkg.go.dev/github.com/reugn/equalizer","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/reugn.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":"2019-06-14T09:25:13.000Z","updated_at":"2025-02-26T03:54:36.000Z","dependencies_parsed_at":"2024-06-19T00:10:49.777Z","dependency_job_id":"95da3492-38d8-4d0e-9f2b-438e004acfc5","html_url":"https://github.com/reugn/equalizer","commit_stats":null,"previous_names":[],"tags_count":1,"template":false,"template_full_name":null,"purl":"pkg:github/reugn/equalizer","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/reugn%2Fequalizer","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/reugn%2Fequalizer/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/reugn%2Fequalizer/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/reugn%2Fequalizer/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/reugn","download_url":"https://codeload.github.com/reugn/equalizer/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/reugn%2Fequalizer/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":263969178,"owners_count":23537381,"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","quota","quotas","rate","rate-limit","rate-limiter","rate-limiting","ratelimit","ratelimiter","sliding-window","throttle","throttler","throttling","tokenbucket"],"created_at":"2024-07-30T20:01:58.459Z","updated_at":"2025-07-06T20:37:59.193Z","avatar_url":"https://github.com/reugn.png","language":"Go","readme":"\u003ch1 align=\"center\"\u003eequalizer\u003c/h1\u003e\n\u003cp align=\"center\"\u003e\n  \u003ca href=\"https://github.com/reugn/equalizer/actions/workflows/build.yml\"\u003e\u003cimg src=\"https://github.com/reugn/equalizer/actions/workflows/build.yml/badge.svg\"\u003e\u003c/a\u003e\n  \u003ca href=\"https://pkg.go.dev/github.com/reugn/equalizer\"\u003e\u003cimg src=\"https://pkg.go.dev/badge/github.com/reugn/equalizer\"\u003e\u003c/a\u003e\n  \u003ca href=\"https://goreportcard.com/report/github.com/reugn/equalizer\"\u003e\u003cimg src=\"https://goreportcard.com/badge/github.com/reugn/equalizer\"\u003e\u003c/a\u003e\n  \u003ca href=\"https://codecov.io/gh/reugn/equalizer\"\u003e\u003cimg src=\"https://codecov.io/gh/reugn/equalizer/branch/master/graph/badge.svg\"\u003e\u003c/a\u003e\n\u003c/p\u003e\n\nThe `equalizer` package provides a set of simple and easy-to-use rate limiters for Go.\nThese rate limiters can be used to limit the rate of requests to any resource, such as a database,\nAPI, or file.\n\nThe package includes the following rate limiters:\n* [Equalizer](#equalizer)\n* [Slider](#slider)\n* [Token Bucket](#token-bucket)\n\n## Equalizer\n`Equalizer` is a rate limiter that adjusts the rate of requests based on the outcomes of previous requests.\nIf previous attempts have failed, Equalizer will slow down the rate of requests to avoid overloading the system.\nConversely, if previous attempts have been successful, Equalizer will accelerate the rate of requests to make\nthe most of the available capacity.\n\n### Usage Example\n```go\n// a random offset manager \noffset := equalizer.NewRandomOffset(96)\n// an Equalizer with a bitmap size of 96, 16 reserved positive bits, and the random offset manager\neq := equalizer.NewEqualizer(96, 16, offset)\n// non-blocking quota request\nhaveQuota := eq.TryAcquire()\n// update on the successful request\neq.Success(1)\n```\n\n### Benchmarks\n```console\nBenchmarkEqualizer_ShortTryAcquireStep-16       31538967                38.33 ns/op            0 B/op          0 allocs/op\nBenchmarkEqualizer_ShortTryAcquireRandom-16     37563639                31.66 ns/op            0 B/op          0 allocs/op\nBenchmarkEqualizer_ShortNotify-16               29519719                40.43 ns/op            0 B/op          0 allocs/op\nBenchmarkEqualizer_LongTryAcquireStep-16        32084402                38.36 ns/op            0 B/op          0 allocs/op\nBenchmarkEqualizer_LongTryAcquireRandom-16      39996501                30.37 ns/op            0 B/op          0 allocs/op\nBenchmarkEqualizer_LongNotify-16                29648655                40.46 ns/op            0 B/op          0 allocs/op\n```\n\n## Slider\n`Slider` tracks the number of requests that have been processed in a recent time window.\nIf the number of requests exceeds the limit, the rate limiter will block new requests until the window\nhas moved forward.\nImplements the `equalizer.Limiter` interface.\n\n### Usage Example\n```go\n// a Slider with a one-second window size, a 100-millisecond sliding interval,\n// and a capacity of 32\nslider := equalizer.NewSlider(time.Second, 100*time.Millisecond, 32)\n// non-blocking quota request\nhaveQuota := slider.TryAcquire()\n// blocking call\nslider.Acquire(context.Background())\n```\n\n### Benchmarks\n```console\nBenchmarkSlider_TryAcquire-16                   293645348                4.033 ns/op           0 B/op          0 allocs/op\nBenchmarkRateLimiter_Allow-16                     9362382              127.4 ns/op             0 B/op          0 allocs/op\n```\n\u003csup\u003e*\u003c/sup\u003e Compared to `rate.Limiter` from the [golang.org/x/time](https://pkg.go.dev/golang.org/x/time/rate) package.\n\n## Token Bucket\n`TokenBucket` maintains a fixed number of tokens. Each token represents a request that can be processed.\nWhen a request is made, the rate limiter checks to see if there are any available tokens. If there are,\nthe request is processed and one token is removed from the bucket. If there are no available tokens,\nthe request is blocked until a token becomes available.\nImplements the `equalizer.Limiter` interface.\n\n### Usage Example\n```go\n// a TokenBucket with the capacity of 32 and a 100-millisecond refill interval\ntokenBucket := equalizer.NewTokenBucket(32, 100*time.Millisecond)\n// non-blocking quota request\nhaveQuota := tokenBucket.TryAcquire()\n// blocking call\ntokenBucket.Acquire(context.Background())\n```\n\n### Benchmarks\n```console\nBenchmarkTokenBucket_TryAcquire-16              304653043                3.909 ns/op           0 B/op          0 allocs/op\nBenchmarkRateLimiter_Allow-16                     9362382              127.4 ns/op             0 B/op          0 allocs/op\n```\n\u003csup\u003e*\u003c/sup\u003e Compared to `rate.Limiter` from the [golang.org/x/time](https://pkg.go.dev/golang.org/x/time/rate) package.\n\n## License\nLicensed under the [MIT](./LICENSE) License.\n","funding_links":[],"categories":["Utilities","公用事业公司","工具库`可以提升效率的通用代码库和工具`","Utility","工具库"],"sub_categories":["Utility/Miscellaneous","实用程序/Miscellaneous","HTTP Clients","Fail injection","查询语"],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Freugn%2Fequalizer","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Freugn%2Fequalizer","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Freugn%2Fequalizer/lists"}