{"id":23091554,"url":"https://github.com/chopinsky/futures-rate","last_synced_at":"2025-08-16T09:30:30.421Z","repository":{"id":57632729,"uuid":"227234542","full_name":"Chopinsky/futures-rate","owner":"Chopinsky","description":"A rust package to help applications guard critical resources or code path when using futures.","archived":false,"fork":false,"pushed_at":"2020-02-22T04:01:11.000Z","size":66,"stargazers_count":6,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2024-12-01T01:16:38.629Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Rust","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/Chopinsky.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":"2019-12-10T23:24:55.000Z","updated_at":"2023-01-04T12:25:18.000Z","dependencies_parsed_at":"2022-08-31T13:01:33.382Z","dependency_job_id":null,"html_url":"https://github.com/Chopinsky/futures-rate","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Chopinsky%2Ffutures-rate","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Chopinsky%2Ffutures-rate/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Chopinsky%2Ffutures-rate/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Chopinsky%2Ffutures-rate/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Chopinsky","download_url":"https://codeload.github.com/Chopinsky/futures-rate/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":230026316,"owners_count":18161593,"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-12-16T21:18:38.202Z","updated_at":"2024-12-16T21:18:38.271Z","avatar_url":"https://github.com/Chopinsky.png","language":"Rust","funding_links":[],"categories":[],"sub_categories":[],"readme":"[futures-rate][docsrs]\n======================\n\n[![futures-rate on crates.io][cratesio-image]][cratesio]\n[![futures-rate on docs.rs][docsrs-image]][docsrs]\n\n[cratesio]: https://crates.io/crates/futures-rate\n[cratesio-image]: https://img.shields.io/crates/v/futures-rate.svg\n[docsrs-image]: https://docs.rs/futures-rate/badge.svg\n[docsrs]: https://docs.rs/futures-rate\n\n## What is this library\n\nThis library provides easy tools to help Rust applications guide\ncritical resources or code paths from being overwhelmed. \n\nDepending on the configuration, the library will limit the amount\nof guarded futures being polled concurrently.\n\n## How to use\n\n#### Installation\n\nFirst of all, add the dependency to your Rust project:\n\n```bash\n$ cargo install futures-rate\n``` \n\nOr in your project's `Cargo.toml`, add the following dependency:\n\n```toml\n[dependencies]\nfutures-rate = \"^0.1.0\"\n```\n\nthen run `$ cargo install` in your terminal from project's root directory.\n\n#### Limit access rate\n\n* Create and manage a [`GateKeeper`] object in your main thread, which will\nset the access limit to a certain resource:\n\n```rust\nuse futures_rate::GateKeeper;\n\n/// then in main thread\nfn main() {\n    // ... other code\n\n    // At most 10 futures can pass the gate at any given time  \n    let gatekeeper = GateKeeper::new(10);\n\n    // ... more code\n}\n```\n\n* Then register your future to the [`GateKeeper`] such that the resourceful\nfuture can be protected:\n\n```rust\nuse futures_rate::{GateKeeper, Permit};\n\n/// in the business logic which has access to the `gatekeeper` object\nasync fn work(gatekeeper: \u0026GateKeeper) -\u003e usize {\n    // create the IO-heavy future\n    let ioFut = async { \n        // do async work here \n    };\n\n    let permit = gatekeeper.register(async {\n        // At most 10 IO work can be on-the-fly at any given time\n        ioFut.await;        \n        \n        // the result of all questions is always 42\n        42\n    });\n    \n    permit.await\n}\n``` \n\n#### Naive Future Lock\n\nIf setting the \n\n\n## Examples\n\nA classic use case is to place a [`GateKeeper`] over an a client socket pool, such that only\na limited number of future visitors can be allowed to poll the resources and hence limit the\namount of open connections.\n\n ```rust\n use futures::{executor, future};\n use futures_rate::{GateKeeper, Permit};\n use std::future::Future;\n use std::thread;\n use std::time::Duration;\n\n fn main() {\n     let gatekeeper = GateKeeper::new(1);\n     let fut_values = async {\n         let fut_1 = build_fut(0, \u0026gatekeeper);\n         let fut_2 = build_fut(1, \u0026gatekeeper);\n         let fin = future::join(fut_1, fut_2);\n         fin.await\n     };\n\n     let values = executor::block_on(fut_values);\n\n     println!(\"Values from fut_1={:?}\", values.0);\n     println!(\"Values from fut_2={:?}\", values.1);\n }\n\n fn build_fut(\n     offset: i32,\n     gatekeeper: \u0026GateKeeper,\n ) -\u003e Permit\u003cVec\u003ci32\u003e, impl Future\u003cOutput = Vec\u003ci32\u003e\u003e\u003e {\n     gatekeeper.register(async move {\n         let mut values = Vec::with_capacity(100);\n         (0..100).for_each(|v| {\n             thread::sleep(Duration::from_millis(1));\n             values.push(2 * v + offset);\n         });\n\n         values\n     }).unwrap()\n }\n ```\n\n [`GateKeeper`]: struct.GateKeeper.html\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fchopinsky%2Ffutures-rate","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fchopinsky%2Ffutures-rate","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fchopinsky%2Ffutures-rate/lists"}