Skip to content

Commit aa60f41

Browse files
authored
allow configurable in-memory buffer size for SeekableStream (#1359)
SeekableStream makes use of an in-memory buffer during reads. Right now, this is currently set to 64k. This change allows the buffer size to be configurable.
1 parent f704e9a commit aa60f41

File tree

1 file changed

+13
-11
lines changed

1 file changed

+13
-11
lines changed

sdk/core/src/seekable_stream.rs

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,35 @@
11
use bytes::Bytes;
2-
use futures::io::AsyncRead;
3-
use futures::stream::Stream;
4-
use futures::task::Poll;
2+
use dyn_clone::DynClone;
3+
use futures::{io::AsyncRead, stream::Stream, task::Poll};
4+
use std::{pin::Pin, task::Context};
5+
6+
/// Amount of the stream to buffer in memory during streaming uploads
7+
pub(crate) const DEFAULT_BUFFER_SIZE: usize = 1024 * 64;
58

69
/// Enable a type implementing `AsyncRead` to be consumed as if it were
710
/// a `Stream` of `Bytes`.
811
#[cfg_attr(target_arch = "wasm32", async_trait::async_trait(?Send))]
912
#[cfg_attr(not(target_arch = "wasm32"), async_trait::async_trait)]
10-
pub trait SeekableStream:
11-
AsyncRead + Unpin + std::fmt::Debug + Send + Sync + dyn_clone::DynClone
12-
{
13+
pub trait SeekableStream: AsyncRead + Unpin + std::fmt::Debug + Send + Sync + DynClone {
1314
async fn reset(&mut self) -> crate::error::Result<()>;
1415
fn len(&self) -> usize;
1516

1617
fn is_empty(&self) -> bool {
1718
self.len() == 0
1819
}
20+
21+
fn buffer_size(&self) -> usize {
22+
DEFAULT_BUFFER_SIZE
23+
}
1924
}
2025

2126
dyn_clone::clone_trait_object!(SeekableStream);
2227

2328
impl Stream for dyn SeekableStream {
2429
type Item = crate::error::Result<Bytes>;
2530

26-
fn poll_next(
27-
self: std::pin::Pin<&mut Self>,
28-
cx: &mut std::task::Context<'_>,
29-
) -> std::task::Poll<Option<Self::Item>> {
30-
let mut buffer = vec![0_u8; 1024 * 64];
31+
fn poll_next(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
32+
let mut buffer = vec![0_u8; self.buffer_size()];
3133

3234
match self.poll_read(cx, &mut buffer) {
3335
Poll::Ready(Ok(0)) => Poll::Ready(None),

0 commit comments

Comments
 (0)