Skip to content

Commit

Permalink
Add timeout (#19)
Browse files Browse the repository at this point in the history
* Add timeout

* use tower_http timeout

* fix lint

---------

Co-authored-by: Michael Yu <michael@magiceden.io>
  • Loading branch information
nothing0012 and mi-yu committed Oct 12, 2023
1 parent 199c17c commit 16d672f
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 2 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ tempfile = "3.2.0"
tokio = { version = "1.17.0", features = ["rt-multi-thread"] }
tokio-stream = "0.1.9"
tokio-util = {version = "0.7.3", features = ["compat"] }
tower-http = { version = "0.4.0", features = ["compression-br", "compression-gzip", "cors", "set-header"] }
tower-http = { version = "0.4.0", features = ["compression-br", "compression-gzip", "cors", "set-header", "timeout"] }

rdkafka = { version = "0.33.2" }
axum-jrpc = { version = "0.5.1", features = ["serde_json", "anyhow_error"] }
Expand Down
2 changes: 1 addition & 1 deletion src/block_rarity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ impl<'de> Deserialize<'de> for BlockRarity {

pub(crate) fn is_palindrome(n: &u64) -> bool {
let s = n.to_string();
if s.chars().nth(0) != s.chars().last() {
if s.chars().next() != s.chars().last() {
return false;
}
let reversed = s.chars().rev().collect::<String>();
Expand Down
20 changes: 20 additions & 0 deletions src/subcommand/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ use {
compression::CompressionLayer,
cors::{Any, CorsLayer},
set_header::SetResponseHeaderLayer,
timeout::TimeoutLayer,
},
};

Expand Down Expand Up @@ -133,6 +134,11 @@ pub(crate) struct Server {
https: bool,
#[arg(long, help = "Redirect HTTP traffic to HTTPS.")]
redirect_http_to_https: bool,
#[clap(
long,
help = "Timeout requests after <SECONDS> seconds. Default: 30 seconds."
)]
timeout: Option<u64>,
}

impl Server {
Expand Down Expand Up @@ -223,6 +229,7 @@ impl Server {
.allow_origin(Any),
)
.layer(CompressionLayer::new())
.layer(TimeoutLayer::new(Duration::from_secs(self.timeout.unwrap_or(30))))
.with_state(server_config);

match (self.http_port(), self.https_port()) {
Expand Down Expand Up @@ -1177,6 +1184,10 @@ mod tests {
Self::new_with_args(&["--index-sats"], &[])
}

fn new_with_timeout() -> Self {
Self::new_with_args(&[], &["--timeout", "1"])
}

fn new_with_args(ord_args: &[&str], server_args: &[&str]) -> Self {
Self::new_server(test_bitcoincore_rpc::spawn(), None, ord_args, server_args)
}
Expand Down Expand Up @@ -1864,6 +1875,15 @@ mod tests {
);
}

#[test]
fn output_with_timeout() {
TestServer::new_with_timeout().assert_response_regex(
"/block/0",
StatusCode::OK,
".*<title>Block 0</title>.*<h1>Block 0</h1>.*",
);
}

#[test]
fn output_without_sat_index() {
let txid = "4a5e1e4baab89f3a32518a88c31bc87f618f76673e2cc77ab2127b7afdeda33b";
Expand Down

0 comments on commit 16d672f

Please sign in to comment.