@@ -20,30 +20,21 @@ pub const MAX_ROOTLESS_PIPE_SIZE: usize = 1024 * 1024;
2020#[ cfg( any( target_os = "linux" , target_os = "android" ) ) ]
2121const KERNEL_DEFAULT_PIPE_SIZE : usize = 64 * 1024 ;
2222
23- /// A wrapper around [`rustix::pipe::pipe`] that ensures the pipe is cleaned up.
23+ /// return pipe larger than given size
24+ /// SIZE_REQUIRED should be true if you want to fail when changing pipe size failed
25+ /// e.g. writing size to pipe should not hang
2426///
25- /// Returns two `File` objects: everything written to the second can be read
26- /// from the first.
2727/// used for resolving the limitation for splice: one of a input or output should be pipe
2828#[ inline]
2929#[ cfg( any( target_os = "linux" , target_os = "android" ) ) ]
30- pub fn pipe ( ) -> std:: io:: Result < ( File , File ) > {
31- let ( read, write) = rustix:: pipe:: pipe ( ) ?;
32- // improve performance for splice
33- let _ = fcntl_setpipe_size ( & read, MAX_ROOTLESS_PIPE_SIZE ) ;
34-
35- Ok ( ( File :: from ( read) , File :: from ( write) ) )
36- }
37-
38- /// return pipe larger than given size and kernel's default size
39- ///
40- /// useful to save RAM usage
41- #[ inline]
42- #[ cfg( any( target_os = "linux" , target_os = "android" ) ) ]
43- fn pipe_with_size ( s : usize ) -> std:: io:: Result < ( File , File ) > {
30+ pub fn pipe < const SIZE_REQUIRED : bool > ( s : usize ) -> std:: io:: Result < ( File , File ) > {
4431 let ( read, write) = rustix:: pipe:: pipe ( ) ?;
32+ // guard unnecessary syscall
4533 if s > KERNEL_DEFAULT_PIPE_SIZE {
46- let _ = fcntl_setpipe_size ( & read, s) ;
34+ let r = fcntl_setpipe_size ( & read, s) ;
35+ if SIZE_REQUIRED {
36+ r?;
37+ }
4738 }
4839
4940 Ok ( ( File :: from ( read) , File :: from ( write) ) )
@@ -126,7 +117,10 @@ where
126117 S : AsFd ,
127118{
128119 static PIPE_CACHE : OnceLock < Option < ( File , File ) > > = OnceLock :: new ( ) ;
129- let Some ( ( pipe_rd, pipe_wr) ) = PIPE_CACHE . get_or_init ( || pipe ( ) . ok ( ) ) . as_ref ( ) else {
120+ let Some ( ( pipe_rd, pipe_wr) ) = PIPE_CACHE
121+ . get_or_init ( || pipe :: < false > ( MAX_ROOTLESS_PIPE_SIZE ) . ok ( ) )
122+ . as_ref ( )
123+ else {
130124 return Ok ( true ) ;
131125 } ;
132126 // improve throughput
@@ -197,7 +191,7 @@ pub fn send_n_bytes(
197191 }
198192 }
199193 } else if let Some ( ( broker_r, broker_w) ) = PIPE_CACHE
200- . get_or_init ( || pipe_with_size ( pipe_size) . ok ( ) )
194+ . get_or_init ( || pipe :: < false > ( pipe_size) . ok ( ) )
201195 . as_ref ( )
202196 {
203197 // todo: create fn splice_bounded_broker
0 commit comments