Skip to content

Commit

Permalink
Merge pull request #77 from Kuadrant/just_call_it
Browse files Browse the repository at this point in the history
`RaterLimiter` to take a `&Namespace`
  • Loading branch information
alexsnaps authored Jun 9, 2022
2 parents 51591cc + 4eb2ea6 commit 6e34ef7
Show file tree
Hide file tree
Showing 5 changed files with 114 additions and 121 deletions.
6 changes: 4 additions & 2 deletions limitador-server/src/envoy_rls/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ impl RateLimitService for MyRateLimiter {
}));
}

let namespace = namespace.into();

for descriptor in &req.descriptors {
for entry in &descriptor.entries {
values.insert(entry.key.clone(), entry.value.clone());
Expand All @@ -62,11 +64,11 @@ impl RateLimitService for MyRateLimiter {

let is_rate_limited_res = match &*self.limiter {
Limiter::Blocking(limiter) => {
limiter.check_rate_limited_and_update(namespace, &values, i64::from(hits_addend))
limiter.check_rate_limited_and_update(&namespace, &values, i64::from(hits_addend))
}
Limiter::Async(limiter) => {
limiter
.check_rate_limited_and_update(namespace, &values, i64::from(hits_addend))
.check_rate_limited_and_update(&namespace, &values, i64::from(hits_addend))
.await
}
};
Expand Down
30 changes: 18 additions & 12 deletions limitador-server/src/http_api/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,10 @@ async fn get_limits(
data: web::Data<Arc<Limiter>>,
namespace: web::Path<String>,
) -> Result<web::Json<Vec<Limit>>, ErrorResponse> {
let namespace = &namespace.into_inner().into();
let get_limits_result = match data.get_ref().as_ref() {
Limiter::Blocking(limiter) => limiter.get_limits(namespace.into_inner()),
Limiter::Async(limiter) => limiter.get_limits(namespace.into_inner()).await,
Limiter::Blocking(limiter) => limiter.get_limits(namespace),
Limiter::Async(limiter) => limiter.get_limits(namespace).await,
};

match get_limits_result {
Expand Down Expand Up @@ -108,9 +109,10 @@ async fn delete_limits(
data: web::Data<Arc<Limiter>>,
namespace: web::Path<String>,
) -> Result<web::Json<()>, ErrorResponse> {
let namespace = namespace.into_inner().into();
let delete_limits_result = match data.get_ref().as_ref() {
Limiter::Blocking(limiter) => limiter.delete_limits(namespace.into_inner()),
Limiter::Async(limiter) => limiter.delete_limits(namespace.into_inner()).await,
Limiter::Blocking(limiter) => limiter.delete_limits(&namespace),
Limiter::Async(limiter) => limiter.delete_limits(&namespace).await,
};

match delete_limits_result {
Expand All @@ -124,9 +126,10 @@ async fn get_counters(
data: web::Data<Arc<Limiter>>,
namespace: web::Path<String>,
) -> Result<web::Json<Vec<Counter>>, ErrorResponse> {
let namespace = namespace.into_inner().into();
let get_counters_result = match data.get_ref().as_ref() {
Limiter::Blocking(limiter) => limiter.get_counters(namespace.into_inner()),
Limiter::Async(limiter) => limiter.get_counters(namespace.into_inner()).await,
Limiter::Blocking(limiter) => limiter.get_counters(&namespace),
Limiter::Async(limiter) => limiter.get_counters(&namespace).await,
};

match get_counters_result {
Expand All @@ -151,9 +154,10 @@ async fn check(
values,
delta,
} = request.into_inner();
let namespace = namespace.into();
let is_rate_limited_result = match state.get_ref().as_ref() {
Limiter::Blocking(limiter) => limiter.is_rate_limited(namespace, &values, delta),
Limiter::Async(limiter) => limiter.is_rate_limited(namespace, &values, delta).await,
Limiter::Blocking(limiter) => limiter.is_rate_limited(&namespace, &values, delta),
Limiter::Async(limiter) => limiter.is_rate_limited(&namespace, &values, delta).await,
};

match is_rate_limited_result {
Expand All @@ -178,9 +182,10 @@ async fn report(
values,
delta,
} = request.into_inner();
let namespace = namespace.into();
let update_counters_result = match data.get_ref().as_ref() {
Limiter::Blocking(limiter) => limiter.update_counters(namespace, &values, delta),
Limiter::Async(limiter) => limiter.update_counters(namespace, &values, delta).await,
Limiter::Blocking(limiter) => limiter.update_counters(&namespace, &values, delta),
Limiter::Async(limiter) => limiter.update_counters(&namespace, &values, delta).await,
};

match update_counters_result {
Expand All @@ -199,13 +204,14 @@ async fn check_and_report(
values,
delta,
} = request.into_inner();
let namespace = namespace.into();
let rate_limited_and_update_result = match data.get_ref().as_ref() {
Limiter::Blocking(limiter) => {
limiter.check_rate_limited_and_update(namespace, &values, delta)
limiter.check_rate_limited_and_update(&namespace, &values, delta)
}
Limiter::Async(limiter) => {
limiter
.check_rate_limited_and_update(namespace, &values, delta)
.check_rate_limited_and_update(&namespace, &values, delta)
.await
}
};
Expand Down
14 changes: 11 additions & 3 deletions limitador/benches/bench.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,11 @@ fn bench_is_rate_limited(b: &mut Bencher, test_scenario: &TestScenario, storage:

black_box(
rate_limiter
.is_rate_limited(params.namespace.as_ref(), &params.values, params.delta)
.is_rate_limited(
&params.namespace.to_owned().into(),
&params.values,
params.delta,
)
.unwrap(),
)
})
Expand All @@ -145,7 +149,11 @@ fn bench_update_counters(b: &mut Bencher, test_scenario: &TestScenario, storage:

black_box(
rate_limiter
.update_counters(params.namespace.as_ref(), &params.values, params.delta)
.update_counters(
&params.namespace.to_owned().into(),
&params.values,
params.delta,
)
.unwrap(),
)
})
Expand All @@ -165,7 +173,7 @@ fn bench_check_rate_limited_and_update(
black_box(
rate_limiter
.check_rate_limited_and_update(
params.namespace.as_ref(),
&params.namespace.to_owned().into(),
&params.values,
params.delta,
)
Expand Down
Loading

0 comments on commit 6e34ef7

Please sign in to comment.