Skip to content

Commit 5f0bee4

Browse files
Support request --timeout in seconds
1 parent 7a1ea28 commit 5f0bee4

File tree

5 files changed

+70
-3
lines changed

5 files changed

+70
-3
lines changed

Cargo.lock

Lines changed: 3 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ reqwest = { version = "0.12", features = [
1717
"__rustls",
1818
"rustls-tls-native-roots",
1919
] }
20-
rabbitmq_http_client = { version = "0.60.0", features = [
20+
rabbitmq_http_client = { version = "0.61.0", features = [
2121
"blocking",
2222
"tabled",
2323
] }

src/cli.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -558,6 +558,16 @@ pub fn parser(pre_flight_settings: PreFlightSettings) -> Command {
558558
.help("Local path to a client private key file in the PEM format")
559559
.value_parser(value_parser!(PathBuf)),
560560
)
561+
// --timeout
562+
.arg(
563+
Arg::new("timeout")
564+
.long("timeout")
565+
.env("RABBITMQADMIN_TIMEOUT")
566+
.help("HTTP API request timeout in seconds. Must be greater than 0")
567+
.required(false)
568+
.default_value("60")
569+
.value_parser(value_parser!(u64).range(1..)),
570+
)
561571
// --quiet
562572
.arg(
563573
Arg::new("quiet")

src/main.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ use clap::{ArgMatches, crate_name, crate_version};
1919
use errors::CommandRunError;
2020
use reqwest::{Identity, tls::Version as TlsVersion};
2121
use std::path::{Path, PathBuf};
22+
use std::time::Duration;
2223
use std::{fs, process};
2324
use sysexits::ExitCode;
2425

@@ -124,11 +125,17 @@ fn configure_http_api_client<'a>(
124125
// Due to how SharedSettings are computed, these should safe to unwrap()
125126
let username = merged_settings.username.clone().unwrap();
126127
let password = merged_settings.password.clone().unwrap();
128+
129+
// Extract timeout from CLI arguments (default is 60 seconds)
130+
let timeout_secs = cli.get_one::<u64>("timeout").copied().unwrap_or(60);
131+
let timeout = Duration::from_secs(timeout_secs);
132+
127133
let client = build_rabbitmq_http_api_client(
128134
httpc,
129135
endpoint.to_owned(),
130136
username.clone(),
131137
password.clone(),
138+
timeout,
132139
);
133140
Ok(client)
134141
}
@@ -173,11 +180,13 @@ fn build_rabbitmq_http_api_client(
173180
endpoint: String,
174181
username: String,
175182
password: String,
183+
timeout: Duration,
176184
) -> APIClient {
177185
ClientBuilder::new()
178186
.with_endpoint(endpoint)
179187
.with_basic_auth_credentials(username, password)
180188
.with_client(httpc)
189+
.with_request_timeout(timeout)
181190
.build()
182191
}
183192

tests/timeout_tests.rs

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
// Copyright (C) 2023-2025 RabbitMQ Core Team (teamrabbitmq@gmail.com)
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
mod test_helpers;
16+
use std::error::Error;
17+
use test_helpers::{run_fails, run_succeeds};
18+
19+
#[test]
20+
fn timeout_flag_with_valid_value() -> Result<(), Box<dyn Error>> {
21+
run_succeeds(["--timeout", "30", "show", "overview"]);
22+
23+
Ok(())
24+
}
25+
26+
#[test]
27+
fn timeout_flag_with_zero_value_should_fail() -> Result<(), Box<dyn Error>> {
28+
run_fails(["--timeout", "0", "show", "overview"]);
29+
30+
Ok(())
31+
}
32+
33+
#[test]
34+
fn timeout_flag_with_negative_value_should_fail() -> Result<(), Box<dyn Error>> {
35+
run_fails(["--timeout", "-1", "show", "overview"]);
36+
37+
Ok(())
38+
}
39+
40+
#[test]
41+
fn timeout_uses_default_when_not_specified() -> Result<(), Box<dyn Error>> {
42+
// Should use the default timeout of 60 seconds but we have no
43+
// easy way of testing this. Welp.
44+
run_succeeds(["show", "overview"]);
45+
46+
Ok(())
47+
}

0 commit comments

Comments
 (0)