@@ -351,7 +351,8 @@ const BUF_WRITE_SIZE: usize = 1460;
351351/// A concrete type for transport layer connection + extra fields for logging
352352#[ derive( Debug ) ]
353353pub struct Stream {
354- stream : BufStream < RawStreamWrapper > ,
354+ // Use `Option` to be able to swap to adjust the buffer size. Always safe to unwrap
355+ stream : Option < BufStream < RawStreamWrapper > > ,
355356 // the data put back at the front of the read buffer, in order to replay the read
356357 rewind_read_buf : Vec < u8 > ,
357358 buffer_write : bool ,
@@ -368,9 +369,17 @@ pub struct Stream {
368369}
369370
370371impl Stream {
372+ fn stream ( & self ) -> & BufStream < RawStreamWrapper > {
373+ self . stream . as_ref ( ) . expect ( "stream should always be set" )
374+ }
375+
376+ fn stream_mut ( & mut self ) -> & mut BufStream < RawStreamWrapper > {
377+ self . stream . as_mut ( ) . expect ( "stream should always be set" )
378+ }
379+
371380 /// set TCP nodelay for this connection if `self` is TCP
372381 pub fn set_nodelay ( & mut self ) -> Result < ( ) > {
373- if let RawStream :: Tcp ( s) = & self . stream . get_mut ( ) . stream {
382+ if let RawStream :: Tcp ( s) = & self . stream_mut ( ) . get_mut ( ) . stream {
374383 s. set_nodelay ( true )
375384 . or_err ( ConnectError , "failed to set_nodelay" ) ?;
376385 }
@@ -379,7 +388,7 @@ impl Stream {
379388
380389 /// set TCP keepalive settings for this connection if `self` is TCP
381390 pub fn set_keepalive ( & mut self , ka : & TcpKeepalive ) -> Result < ( ) > {
382- if let RawStream :: Tcp ( s) = & self . stream . get_mut ( ) . stream {
391+ if let RawStream :: Tcp ( s) = & self . stream_mut ( ) . get_mut ( ) . stream {
383392 debug ! ( "Setting tcp keepalive" ) ;
384393 set_tcp_keepalive ( s, ka) ?;
385394 }
@@ -390,12 +399,12 @@ impl Stream {
390399 pub fn set_rx_timestamp ( & mut self ) -> Result < ( ) > {
391400 use nix:: sys:: socket:: { setsockopt, sockopt, TimestampingFlag } ;
392401
393- if let RawStream :: Tcp ( s) = & self . stream . get_mut ( ) . stream {
402+ if let RawStream :: Tcp ( s) = & self . stream_mut ( ) . get_mut ( ) . stream {
394403 let timestamp_options = TimestampingFlag :: SOF_TIMESTAMPING_RX_SOFTWARE
395404 | TimestampingFlag :: SOF_TIMESTAMPING_SOFTWARE ;
396405 setsockopt ( s. as_raw_fd ( ) , sockopt:: Timestamping , & timestamp_options)
397406 . or_err ( InternalError , "failed to set SOF_TIMESTAMPING_RX_SOFTWARE" ) ?;
398- self . stream . get_mut ( ) . enable_rx_ts ( true ) ;
407+ self . stream_mut ( ) . get_mut ( ) . enable_rx_ts ( true ) ;
399408 }
400409
401410 Ok ( ( ) )
@@ -412,16 +421,28 @@ impl Stream {
412421 self . rewind_read_buf . extend_from_slice ( data) ;
413422 }
414423 }
424+
425+ /// Set the buffer of BufStream
426+ /// It is only set later because of the malloc overhead in critical accept() path
427+ pub ( crate ) fn set_buffer ( & mut self ) {
428+ use std:: mem;
429+ // Since BufStream doesn't provide an API to adjust the buf directly,
430+ // we take the raw stream out of it and put it in a new BufStream with the size we want
431+ let stream = mem:: take ( & mut self . stream ) ;
432+ let stream =
433+ stream. map ( |s| BufStream :: with_capacity ( BUF_READ_SIZE , BUF_WRITE_SIZE , s. into_inner ( ) ) ) ;
434+ let _ = mem:: replace ( & mut self . stream , stream) ;
435+ }
415436}
416437
417438impl From < TcpStream > for Stream {
418439 fn from ( s : TcpStream ) -> Self {
419440 Stream {
420- stream : BufStream :: with_capacity (
421- BUF_READ_SIZE ,
422- BUF_WRITE_SIZE ,
441+ stream : Some ( BufStream :: with_capacity (
442+ 0 ,
443+ 0 ,
423444 RawStreamWrapper :: new ( RawStream :: Tcp ( s) ) ,
424- ) ,
445+ ) ) ,
425446 rewind_read_buf : Vec :: new ( ) ,
426447 buffer_write : true ,
427448 established_ts : SystemTime :: now ( ) ,
@@ -439,11 +460,11 @@ impl From<TcpStream> for Stream {
439460impl From < UnixStream > for Stream {
440461 fn from ( s : UnixStream ) -> Self {
441462 Stream {
442- stream : BufStream :: with_capacity (
443- BUF_READ_SIZE ,
444- BUF_WRITE_SIZE ,
463+ stream : Some ( BufStream :: with_capacity (
464+ 0 ,
465+ 0 ,
445466 RawStreamWrapper :: new ( RawStream :: Unix ( s) ) ,
446- ) ,
467+ ) ) ,
447468 rewind_read_buf : Vec :: new ( ) ,
448469 buffer_write : true ,
449470 established_ts : SystemTime :: now ( ) ,
@@ -460,14 +481,14 @@ impl From<UnixStream> for Stream {
460481#[ cfg( unix) ]
461482impl AsRawFd for Stream {
462483 fn as_raw_fd ( & self ) -> std:: os:: unix:: io:: RawFd {
463- self . stream . get_ref ( ) . as_raw_fd ( )
484+ self . stream ( ) . get_ref ( ) . as_raw_fd ( )
464485 }
465486}
466487
467488#[ cfg( windows) ]
468489impl AsRawSocket for Stream {
469490 fn as_raw_socket ( & self ) -> std:: os:: windows:: io:: RawSocket {
470- self . stream . get_ref ( ) . as_raw_socket ( )
491+ self . stream ( ) . get_ref ( ) . as_raw_socket ( )
471492 }
472493}
473494
@@ -551,7 +572,7 @@ impl Drop for Stream {
551572 t. 0 . on_disconnected ( ) ;
552573 }
553574 /* use nodelay/local_addr function to detect socket status */
554- let ret = match & self . stream . get_ref ( ) . stream {
575+ let ret = match & self . stream ( ) . get_ref ( ) . stream {
555576 RawStream :: Tcp ( s) => s. nodelay ( ) . err ( ) ,
556577 #[ cfg( unix) ]
557578 RawStream :: Unix ( s) => s. local_addr ( ) . err ( ) ,
@@ -594,10 +615,10 @@ impl AsyncRead for Stream {
594615 let _ = std:: mem:: replace ( & mut self . rewind_read_buf , remaining_buf) ;
595616 result
596617 } else {
597- Pin :: new ( & mut self . stream ) . poll_read ( cx, buf)
618+ Pin :: new ( & mut self . stream_mut ( ) ) . poll_read ( cx, buf)
598619 } ;
599620 self . read_pending_time . poll_time ( & result) ;
600- self . rx_ts = self . stream . get_ref ( ) . rx_ts ;
621+ self . rx_ts = self . stream ( ) . get_ref ( ) . rx_ts ;
601622 result
602623 }
603624}
@@ -609,22 +630,22 @@ impl AsyncWrite for Stream {
609630 buf : & [ u8 ] ,
610631 ) -> Poll < io:: Result < usize > > {
611632 let result = if self . buffer_write {
612- Pin :: new ( & mut self . stream ) . poll_write ( cx, buf)
633+ Pin :: new ( & mut self . stream_mut ( ) ) . poll_write ( cx, buf)
613634 } else {
614- Pin :: new ( & mut self . stream . get_mut ( ) ) . poll_write ( cx, buf)
635+ Pin :: new ( & mut self . stream_mut ( ) . get_mut ( ) ) . poll_write ( cx, buf)
615636 } ;
616637 self . write_pending_time . poll_write_time ( & result, buf. len ( ) ) ;
617638 result
618639 }
619640
620641 fn poll_flush ( mut self : Pin < & mut Self > , cx : & mut Context ) -> Poll < io:: Result < ( ) > > {
621- let result = Pin :: new ( & mut self . stream ) . poll_flush ( cx) ;
642+ let result = Pin :: new ( & mut self . stream_mut ( ) ) . poll_flush ( cx) ;
622643 self . write_pending_time . poll_time ( & result) ;
623644 result
624645 }
625646
626647 fn poll_shutdown ( mut self : Pin < & mut Self > , cx : & mut Context ) -> Poll < io:: Result < ( ) > > {
627- Pin :: new ( & mut self . stream ) . poll_shutdown ( cx)
648+ Pin :: new ( & mut self . stream_mut ( ) ) . poll_shutdown ( cx)
628649 }
629650
630651 fn poll_write_vectored (
@@ -635,9 +656,9 @@ impl AsyncWrite for Stream {
635656 let total_size = bufs. iter ( ) . fold ( 0 , |acc, s| acc + s. len ( ) ) ;
636657
637658 let result = if self . buffer_write {
638- Pin :: new ( & mut self . stream ) . poll_write_vectored ( cx, bufs)
659+ Pin :: new ( & mut self . stream_mut ( ) ) . poll_write_vectored ( cx, bufs)
639660 } else {
640- Pin :: new ( & mut self . stream . get_mut ( ) ) . poll_write_vectored ( cx, bufs)
661+ Pin :: new ( & mut self . stream_mut ( ) . get_mut ( ) ) . poll_write_vectored ( cx, bufs)
641662 } ;
642663
643664 self . write_pending_time . poll_write_time ( & result, total_size) ;
@@ -646,9 +667,9 @@ impl AsyncWrite for Stream {
646667
647668 fn is_write_vectored ( & self ) -> bool {
648669 if self . buffer_write {
649- self . stream . is_write_vectored ( ) // it is true
670+ self . stream ( ) . is_write_vectored ( ) // it is true
650671 } else {
651- self . stream . get_ref ( ) . is_write_vectored ( )
672+ self . stream ( ) . get_ref ( ) . is_write_vectored ( )
652673 }
653674 }
654675}
0 commit comments