mirror of
https://github.com/fawney19/Aether.git
synced 2026-09-02 09:20:22 +08:00
fix(gateway): raise group rate limits by tier
This commit is contained in:
@@ -2000,7 +2000,7 @@ fn resolve_effective_rate_limit_policy(
|
|||||||
groups: &[aether_data::repository::users::StoredUserGroup],
|
groups: &[aether_data::repository::users::StoredUserGroup],
|
||||||
) -> Option<i32> {
|
) -> Option<i32> {
|
||||||
let group_policy = groups.iter().fold(None, |effective, group| {
|
let group_policy = groups.iter().fold(None, |effective, group| {
|
||||||
intersect_rate_limit_policies(
|
union_rate_limit_policies(
|
||||||
effective,
|
effective,
|
||||||
rate_limit_restriction_from_mode(&group.rate_limit_mode, group.rate_limit),
|
rate_limit_restriction_from_mode(&group.rate_limit_mode, group.rate_limit),
|
||||||
)
|
)
|
||||||
@@ -2073,6 +2073,22 @@ fn intersect_rate_limit_policies(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn union_rate_limit_policies(
|
||||||
|
left: Option<RateLimitRestriction>,
|
||||||
|
right: Option<RateLimitRestriction>,
|
||||||
|
) -> Option<RateLimitRestriction> {
|
||||||
|
match (left, right) {
|
||||||
|
(None, None) => None,
|
||||||
|
(Some(value), None) | (None, Some(value)) => Some(value),
|
||||||
|
(Some(RateLimitRestriction::Unlimited), _) | (_, Some(RateLimitRestriction::Unlimited)) => {
|
||||||
|
Some(RateLimitRestriction::Unlimited)
|
||||||
|
}
|
||||||
|
(Some(RateLimitRestriction::Limited(left)), Some(RateLimitRestriction::Limited(right))) => {
|
||||||
|
Some(RateLimitRestriction::Limited(left.max(right)))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
fn rate_limit_policy_value(policy: Option<RateLimitRestriction>) -> Option<i32> {
|
fn rate_limit_policy_value(policy: Option<RateLimitRestriction>) -> Option<i32> {
|
||||||
match policy {
|
match policy {
|
||||||
None => None,
|
None => None,
|
||||||
@@ -2358,35 +2374,72 @@ mod tests {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn rate_limit_policy_uses_most_restrictive_custom_limit() {
|
fn rate_limit_policy_uses_highest_group_limit_before_user_restriction() {
|
||||||
let groups = vec![sample_group(
|
let groups = vec![
|
||||||
"restricted",
|
sample_group(
|
||||||
10,
|
"default",
|
||||||
None,
|
10,
|
||||||
"unrestricted",
|
None,
|
||||||
Some(60),
|
"unrestricted",
|
||||||
"custom",
|
Some(30),
|
||||||
)];
|
"custom",
|
||||||
|
),
|
||||||
|
sample_group(
|
||||||
|
"tier-1",
|
||||||
|
20,
|
||||||
|
None,
|
||||||
|
"unrestricted",
|
||||||
|
Some(100),
|
||||||
|
"custom",
|
||||||
|
),
|
||||||
|
];
|
||||||
|
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
resolve_effective_rate_limit_policy(Some(120), "custom", &groups),
|
resolve_effective_rate_limit_policy(Some(120), "custom", &groups),
|
||||||
Some(60)
|
Some(100)
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn rate_limit_unlimited_does_not_bypass_limited_group() {
|
fn rate_limit_unlimited_group_overrides_limited_groups() {
|
||||||
|
let groups = vec![
|
||||||
|
sample_group(
|
||||||
|
"default",
|
||||||
|
10,
|
||||||
|
None,
|
||||||
|
"unrestricted",
|
||||||
|
Some(30),
|
||||||
|
"custom",
|
||||||
|
),
|
||||||
|
sample_group(
|
||||||
|
"tier-2",
|
||||||
|
20,
|
||||||
|
None,
|
||||||
|
"unrestricted",
|
||||||
|
Some(0),
|
||||||
|
"custom",
|
||||||
|
),
|
||||||
|
];
|
||||||
|
|
||||||
|
assert_eq!(
|
||||||
|
resolve_effective_rate_limit_policy(None, "system", &groups),
|
||||||
|
Some(0)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn rate_limit_user_policy_still_restricts_group_grants() {
|
||||||
let groups = vec![sample_group(
|
let groups = vec![sample_group(
|
||||||
"restricted",
|
"tier-1",
|
||||||
10,
|
10,
|
||||||
None,
|
None,
|
||||||
"unrestricted",
|
"unrestricted",
|
||||||
Some(60),
|
Some(100),
|
||||||
"custom",
|
"custom",
|
||||||
)];
|
)];
|
||||||
|
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
resolve_effective_rate_limit_policy(Some(0), "custom", &groups),
|
resolve_effective_rate_limit_policy(Some(60), "custom", &groups),
|
||||||
Some(60)
|
Some(60)
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user