Use ReaderStream

This commit is contained in:
Edward Shen 2021-07-18 11:37:39 -04:00
parent afa2cf55fa
commit bd306455bc
Signed by: edward
GPG key ID: 19182661E818369F
3 changed files with 8 additions and 12 deletions

5
src/cache/fs.rs vendored
View file

@ -35,7 +35,7 @@ use tokio::io::{
ReadBuf,
};
use tokio::sync::mpsc::Sender;
use tokio_util::codec::{BytesCodec, FramedRead};
use tokio_util::io::ReaderStream;
use tracing::{debug, instrument, warn};
use super::compat::LegacyImageMetadata;
@ -122,8 +122,7 @@ pub(super) async fn read_file(
// successfully decoded the data; otherwise the file is garbage.
if let Some(reader) = reader {
let stream =
CacheStream::Completed(FramedRead::new(reader as Pin<Box<_>>, BytesCodec::new()));
let stream = CacheStream::Completed(ReaderStream::new(reader));
parsed_metadata.map(|metadata| Ok((stream, maybe_header, metadata)))
} else {
debug!("Reader was invalid, file is corrupt");

4
src/cache/mem.rs vendored
View file

@ -322,7 +322,7 @@ mod test_util {
use parking_lot::Mutex;
use tokio::io::BufReader;
use tokio::sync::mpsc::Sender;
use tokio_util::codec::{BytesCodec, FramedRead};
use tokio_util::io::ReaderStream;
#[derive(Default)]
pub struct TestDiskCache(
@ -347,7 +347,7 @@ mod test_util {
let reader = Box::pin(BufReader::new(tokio_util::io::StreamReader::new(
tokio_stream::once(Ok::<_, std::io::Error>(image)),
)));
let stream = CacheStream::Completed(FramedRead::new(reader, BytesCodec::new()));
let stream = CacheStream::Completed(ReaderStream::new(reader));
self.0.lock().get_mut().insert(key, Ok((stream, metadata)));
Ok(())
}

11
src/cache/mod.rs vendored
View file

@ -7,7 +7,7 @@ use std::task::{Context, Poll};
use actix_web::http::HeaderValue;
use async_trait::async_trait;
use bytes::{Bytes, BytesMut};
use bytes::Bytes;
use chacha20::Key;
use chrono::{DateTime, FixedOffset};
use futures::{Stream, StreamExt};
@ -17,7 +17,7 @@ use serde::{Deserialize, Serialize};
use serde_repr::{Deserialize_repr, Serialize_repr};
use thiserror::Error;
use tokio::sync::mpsc::Sender;
use tokio_util::codec::{BytesCodec, FramedRead};
use tokio_util::io::ReaderStream;
pub use disk::DiskCache;
pub use fs::UpstreamError;
@ -252,7 +252,7 @@ pub struct CacheEntry {
pub enum CacheStream {
Memory(MemStream),
Completed(FramedRead<Pin<Box<dyn MetadataFetch + Send + Sync>>, BytesCodec>),
Completed(ReaderStream<Pin<Box<dyn MetadataFetch + Send + Sync>>>),
}
impl From<CachedImage> for CacheStream {
@ -269,10 +269,7 @@ impl Stream for CacheStream {
fn poll_next(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
match self.get_mut() {
Self::Memory(stream) => stream.poll_next_unpin(cx),
Self::Completed(stream) => stream
.poll_next_unpin(cx)
.map_ok(BytesMut::freeze)
.map_err(|_| UpstreamError),
Self::Completed(stream) => stream.poll_next_unpin(cx).map_err(|_| UpstreamError),
}
}
}