Skip to content
Open
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
13 changes: 13 additions & 0 deletions tower/src/hedge/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,10 @@ pub struct SelectPolicy<P> {

impl<S, P> Hedge<S, P> {
/// Create a new hedge middleware.
///
/// # Panics
///
/// Panics if `period` is zero.
pub fn new<Request>(
service: S,
policy: P,
Expand All @@ -97,6 +101,10 @@ impl<S, P> Hedge<S, P> {
S::Error: Into<crate::BoxError>,
P: Policy<Request> + Clone,
{
assert!(
period > Duration::ZERO,
"histogram rotation period must be greater than zero"
);
let histo = Arc::new(Mutex::new(RotatingHistogram::new(period)));
Self::new_with_histo(service, policy, min_data_points, latency_percentile, histo)
}
Expand All @@ -116,6 +124,11 @@ impl<S, P> Hedge<S, P> {
S::Error: Into<crate::BoxError>,
P: Policy<Request> + Clone,
{
assert!(
period > Duration::ZERO,
"histogram rotation period must be greater than zero"
);

let histo = Arc::new(Mutex::new(RotatingHistogram::new(period)));
{
let mut locked = histo.lock().unwrap();
Expand Down
23 changes: 23 additions & 0 deletions tower/tests/hedge/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -182,3 +182,26 @@ fn new_service<P: Policy<Req> + Clone>(policy: P) -> (mock::Spawn<Hedge<Mock, P>

(mock::Spawn::new(service), handle)
}

#[test]
#[should_panic(expected = "histogram rotation period must be greater than zero")]
fn hedge_new_with_mock_latencies_zero_period_panics() {
let (service, _handle) = tower_test::mock::pair::<Req, Res>();
let mock_latencies: [u64; 2] = [10, 20];

let _service = Hedge::new_with_mock_latencies(
service,
TestPolicy,
10,
0.9,
Duration::ZERO,
&mock_latencies,
);
}
#[test]
#[should_panic(expected = "histogram rotation period must be greater than zero")]
fn hedge_new_zero_period_panics() {
let (service, _handle) = tower_test::mock::pair::<Req, Res>();

let _service = Hedge::new(service, TestPolicy, 10, 0.9, Duration::ZERO);
}