{"id":22124117,"url":"https://github.com/let4be/relabuf","last_synced_at":"2025-07-26T16:40:00.099Z","repository":{"id":57659541,"uuid":"375824409","full_name":"let4be/relabuf","owner":"let4be","description":"Release at threshold buffer","archived":false,"fork":false,"pushed_at":"2021-10-23T10:13:32.000Z","size":76,"stargazers_count":2,"open_issues_count":2,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2024-11-13T18:36:05.510Z","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/let4be.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":"2021-06-10T20:36:28.000Z","updated_at":"2022-10-21T10:01:40.000Z","dependencies_parsed_at":"2022-09-08T00:11:34.054Z","dependency_job_id":null,"html_url":"https://github.com/let4be/relabuf","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/let4be%2Frelabuf","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/let4be%2Frelabuf/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/let4be%2Frelabuf/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/let4be%2Frelabuf/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/let4be","download_url":"https://codeload.github.com/let4be/relabuf/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":227590143,"owners_count":17790446,"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-01T15:46:36.272Z","updated_at":"2024-12-01T15:46:37.897Z","avatar_url":"https://github.com/let4be.png","language":"Rust","funding_links":[],"categories":[],"sub_categories":[],"readme":"![crates.io](https://img.shields.io/crates/v/relabuf.svg)\n[![Dependency status](https://deps.rs/repo/github/let4be/relabuf/status.svg)](https://deps.rs/repo/github/let4be/relabuf)\n\n# Relabuf - smart buffer with release valve\n - consumes items from external `future`\n - buffers internally up to `hard_cap`\n - when `hard_cap` is reached no longer consumes causing producer to backoff and slowdown\n - capable of releasing contents ONLY under certain conditions\n - a `release_after` has passed since the latest successful content release(or since start) and buffer is not empty\n - a `soft_cap` of items were added\n - each consumption should be either `confirmed` or `returned` to the buffer\n - returns usually happen due to error(for example DB is down) - so it's possible to configure a backoff\n - backoff essentially overrides time release valve\n - exposes released items via a `future` user can `await` on\n## Install\n\n```\n[dependencies]\nrelabuf = \"~0.15.0\"\n```\n\nexample:\n\n```rust\nuse anyhow::Context;\nuse flume::{bounded, Sender};\nuse relabuf::{ExponentialBackoff, RelaBuf, RelaBufConfig};\nuse std::time::{Duration, Instant};\nuse async_io::Timer;\n\nasync fn producer(tx: Sender\u003cu32\u003e) {\n    for i in 0..16 {\n        let dur = Duration::from_millis(150_u64 * (i as u64));\n        println!(\"waiting {:?} before emitting {}\", \u0026dur, i);\n        Timer::interval(dur).await;\n\n        let t = Instant::now();\n        let r = tx.send_async(i).await;\n        println!(\"emit for {} took {:?}: {:?}\", i, t.elapsed(), r);\n    }\n    println!(\"producer is finished!\")\n}\n\n#[tokio::main]\nasync fn main() {\n    let (tx, rx) = bounded(5);\n\n    tokio::spawn(producer(tx));\n\n    let opts = RelaBufConfig {\n        soft_cap: 3,\n        hard_cap: 5,\n        release_after: Duration::from_secs(5),\n        backoff: Some(ExponentialBackoff {\n            max_elapsed_time: None,\n            ..ExponentialBackoff::default()\n        }),\n    };\n\n    let (buf, proxy) = RelaBuf::new(opts, move || {\n        let rx = rx.clone();\n        Box::pin(async move { rx.recv_async().await.context(\"cannot read\") })\n    });\n    \n    tokio::spawn(proxy.go());\n\n    let mut i = 0;\n\n    while let Ok(consumed) = buf.next().await {\n        i += 1;\n\n        if i \u003c= 7 {\n            println!(\n                \"consumed {:?} because {:?}, since last consumption {:?} - returning due to err\",\n                consumed.items, consumed.reason, consumed.elapsed\n            );\n            consumed.return_on_err();\n        } else {\n            println!(\n                \"consumed {:?} because {:?}, since last consumption {:?}\",\n                consumed.items, consumed.reason, consumed.elapsed\n            );\n            consumed.confirm();\n        }\n    }\n    println!(\"done ;)\");\n}\n```","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flet4be%2Frelabuf","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Flet4be%2Frelabuf","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flet4be%2Frelabuf/lists"}