Skip to content

Commit

Permalink
Add Read and Write timeouts
Browse files Browse the repository at this point in the history
What
--
Add Read and Write timeouts

Reading and writing data from/to a tcp stream can now timeout is it
takes longer than the set timeout. Default is 60 seconds.

Other
--
* Improve server creation in tests
  • Loading branch information
mattgathu committed Dec 30, 2023
1 parent 6a27b6d commit 4fae20e
Show file tree
Hide file tree
Showing 4 changed files with 119 additions and 64 deletions.
98 changes: 49 additions & 49 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ pub enum SevaError {
UriTooLong,
#[error("Missing value for header: {0}")]
MissingHeaderValue(HeaderName),
#[error("timed out while reading data")]
ReadTimeOut,
#[error("timed out while writing data")]
WriteTimeOut,
}

#[derive(Error, Debug)]
Expand All @@ -53,10 +57,18 @@ impl fmt::Display for ParsingError {
pub trait IoErrorUtils {
fn kind(&self) -> io::ErrorKind;

fn is_addr_in_use(&self) -> bool {
self.kind() == io::ErrorKind::AddrInUse
}

fn is_blocking(&self) -> bool {
self.kind() == io::ErrorKind::WouldBlock
}

fn is_timed_out(&self) -> bool {
self.kind() == io::ErrorKind::TimedOut
}

fn is_not_found(&self) -> bool {
self.kind() == io::ErrorKind::NotFound
}
Expand Down
7 changes: 6 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ struct Args {
/// Log Level
#[arg(long, default_value = "INFO")]
log_level: Level,

/// Timeout duration in seconds
#[arg(long, default_value = "60")]
timeout: u64,
}

fn expand_tilde(path: &str) -> String {
Expand Down Expand Up @@ -81,7 +85,8 @@ fn main() -> errors::Result<()> {
port = args.port,
);

let mut server = server::HttpServer::new(&args.host, args.port, dir, false)?;
let mut server =
server::HttpServer::new(&args.host, args.port, dir, false, args.timeout)?;
server.run()?;
Ok(())
}
Loading

0 comments on commit 4fae20e

Please sign in to comment.