Skip to content

Commit

Permalink
Add Read and Write timeouts (#2)
Browse files Browse the repository at this point in the history
* Add Read and Write timeouts

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 authored Dec 30, 2023
1 parent 6a27b6d commit 599ab97
Show file tree
Hide file tree
Showing 5 changed files with 119 additions and 66 deletions.
100 changes: 50 additions & 50 deletions Cargo.lock

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

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "seva"
version = "0.1.1"
version = "0.1.2"
edition = "2021"
authors = ["Matt Gathu <[email protected]>"]
description = "Simple directory http server inspired by Python's http.server"
Expand Down
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 599ab97

Please sign in to comment.