{"id":16936469,"url":"https://github.com/jonhoo/stream-cancel","last_synced_at":"2025-05-15T01:08:45.201Z","repository":{"id":46091070,"uuid":"138221669","full_name":"jonhoo/stream-cancel","owner":"jonhoo","description":"A Rust library for interrupting asynchronous streams.","archived":false,"fork":false,"pushed_at":"2024-12-31T10:01:21.000Z","size":140,"stargazers_count":159,"open_issues_count":3,"forks_count":8,"subscribers_count":3,"default_branch":"main","last_synced_at":"2025-05-07T09:56:17.311Z","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":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/jonhoo.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE-APACHE","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":"2018-06-21T21:05:35.000Z","updated_at":"2025-04-05T21:10:31.000Z","dependencies_parsed_at":"2024-06-19T01:37:46.069Z","dependency_job_id":"8bfc1c00-4e25-46b9-a2ab-680ddaa65eec","html_url":"https://github.com/jonhoo/stream-cancel","commit_stats":{"total_commits":115,"total_committers":16,"mean_commits":7.1875,"dds":"0.16521739130434787","last_synced_commit":"e72412eb59f3871647577ad93e207db6c9e0a489"},"previous_names":[],"tags_count":22,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jonhoo%2Fstream-cancel","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jonhoo%2Fstream-cancel/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jonhoo%2Fstream-cancel/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jonhoo%2Fstream-cancel/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/jonhoo","download_url":"https://codeload.github.com/jonhoo/stream-cancel/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":254254042,"owners_count":22039792,"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-10-13T20:57:03.109Z","updated_at":"2025-05-15T01:08:40.195Z","avatar_url":"https://github.com/jonhoo.png","language":"Rust","funding_links":[],"categories":["others"],"sub_categories":[],"readme":"[![Crates.io](https://img.shields.io/crates/v/stream-cancel.svg)](https://crates.io/crates/stream-cancel)\n[![Documentation](https://docs.rs/stream-cancel/badge.svg)](https://docs.rs/stream-cancel/)\n[![codecov](https://codecov.io/gh/jonhoo/stream-cancel/graph/badge.svg?token=6XoCSUKWXW)](https://codecov.io/gh/jonhoo/stream-cancel)\n\nThis crate provides multiple mechanisms for interrupting a `Stream`.\n\n## Stream combinator\n\nThe extension trait [`StreamExt`] provides a single new `Stream` combinator: `take_until_if`.\n[`StreamExt::take_until_if`] continues yielding elements from the underlying `Stream` until a\n`Future` resolves, and at that moment immediately yields `None` and stops producing further\nelements.\n\nFor convenience, the crate also includes the [`Tripwire`] type, which produces a cloneable\n`Future` that can then be passed to `take_until_if`. When a new `Tripwire` is created, an\nassociated [`Trigger`] is also returned, which interrupts the `Stream` when it is dropped.\n\n\n```rust\nuse stream_cancel::{StreamExt, Tripwire};\nuse futures::prelude::*;\nuse tokio_stream::wrappers::TcpListenerStream;\n\n#[tokio::main]\nasync fn main() {\n    let listener = tokio::net::TcpListener::bind(\"127.0.0.1:0\").await.unwrap();\n    let (trigger, tripwire) = Tripwire::new();\n\n    tokio::spawn(async move {\n        let mut incoming = TcpListenerStream::new(listener).take_until_if(tripwire);\n        while let Some(mut s) = incoming.next().await.transpose().unwrap() {\n            tokio::spawn(async move {\n                let (mut r, mut w) = s.split();\n                println!(\"copied {} bytes\", tokio::io::copy(\u0026mut r, \u0026mut w).await.unwrap());\n            });\n        }\n    });\n\n    // tell the listener to stop accepting new connections\n    drop(trigger);\n    // the spawned async block will terminate cleanly, allowing main to return\n}\n```\n\n## Stream wrapper\n\nAny stream can be wrapped in a [`Valved`], which enables it to be remotely terminated through\nan associated [`Trigger`]. This can be useful to implement graceful shutdown on \"infinite\"\nstreams like a `TcpListener`. Once [`Trigger::cancel`] is called on the handle for a given\nstream's [`Valved`], the stream will yield `None` to indicate that it has terminated.\n\n```rust\nuse stream_cancel::Valved;\nuse futures::prelude::*;\nuse tokio_stream::wrappers::TcpListenerStream;\nuse std::thread;\n\n#[tokio::main]\nasync fn main() {\n    let (exit_tx, exit_rx) = tokio::sync::oneshot::channel();\n    let listener = tokio::net::TcpListener::bind(\"127.0.0.1:0\").await.unwrap();\n\n    tokio::spawn(async move {\n        let (exit, mut incoming) = Valved::new(TcpListenerStream::new(listener));\n        exit_tx.send(exit).unwrap();\n        while let Some(mut s) = incoming.next().await.transpose().unwrap() {\n            tokio::spawn(async move {\n                let (mut r, mut w) = s.split();\n                println!(\"copied {} bytes\", tokio::io::copy(\u0026mut r, \u0026mut w).await.unwrap());\n            });\n        }\n    });\n\n    let exit = exit_rx.await;\n\n    // the server thread will normally never exit, since more connections\n    // can always arrive. however, with a Valved, we can turn off the\n    // stream of incoming connections to initiate a graceful shutdown\n    drop(exit);\n}\n```\n\nYou can share the same [`Trigger`] between multiple streams by first creating a [`Valve`],\nand then wrapping multiple streams using [`Valve::Wrap`]:\n\n```rust\nuse stream_cancel::Valve;\nuse futures::prelude::*;\nuse tokio_stream::wrappers::TcpListenerStream;\n\n#[tokio::main]\nasync fn main() {\n    let (exit, valve) = Valve::new();\n    let listener1 = tokio::net::TcpListener::bind(\"127.0.0.1:0\").await.unwrap();\n    let listener2 = tokio::net::TcpListener::bind(\"127.0.0.1:0\").await.unwrap();\n\n    tokio::spawn(async move {\n        let incoming1 = valve.wrap(TcpListenerStream::new(listener1));\n        let incoming2 = valve.wrap(TcpListenerStream::new(listener2));\n\n        use futures_util::stream::select;\n        let mut incoming = select(incoming1, incoming2);\n        while let Some(mut s) = incoming.next().await.transpose().unwrap() {\n            tokio::spawn(async move {\n                let (mut r, mut w) = s.split();\n                println!(\"copied {} bytes\", tokio::io::copy(\u0026mut r, \u0026mut w).await.unwrap());\n            });\n        }\n    });\n\n    // the runtime will not become idle until both incoming1 and incoming2 have stopped\n    // (due to the select). this checks that they are indeed both interrupted when the\n    // valve is closed.\n    drop(exit);\n}\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjonhoo%2Fstream-cancel","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjonhoo%2Fstream-cancel","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjonhoo%2Fstream-cancel/lists"}