{"id":15821044,"url":"https://github.com/anilsenay/throttle","last_synced_at":"2025-10-30T06:41:40.903Z","repository":{"id":254583161,"uuid":"846955166","full_name":"anilsenay/throttle","owner":"anilsenay","description":"simple and efficient way to throttle operations in Golang by using an iterator","archived":false,"fork":false,"pushed_at":"2024-08-24T14:01:16.000Z","size":29,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2024-10-12T07:40:23.831Z","etag":null,"topics":["go","go-iter","go123","golang","iterator","iterators"],"latest_commit_sha":null,"homepage":"","language":"Go","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/anilsenay.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-08-24T12:37:48.000Z","updated_at":"2024-08-24T14:05:06.000Z","dependencies_parsed_at":null,"dependency_job_id":"b1c5da86-a896-4bce-b606-a07265873dcd","html_url":"https://github.com/anilsenay/throttle","commit_stats":{"total_commits":10,"total_committers":1,"mean_commits":10.0,"dds":0.0,"last_synced_commit":"cc94c0c9cd399ba33789389ca9355e37a5ad142e"},"previous_names":["anilsenay/throttle"],"tags_count":2,"template":false,"template_full_name":null,"purl":"pkg:github/anilsenay/throttle","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/anilsenay%2Fthrottle","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/anilsenay%2Fthrottle/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/anilsenay%2Fthrottle/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/anilsenay%2Fthrottle/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/anilsenay","download_url":"https://codeload.github.com/anilsenay/throttle/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/anilsenay%2Fthrottle/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":267616825,"owners_count":24116167,"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","status":"online","status_checked_at":"2025-07-28T02:00:09.689Z","response_time":68,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"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","go-iter","go123","golang","iterator","iterators"],"created_at":"2024-10-05T07:20:18.301Z","updated_at":"2025-10-30T06:41:40.844Z","avatar_url":"https://github.com/anilsenay.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"# throttle\n\n[![Build Status](https://github.com/anilsenay/throttle/actions/workflows/go.yml/badge.svg?branch=master)](https://github.com/features/actions)\n[![Coverage Status](https://img.shields.io/codecov/c/github/anilsenay/throttle/master.svg)](https://codecov.io/gh/anilsenay/throttle)\n[![Go Report Card](https://goreportcard.com/badge/github.com/anilsenay/throttle)](https://goreportcard.com/report/github.com/anilsenay/throttle)\n[![Go Doc](https://godoc.org/github.com/anilsenay/throttle?status.svg)](https://godoc.org/github.com/anilsenay/throttle)\n[![Release](https://img.shields.io/github/release/anilsenay/throttle.svg?style=flat-square)](https://github.com/anilsenay/throttle/releases)\n\n`throttle` is a Go package that provides a simple and efficient way to throttle operations in your Go applications. It allows you to limit the rate at which operations are performed, which can be useful for rate limiting API calls, controlling resource usage, or managing concurrency.\n\n## Features\n\n- Easy-to-use throttling mechanism via Iterators\n- Supports generic slices\n- Configurable operations per interval\n- Uses efficient channels and tickers for throttling\n\n## Prerequirement\n\n- Go 1.23 ❗\n\n## Installation\n\nTo install the throttle package, use the following command:\n\n```bash\ngo get github.com/anilsenay/throttle\n```\n\n## Usage\n\nHere's a basic example of how to use the `Limit` function:\n\n```go\npackage main\n\nimport (\n    \"fmt\"\n    \"time\"\n\n    \"github.com/anilsenay/throttle\"\n)\n\nfunc main() {\n    numbers := []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}\n\n    // Throttle to 2 operations per second\n    for i, v := range throttle.Limit(numbers, 2, time.Second) {\n        fmt.Printf(\"Processing: %d\\n\", v)\n    }\n}\n```\n\n```go\nOutput:\n\nProcessing: 1\nProcessing: 2\n// wait 1 second\nProcessing: 3\nProcessing: 4\n// wait 1 second\n...\n```\n\nThis example will process the numbers slice at a rate of 2 items per second.\n\n## API Reference\n\n### throttle package\n\n#### func [Limit](https://github.com/anilsenay/throttle/blob/master/throttle.go#L10)\n\n`func Limit[Slice ~[]E, E any](s Slice, ops int, interval time.Duration) iter.Seq2[int, E]`\n\n`Limit` takes a slice, the number of operations per interval, and the interval duration. It returns an iterator that yields the index and value of each element in the slice, throttled according to the specified rate.\n\n`Limit` provides a high-level, iterator-based API that internally uses `Throttler` for the actual rate limiting mechanism.\n\n### throttler package\n\n#### type [Throttler](https://github.com/anilsenay/throttle/blob/master/throttler/throttler.go#L8)\n\n```go\ntype Throttler struct {\n    bucket      int\n    bucketLimit int\n    interval    time.Duration\n    ticker      *time.Ticker\n    done        bool\n    waitCh      chan struct{}\n    once        sync.Once\n}\n```\n\nThe Throttler struct provides low-level control over throttling. It can be used directly for more fine-grained throttling needs.\n\n#### func [New](https://github.com/anilsenay/throttle/blob/master/throttler/throttler.go#L18)\n\n`func New(ops int, interval time.Duration) *Throttler`\n\nCreates a new Throttler with the specified number of operations per interval.\n\n#### func (\\*Throttler) [Allow](https://github.com/anilsenay/throttle/blob/master/throttler/throttler.go#L51)\n\n`func (t *Throttler) Allow() bool`\n\nAllow checks if an operation is permitted based on the current throttling state. It returns `true` if the operation is allowed, and `false` otherwise.\n\n#### func (\\*Throttler) [Stop](https://github.com/anilsenay/throttle/blob/master/throttler/throttler.go#L30)\n\n`func (t *Throttler) Stop()`\n\nStops the throttler and releases associated resources.\n\n#### func (\\*Throttler) [IsDone](https://github.com/anilsenay/throttle/blob/master/throttler/throttler.go#L26)\n\n`func (t *Throttler) IsDone() bool`\n\nIsDone returns `true` if the Throttler has been stopped, and `false` otherwise.\n\n##### Example:\n\n```go\nth := New(3, time.Second)\n\ni := 0\nfor th.Allow() {\n    if i == 10 {\n        th.Stop()\n    }\n    i++\n}\n```\n\n## Internal Workings\n\nThe Throttler uses a token bucket algorithm for rate limiting:\n\n- The bucket is filled with tokens at the start of each interval.\n- Each Allow() call consumes one token.\n- If the bucket is empty, Allow() blocks until the next interval.\n- A background goroutine refills the bucket at the start of each interval.\n\n## Contributing\n\nContributions are welcome! Please feel free to submit a Pull Request.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fanilsenay%2Fthrottle","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fanilsenay%2Fthrottle","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fanilsenay%2Fthrottle/lists"}