Skip to content

Commit

Permalink
📝 refine err as cold code
Browse files Browse the repository at this point in the history
  • Loading branch information
Xudong-Huang committed Jun 29, 2024
1 parent ce2d4bc commit b93d483
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 9 deletions.
19 changes: 12 additions & 7 deletions src/http_server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,11 @@ pub trait HttpServiceFactory: Send + Sized + 'static {
}
}

#[cold]
pub(crate) fn err<T>(e: io::Error) -> io::Result<T> {
Err(e)
}

#[cfg(unix)]
#[inline]
fn nonblock_read(stream: &mut impl Read, req_buf: &mut BytesMut) -> io::Result<usize> {
Expand All @@ -82,10 +87,10 @@ fn nonblock_read(stream: &mut impl Read, req_buf: &mut BytesMut) -> io::Result<u
let mut read_cnt = 0;
loop {
match stream.read(unsafe { read_buf.get_unchecked_mut(read_cnt..) }) {
Ok(0) => 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),
}
}

Expand All @@ -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);
Expand Down Expand Up @@ -184,7 +189,7 @@ fn each_connection_loop<T: HttpService>(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) };

Expand Down
6 changes: 4 additions & 2 deletions src/request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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));
}
};

Expand Down
1 change: 1 addition & 0 deletions src/response.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down

0 comments on commit b93d483

Please sign in to comment.