Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add timeout #19

Merged
merged 3 commits into from
Oct 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 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
Expand Up @@ -57,7 +57,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 @@ -37,6 +37,7 @@ use {
compression::CompressionLayer,
cors::{Any, CorsLayer},
set_header::SetResponseHeaderLayer,
timeout::TimeoutLayer,
},
};

Expand Down Expand Up @@ -132,6 +133,11 @@ pub(crate) struct Server {
https: bool,
#[clap(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 @@ -214,6 +220,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 @@ -1112,6 +1119,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 @@ -1770,6 +1781,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
Loading