From b93d483078ba975973c5aaca931af25b06c6aab8 Mon Sep 17 00:00:00 2001 From: Xudong Huang Date: Sat, 29 Jun 2024 19:21:49 +0800 Subject: [PATCH] :pencil: refine err as cold code --- src/http_server.rs | 19 ++++++++++++------- src/request.rs | 6 ++++-- src/response.rs | 1 + 3 files changed, 17 insertions(+), 9 deletions(-) diff --git a/src/http_server.rs b/src/http_server.rs index 59bf0ce..08b35b5 100644 --- a/src/http_server.rs +++ b/src/http_server.rs @@ -73,6 +73,11 @@ pub trait HttpServiceFactory: Send + Sized + 'static { } } +#[cold] +pub(crate) fn err(e: io::Error) -> io::Result { + Err(e) +} + #[cfg(unix)] #[inline] fn nonblock_read(stream: &mut impl Read, req_buf: &mut BytesMut) -> io::Result { @@ -82,10 +87,10 @@ fn nonblock_read(stream: &mut impl Read, req_buf: &mut BytesMut) -> io::Result return Err(io::Error::new(io::ErrorKind::BrokenPipe, "read closed")), + Ok(0) => return err(io::Error::new(io::ErrorKind::BrokenPipe, "read closed")), Ok(n) => read_cnt += n, - Err(err) if err.kind() == io::ErrorKind::WouldBlock => break, - Err(err) => return Err(err), + Err(e) if e.kind() == io::ErrorKind::WouldBlock => break, + Err(e) => return err(e), } } @@ -105,10 +110,10 @@ fn nonblock_write(stream: &mut impl Write, rsp_buf: &mut BytesMut) -> io::Result let mut write_cnt = 0; while write_cnt < len { match stream.write(unsafe { write_buf.get_unchecked(write_cnt..) }) { - Ok(0) => return Err(io::Error::new(io::ErrorKind::BrokenPipe, "write closed")), + Ok(0) => return err(io::Error::new(io::ErrorKind::BrokenPipe, "write closed")), Ok(n) => write_cnt += n, - Err(err) if err.kind() == io::ErrorKind::WouldBlock => break, - Err(err) => return Err(err), + Err(e) if e.kind() == io::ErrorKind::WouldBlock => break, + Err(e) => return err(e), } } rsp_buf.advance(write_cnt); @@ -184,7 +189,7 @@ fn each_connection_loop(stream: &mut TcpStream, mut service: T) let read_cnt = stream.read(read_buf)?; if read_cnt == 0 { //connection was closed - return Err(io::Error::new(io::ErrorKind::BrokenPipe, "closed")); + return err(io::Error::new(io::ErrorKind::BrokenPipe, "closed")); } unsafe { req_buf.advance_mut(read_cnt) }; diff --git a/src/request.rs b/src/request.rs index 74569ef..3f1efe9 100644 --- a/src/request.rs +++ b/src/request.rs @@ -7,6 +7,8 @@ pub(crate) const MAX_HEADERS: usize = 16; use bytes::{Buf, BufMut, BytesMut}; use may::net::TcpStream; +use crate::http_server::err; + pub struct BodyReader<'buf, 'stream> { // remaining bytes for body req_buf: &'buf mut BytesMut, @@ -130,9 +132,9 @@ pub fn decode<'header, 'buf, 'stream>( let status = match req.parse_with_uninit_headers(buf, headers) { Ok(s) => s, Err(e) => { - eprintln!("failed to parse http request: {e:?}"); let msg = format!("failed to parse http request: {e:?}"); - return Err(io::Error::new(io::ErrorKind::Other, msg)); + eprintln!("{msg}"); + return err(io::Error::new(io::ErrorKind::Other, msg)); } }; diff --git a/src/response.rs b/src/response.rs index bc556b5..272fdbe 100644 --- a/src/response.rs +++ b/src/response.rs @@ -129,6 +129,7 @@ pub(crate) fn encode(mut rsp: Response, buf: &mut BytesMut) { buf.extend_from_slice(rsp.get_body()); } +#[cold] pub(crate) fn encode_error(e: io::Error, buf: &mut BytesMut) { error!("error in service: err = {:?}", e); let msg_string = e.to_string();