Use ReaderStream
This commit is contained in:
parent
afa2cf55fa
commit
bd306455bc
3 changed files with 8 additions and 12 deletions
5
src/cache/fs.rs
vendored
5
src/cache/fs.rs
vendored
|
@ -35,7 +35,7 @@ use tokio::io::{
|
||||||
ReadBuf,
|
ReadBuf,
|
||||||
};
|
};
|
||||||
use tokio::sync::mpsc::Sender;
|
use tokio::sync::mpsc::Sender;
|
||||||
use tokio_util::codec::{BytesCodec, FramedRead};
|
use tokio_util::io::ReaderStream;
|
||||||
use tracing::{debug, instrument, warn};
|
use tracing::{debug, instrument, warn};
|
||||||
|
|
||||||
use super::compat::LegacyImageMetadata;
|
use super::compat::LegacyImageMetadata;
|
||||||
|
@ -122,8 +122,7 @@ pub(super) async fn read_file(
|
||||||
// successfully decoded the data; otherwise the file is garbage.
|
// successfully decoded the data; otherwise the file is garbage.
|
||||||
|
|
||||||
if let Some(reader) = reader {
|
if let Some(reader) = reader {
|
||||||
let stream =
|
let stream = CacheStream::Completed(ReaderStream::new(reader));
|
||||||
CacheStream::Completed(FramedRead::new(reader as Pin<Box<_>>, BytesCodec::new()));
|
|
||||||
parsed_metadata.map(|metadata| Ok((stream, maybe_header, metadata)))
|
parsed_metadata.map(|metadata| Ok((stream, maybe_header, metadata)))
|
||||||
} else {
|
} else {
|
||||||
debug!("Reader was invalid, file is corrupt");
|
debug!("Reader was invalid, file is corrupt");
|
||||||
|
|
4
src/cache/mem.rs
vendored
4
src/cache/mem.rs
vendored
|
@ -322,7 +322,7 @@ mod test_util {
|
||||||
use parking_lot::Mutex;
|
use parking_lot::Mutex;
|
||||||
use tokio::io::BufReader;
|
use tokio::io::BufReader;
|
||||||
use tokio::sync::mpsc::Sender;
|
use tokio::sync::mpsc::Sender;
|
||||||
use tokio_util::codec::{BytesCodec, FramedRead};
|
use tokio_util::io::ReaderStream;
|
||||||
|
|
||||||
#[derive(Default)]
|
#[derive(Default)]
|
||||||
pub struct TestDiskCache(
|
pub struct TestDiskCache(
|
||||||
|
@ -347,7 +347,7 @@ mod test_util {
|
||||||
let reader = Box::pin(BufReader::new(tokio_util::io::StreamReader::new(
|
let reader = Box::pin(BufReader::new(tokio_util::io::StreamReader::new(
|
||||||
tokio_stream::once(Ok::<_, std::io::Error>(image)),
|
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)));
|
self.0.lock().get_mut().insert(key, Ok((stream, metadata)));
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
11
src/cache/mod.rs
vendored
11
src/cache/mod.rs
vendored
|
@ -7,7 +7,7 @@ use std::task::{Context, Poll};
|
||||||
|
|
||||||
use actix_web::http::HeaderValue;
|
use actix_web::http::HeaderValue;
|
||||||
use async_trait::async_trait;
|
use async_trait::async_trait;
|
||||||
use bytes::{Bytes, BytesMut};
|
use bytes::Bytes;
|
||||||
use chacha20::Key;
|
use chacha20::Key;
|
||||||
use chrono::{DateTime, FixedOffset};
|
use chrono::{DateTime, FixedOffset};
|
||||||
use futures::{Stream, StreamExt};
|
use futures::{Stream, StreamExt};
|
||||||
|
@ -17,7 +17,7 @@ use serde::{Deserialize, Serialize};
|
||||||
use serde_repr::{Deserialize_repr, Serialize_repr};
|
use serde_repr::{Deserialize_repr, Serialize_repr};
|
||||||
use thiserror::Error;
|
use thiserror::Error;
|
||||||
use tokio::sync::mpsc::Sender;
|
use tokio::sync::mpsc::Sender;
|
||||||
use tokio_util::codec::{BytesCodec, FramedRead};
|
use tokio_util::io::ReaderStream;
|
||||||
|
|
||||||
pub use disk::DiskCache;
|
pub use disk::DiskCache;
|
||||||
pub use fs::UpstreamError;
|
pub use fs::UpstreamError;
|
||||||
|
@ -252,7 +252,7 @@ pub struct CacheEntry {
|
||||||
|
|
||||||
pub enum CacheStream {
|
pub enum CacheStream {
|
||||||
Memory(MemStream),
|
Memory(MemStream),
|
||||||
Completed(FramedRead<Pin<Box<dyn MetadataFetch + Send + Sync>>, BytesCodec>),
|
Completed(ReaderStream<Pin<Box<dyn MetadataFetch + Send + Sync>>>),
|
||||||
}
|
}
|
||||||
|
|
||||||
impl From<CachedImage> for CacheStream {
|
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>> {
|
fn poll_next(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
|
||||||
match self.get_mut() {
|
match self.get_mut() {
|
||||||
Self::Memory(stream) => stream.poll_next_unpin(cx),
|
Self::Memory(stream) => stream.poll_next_unpin(cx),
|
||||||
Self::Completed(stream) => stream
|
Self::Completed(stream) => stream.poll_next_unpin(cx).map_err(|_| UpstreamError),
|
||||||
.poll_next_unpin(cx)
|
|
||||||
.map_ok(BytesMut::freeze)
|
|
||||||
.map_err(|_| UpstreamError),
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue