Rate limits warning (#4075)
Only show the highest warning rate. Change the warning threshold
This commit is contained in:
@@ -118,7 +118,7 @@ struct RunningCommand {
|
|||||||
parsed_cmd: Vec<ParsedCommand>,
|
parsed_cmd: Vec<ParsedCommand>,
|
||||||
}
|
}
|
||||||
|
|
||||||
const RATE_LIMIT_WARNING_THRESHOLDS: [f64; 3] = [50.0, 75.0, 90.0];
|
const RATE_LIMIT_WARNING_THRESHOLDS: [f64; 3] = [75.0, 90.0, 95.0];
|
||||||
|
|
||||||
#[derive(Default)]
|
#[derive(Default)]
|
||||||
struct RateLimitWarningState {
|
struct RateLimitWarningState {
|
||||||
@@ -134,24 +134,30 @@ impl RateLimitWarningState {
|
|||||||
) -> Vec<String> {
|
) -> Vec<String> {
|
||||||
let mut warnings = Vec::new();
|
let mut warnings = Vec::new();
|
||||||
|
|
||||||
|
let mut highest_secondary: Option<f64> = None;
|
||||||
while self.secondary_index < RATE_LIMIT_WARNING_THRESHOLDS.len()
|
while self.secondary_index < RATE_LIMIT_WARNING_THRESHOLDS.len()
|
||||||
&& secondary_used_percent >= RATE_LIMIT_WARNING_THRESHOLDS[self.secondary_index]
|
&& secondary_used_percent >= RATE_LIMIT_WARNING_THRESHOLDS[self.secondary_index]
|
||||||
{
|
{
|
||||||
let threshold = RATE_LIMIT_WARNING_THRESHOLDS[self.secondary_index];
|
highest_secondary = Some(RATE_LIMIT_WARNING_THRESHOLDS[self.secondary_index]);
|
||||||
|
self.secondary_index += 1;
|
||||||
|
}
|
||||||
|
if let Some(threshold) = highest_secondary {
|
||||||
warnings.push(format!(
|
warnings.push(format!(
|
||||||
"Heads up, you've used over {threshold:.0}% of your weekly limit. Run /status for a breakdown."
|
"Heads up, you've used over {threshold:.0}% of your weekly limit. Run /status for a breakdown."
|
||||||
));
|
));
|
||||||
self.secondary_index += 1;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
let mut highest_primary: Option<f64> = None;
|
||||||
while self.primary_index < RATE_LIMIT_WARNING_THRESHOLDS.len()
|
while self.primary_index < RATE_LIMIT_WARNING_THRESHOLDS.len()
|
||||||
&& primary_used_percent >= RATE_LIMIT_WARNING_THRESHOLDS[self.primary_index]
|
&& primary_used_percent >= RATE_LIMIT_WARNING_THRESHOLDS[self.primary_index]
|
||||||
{
|
{
|
||||||
let threshold = RATE_LIMIT_WARNING_THRESHOLDS[self.primary_index];
|
highest_primary = Some(RATE_LIMIT_WARNING_THRESHOLDS[self.primary_index]);
|
||||||
|
self.primary_index += 1;
|
||||||
|
}
|
||||||
|
if let Some(threshold) = highest_primary {
|
||||||
warnings.push(format!(
|
warnings.push(format!(
|
||||||
"Heads up, you've used over {threshold:.0}% of your 5h limit. Run /status for a breakdown."
|
"Heads up, you've used over {threshold:.0}% of your 5h limit. Run /status for a breakdown."
|
||||||
));
|
));
|
||||||
self.primary_index += 1;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
warnings
|
warnings
|
||||||
|
|||||||
@@ -392,33 +392,22 @@ fn rate_limit_warnings_emit_thresholds() {
|
|||||||
warnings.extend(state.take_warnings(95.0, 10.0));
|
warnings.extend(state.take_warnings(95.0, 10.0));
|
||||||
|
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
warnings.len(),
|
warnings,
|
||||||
6,
|
vec![
|
||||||
"expected one warning per threshold per limit"
|
String::from(
|
||||||
);
|
"Heads up, you've used over 75% of your 5h limit. Run /status for a breakdown."
|
||||||
assert!(
|
),
|
||||||
warnings
|
String::from(
|
||||||
.iter()
|
"Heads up, you've used over 75% of your weekly limit. Run /status for a breakdown.",
|
||||||
.any(|w| w.contains("Heads up, you've used over 50% of your 5h limit.")),
|
),
|
||||||
"expected hourly 50% warning (new copy)"
|
String::from(
|
||||||
);
|
"Heads up, you've used over 95% of your 5h limit. Run /status for a breakdown."
|
||||||
assert!(
|
),
|
||||||
warnings
|
String::from(
|
||||||
.iter()
|
"Heads up, you've used over 95% of your weekly limit. Run /status for a breakdown.",
|
||||||
.any(|w| w.contains("Heads up, you've used over 50% of your weekly limit.")),
|
),
|
||||||
"expected weekly 50% warning (new copy)"
|
],
|
||||||
);
|
"expected one warning per limit for the highest crossed threshold"
|
||||||
assert!(
|
|
||||||
warnings
|
|
||||||
.iter()
|
|
||||||
.any(|w| w.contains("Heads up, you've used over 90% of your 5h limit.")),
|
|
||||||
"expected hourly 90% warning (new copy)"
|
|
||||||
);
|
|
||||||
assert!(
|
|
||||||
warnings
|
|
||||||
.iter()
|
|
||||||
.any(|w| w.contains("Heads up, you've used over 90% of your weekly limit.")),
|
|
||||||
"expected weekly 90% warning (new copy)"
|
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user