{"id":27262259,"url":"https://github.com/brandenc40/ratelimiter","last_synced_at":"2025-04-11T05:49:43.077Z","repository":{"id":44959730,"uuid":"448339858","full_name":"brandenc40/ratelimiter","owner":"brandenc40","description":"Thread safe Go rate limiter used to ensure a minimum duration between executions.","archived":false,"fork":false,"pushed_at":"2022-05-06T18:41:55.000Z","size":32,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-04-11T05:49:39.724Z","etag":null,"topics":["go","golang","limit","limiter","rate","rate-limit","rate-limiter","rate-limiting"],"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/brandenc40.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}},"created_at":"2022-01-15T17:05:05.000Z","updated_at":"2022-07-14T17:21:21.000Z","dependencies_parsed_at":"2022-09-26T20:22:07.130Z","dependency_job_id":null,"html_url":"https://github.com/brandenc40/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/brandenc40%2Fratelimiter","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/brandenc40%2Fratelimiter/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/brandenc40%2Fratelimiter/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/brandenc40%2Fratelimiter/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/brandenc40","download_url":"https://codeload.github.com/brandenc40/ratelimiter/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248351431,"owners_count":21089271,"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","limit","limiter","rate","rate-limit","rate-limiter","rate-limiting"],"created_at":"2025-04-11T05:49:42.543Z","updated_at":"2025-04-11T05:49:43.068Z","avatar_url":"https://github.com/brandenc40.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Ratelimiter [![Go Reference](https://pkg.go.dev/badge/github.com/brandenc40/ratelimiter#example-package.svg)](https://pkg.go.dev/github.com/brandenc40/ratelimiter#example-package)\n\nThread safe rate limiter used to ensure a minimum duration between executions. \n\nAdditionally supports the optional limit of max queue size. This can be used to ensure \nprograms don't bottleneck due to having too many requests queued by the ratelimiter at any given time.\n\n\n## Package Interface\n\n```go \n// NoLimit can be used as the minDuration arg value to New if no limiting is required.\nconst NoLimit = time.Duration(-1)\n\n// ErrQueueFull is returned with the queue limit set by WithMaxQueueSize is exceeded.\nvar ErrQueueFull = errors.New(\"ratelimiter: queue is full\")\n\n// RateLimiter provides functionality to block until ready to ensure a rate limit is not exceeded.\ntype RateLimiter interface {\n    // Wait blocks until the next call is ready based on the minimum time between calls.\n    Wait() error\n    // NumQueued returns the current number of queued requests. If WithMaxQueueSize is not set, the result will always be 0.\n    NumQueued() uint32\n}\n\n// New builds a new RateLimiter used to ensure calls adhere to a minimum duration between calls.\nfunc New(minDuration time.Duration, options ...Option) RateLimiter\n\n// WithMaxQueueSize sets the maximum number of requests that can be queued up. If the queue\n// limit is reached, ErrQueueFull will be returned when Wait is called.\nfunc WithMaxQueueSize(maxQueue uint32) Option\n```\n\n## Usage Example\n\n```go\npackage main\n\nimport (\n    \"time\"\n\n    \"github.com/brandenc40/ratelimiter\"\n)\n\nfunc main() {\n    rl := ratelimiter.New(\n        10*time.Millisecond,               // 10ms between calls (100 rps)\n        ratelimiter.WithMaxQueueSize(100), // (optional) max of 100 requests queued up before failure\n    )\n\n    for i := 0; i \u003c 100; i++ {\n        if err := rl.Wait(); err != nil {\n            // handle err\n        }\n        // do some rate limited functionality\n    }\n}\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbrandenc40%2Fratelimiter","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fbrandenc40%2Fratelimiter","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbrandenc40%2Fratelimiter/lists"}