{"id":13575342,"url":"https://github.com/juju/ratelimit","last_synced_at":"2025-04-23T20:49:01.738Z","repository":{"id":14753150,"uuid":"17474371","full_name":"juju/ratelimit","owner":"juju","description":"Efficient token-bucket-based rate limiter package.","archived":false,"fork":false,"pushed_at":"2023-10-05T04:11:43.000Z","size":37,"stargazers_count":2843,"open_issues_count":7,"forks_count":316,"subscribers_count":79,"default_branch":"master","last_synced_at":"2025-04-08T23:14:06.687Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"other","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/juju.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}},"created_at":"2014-03-06T10:38:58.000Z","updated_at":"2025-04-02T07:17:35.000Z","dependencies_parsed_at":"2024-01-13T23:20:15.155Z","dependency_job_id":null,"html_url":"https://github.com/juju/ratelimit","commit_stats":{"total_commits":40,"total_committers":9,"mean_commits":4.444444444444445,"dds":0.275,"last_synced_commit":"f60b32039441cd828005f82f3a54aafd00bc9882"},"previous_names":[],"tags_count":4,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/juju%2Fratelimit","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/juju%2Fratelimit/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/juju%2Fratelimit/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/juju%2Fratelimit/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/juju","download_url":"https://codeload.github.com/juju/ratelimit/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":250513729,"owners_count":21443204,"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-08-01T15:01:00.173Z","updated_at":"2025-04-23T20:49:01.695Z","avatar_url":"https://github.com/juju.png","language":"Go","funding_links":[],"categories":["Misc","开源类库","Go","Open source library"],"sub_categories":["限流器","Current Limiter"],"readme":"# ratelimit\n--\n    import \"github.com/juju/ratelimit\"\n\nThe ratelimit package provides an efficient token bucket implementation. See\nhttp://en.wikipedia.org/wiki/Token_bucket.\n\n## Usage\n\n#### func  Reader\n\n```go\nfunc Reader(r io.Reader, bucket *Bucket) io.Reader\n```\nReader returns a reader that is rate limited by the given token bucket. Each\ntoken in the bucket represents one byte.\n\n#### func  Writer\n\n```go\nfunc Writer(w io.Writer, bucket *Bucket) io.Writer\n```\nWriter returns a writer that is rate limited by the given token bucket. Each\ntoken in the bucket represents one byte.\n\n#### type Bucket\n\n```go\ntype Bucket struct {\n}\n```\n\nBucket represents a token bucket that fills at a predetermined rate. Methods on\nBucket may be called concurrently.\n\n#### func  NewBucket\n\n```go\nfunc NewBucket(fillInterval time.Duration, capacity int64) *Bucket\n```\nNewBucket returns a new token bucket that fills at the rate of one token every\nfillInterval, up to the given maximum capacity. Both arguments must be positive.\nThe bucket is initially full.\n\n#### func  NewBucketWithQuantum\n\n```go\nfunc NewBucketWithQuantum(fillInterval time.Duration, capacity, quantum int64) *Bucket\n```\nNewBucketWithQuantum is similar to NewBucket, but allows the specification of\nthe quantum size - quantum tokens are added every fillInterval.\n\n#### func  NewBucketWithRate\n\n```go\nfunc NewBucketWithRate(rate float64, capacity int64) *Bucket\n```\nNewBucketWithRate returns a token bucket that fills the bucket at the rate of\nrate tokens per second up to the given maximum capacity. Because of limited\nclock resolution, at high rates, the actual rate may be up to 1% different from\nthe specified rate.\n\n#### func (*Bucket) Available\n\n```go\nfunc (tb *Bucket) Available() int64\n```\nAvailable returns the number of available tokens. It will be negative\nwhen there are consumers waiting for tokens. Note that if this\nreturns greater than zero, it does not guarantee that calls that take\ntokens from the buffer will succeed, as the number of available\ntokens could have changed in the meantime. This method is intended\nprimarily for metrics reporting and debugging.\n\n#### func (*Bucket) Rate\n\n```go\nfunc (tb *Bucket) Rate() float64\n```\nRate returns the fill rate of the bucket, in tokens per second.\n\n#### func (*Bucket) Take\n\n```go\nfunc (tb *Bucket) Take(count int64) time.Duration\n```\nTake takes count tokens from the bucket without blocking. It returns the time\nthat the caller should wait until the tokens are actually available.\n\nNote that if the request is irrevocable - there is no way to return tokens to\nthe bucket once this method commits us to taking them.\n\n#### func (*Bucket) TakeAvailable\n\n```go\nfunc (tb *Bucket) TakeAvailable(count int64) int64\n```\nTakeAvailable takes up to count immediately available tokens from the bucket. It\nreturns the number of tokens removed, or zero if there are no available tokens.\nIt does not block.\n\n#### func (*Bucket) TakeMaxDuration\n\n```go\nfunc (tb *Bucket) TakeMaxDuration(count int64, maxWait time.Duration) (time.Duration, bool)\n```\nTakeMaxDuration is like Take, except that it will only take tokens from the\nbucket if the wait time for the tokens is no greater than maxWait.\n\nIf it would take longer than maxWait for the tokens to become available, it does\nnothing and reports false, otherwise it returns the time that the caller should\nwait until the tokens are actually available, and reports true.\n\n#### func (*Bucket) Wait\n\n```go\nfunc (tb *Bucket) Wait(count int64)\n```\nWait takes count tokens from the bucket, waiting until they are available.\n\n#### func (*Bucket) WaitMaxDuration\n\n```go\nfunc (tb *Bucket) WaitMaxDuration(count int64, maxWait time.Duration) bool\n```\nWaitMaxDuration is like Wait except that it will only take tokens from the\nbucket if it needs to wait for no greater than maxWait. It reports whether any\ntokens have been removed from the bucket If no tokens have been removed, it\nreturns immediately.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjuju%2Fratelimit","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjuju%2Fratelimit","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjuju%2Fratelimit/lists"}