@@ -24,6 +24,7 @@ pin_project! {
2424 state: ChunkedReadFileState <Fut >,
2525 counter: u64 ,
2626 callback: F ,
27+ read_sync: bool ,
2728 }
2829}
2930
@@ -57,6 +58,7 @@ pub(crate) fn new_chunked_read(
5758 size : u64 ,
5859 offset : u64 ,
5960 file : File ,
61+ size_threshold : u64 ,
6062) -> impl Stream < Item = Result < Bytes , Error > > {
6163 ChunkedReadFile {
6264 size,
@@ -69,31 +71,45 @@ pub(crate) fn new_chunked_read(
6971 } ,
7072 counter : 0 ,
7173 callback : chunked_read_file_callback,
74+ read_sync : size < size_threshold,
7275 }
7376}
7477
7578#[ cfg( not( feature = "experimental-io-uring" ) ) ]
76- async fn chunked_read_file_callback (
79+ fn chunked_read_file_callback_sync (
7780 mut file : File ,
7881 offset : u64 ,
7982 max_bytes : usize ,
80- ) -> Result < ( File , Bytes ) , Error > {
83+ ) -> Result < ( File , Bytes ) , io :: Error > {
8184 use io:: { Read as _, Seek as _} ;
8285
83- let res = actix_web:: web:: block ( move || {
84- let mut buf = Vec :: with_capacity ( max_bytes) ;
86+ let mut buf = Vec :: with_capacity ( max_bytes) ;
8587
86- file. seek ( io:: SeekFrom :: Start ( offset) ) ?;
88+ file. seek ( io:: SeekFrom :: Start ( offset) ) ?;
8789
88- let n_bytes = file. by_ref ( ) . take ( max_bytes as u64 ) . read_to_end ( & mut buf) ?;
90+ let n_bytes = file. by_ref ( ) . take ( max_bytes as u64 ) . read_to_end ( & mut buf) ?;
8991
90- if n_bytes == 0 {
91- Err ( io:: Error :: from ( io:: ErrorKind :: UnexpectedEof ) )
92- } else {
93- Ok ( ( file, Bytes :: from ( buf) ) )
94- }
95- } )
96- . await ??;
92+ if n_bytes == 0 {
93+ Err ( io:: Error :: from ( io:: ErrorKind :: UnexpectedEof ) )
94+ } else {
95+ Ok ( ( file, Bytes :: from ( buf) ) )
96+ }
97+ }
98+
99+ #[ cfg( not( feature = "experimental-io-uring" ) ) ]
100+ #[ inline]
101+ async fn chunked_read_file_callback (
102+ file : File ,
103+ offset : u64 ,
104+ max_bytes : usize ,
105+ read_sync : bool ,
106+ ) -> Result < ( File , Bytes ) , Error > {
107+ let res = if read_sync {
108+ chunked_read_file_callback_sync ( file, offset, max_bytes) ?
109+ } else {
110+ actix_web:: web:: block ( move || chunked_read_file_callback_sync ( file, offset, max_bytes) )
111+ . await ??
112+ } ;
97113
98114 Ok ( res)
99115}
@@ -171,7 +187,7 @@ where
171187#[ cfg( not( feature = "experimental-io-uring" ) ) ]
172188impl < F , Fut > Stream for ChunkedReadFile < F , Fut >
173189where
174- F : Fn ( File , u64 , usize ) -> Fut ,
190+ F : Fn ( File , u64 , usize , bool ) -> Fut ,
175191 Fut : Future < Output = Result < ( File , Bytes ) , Error > > ,
176192{
177193 type Item = Result < Bytes , Error > ;
@@ -193,7 +209,7 @@ where
193209 . take ( )
194210 . expect ( "ChunkedReadFile polled after completion" ) ;
195211
196- let fut = ( this. callback ) ( file, offset, max_bytes) ;
212+ let fut = ( this. callback ) ( file, offset, max_bytes, * this . read_sync ) ;
197213
198214 this. state
199215 . project_replace ( ChunkedReadFileState :: Future { fut } ) ;
0 commit comments