Skip to content

Commit f661467

Browse files
Add fn to add data from a stream (#1808)
Fixes #1759, see #1759 (comment) ## Description Implements a public API to add from a stream. Also changes add_reader to use the add_stream internally ## Notes & open questions Do the same for docs? ## Change checklist - [ ] Self-review. - [ ] Documentation updates if relevant. - [ ] Tests if relevant. Co-authored-by: Friedel Ziegelmayer <[email protected]>
1 parent 199a677 commit f661467

File tree

1 file changed

+10
-3
lines changed

1 file changed

+10
-3
lines changed

iroh/src/client.rs

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -287,17 +287,24 @@ where
287287
tag: SetTagOption,
288288
) -> anyhow::Result<BlobAddProgress> {
289289
const CAP: usize = 1024 * 64; // send 64KB per request by default
290-
let (mut sink, progress) = self.rpc.bidi(BlobAddStreamRequest { tag }).await?;
291-
292290
let input = ReaderStream::with_capacity(reader, CAP);
291+
self.add_stream(input, tag).await
292+
}
293+
294+
/// Write a blob by passing a stream of bytes.
295+
pub async fn add_stream(
296+
&self,
297+
input: impl Stream<Item = io::Result<Bytes>> + Send + Unpin + 'static,
298+
tag: SetTagOption,
299+
) -> anyhow::Result<BlobAddProgress> {
300+
let (mut sink, progress) = self.rpc.bidi(BlobAddStreamRequest { tag }).await?;
293301
let mut input = input.map(|chunk| match chunk {
294302
Ok(chunk) => Ok(BlobAddStreamUpdate::Chunk(chunk)),
295303
Err(err) => {
296304
warn!("Abort send, reason: failed to read from source stream: {err:?}");
297305
Ok(BlobAddStreamUpdate::Abort)
298306
}
299307
});
300-
301308
tokio::spawn(async move {
302309
// TODO: Is it important to catch this error? It should also result in an error on the
303310
// response stream. If we deem it important, we could one-shot send it into the

0 commit comments

Comments
 (0)