Skip to content

Commit

Permalink
Slightly lighter API
Browse files Browse the repository at this point in the history
  • Loading branch information
alexsnaps committed Sep 12, 2024
1 parent 80a645c commit a4e070c
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 16 deletions.
2 changes: 1 addition & 1 deletion limitador-server/src/http_api/request_types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ pub struct Limit {
impl From<&LimitadorLimit> for Limit {
fn from(ll: &LimitadorLimit) -> Self {
Self {
id: ll.id().clone(),
id: ll.id().map(|id| id.to_string()),
namespace: ll.namespace().as_ref().to_string(),
max_value: ll.max_value(),
seconds: ll.seconds(),
Expand Down
2 changes: 1 addition & 1 deletion limitador/src/counter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ impl Counter {
Duration::from_secs(self.limit.seconds())
}

pub fn id(&self) -> &Option<String> {
pub fn id(&self) -> Option<&str> {
self.limit.id()
}

Expand Down
6 changes: 3 additions & 3 deletions limitador/src/limit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -364,8 +364,8 @@ impl Limit {
&self.namespace
}

pub fn id(&self) -> &Option<String> {
&self.id
pub fn id(&self) -> Option<&str> {
self.id.as_deref()
}

pub fn max_value(&self) -> u64 {
Expand Down Expand Up @@ -1043,6 +1043,6 @@ mod tests {
vec!["app_id"],
);

assert_eq!(limit.id().clone(), Some("test_id".to_string()))
assert_eq!(limit.id(), Some("test_id"))
}
}
21 changes: 10 additions & 11 deletions limitador/src/storage/keys.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,26 +40,25 @@ pub fn key_for_counter(counter: &Counter) -> Vec<u8> {
}

pub fn key_for_counters_of_limit(limit: &Limit) -> Vec<u8> {
if limit.id().is_none() {
let namespace = limit.namespace().as_ref();
format!(
"namespace:{{{namespace}}},counters_of_limit:{}",
serde_json::to_string(limit).unwrap()
)
.into_bytes()
} else {
if let Some(id) = limit.id() {
#[derive(PartialEq, Debug, Serialize, Deserialize)]
struct IdLimitKey<'a> {
id: &'a str,
}

let id = limit.id().as_ref().unwrap();
let key = IdLimitKey { id: id.as_ref() };
let key = IdLimitKey { id };

let mut encoded_key = Vec::new();
encoded_key = postcard::to_extend(&2u8, encoded_key).unwrap();
encoded_key = postcard::to_extend(&key, encoded_key).unwrap();
encoded_key
} else {
let namespace = limit.namespace().as_ref();
format!(
"namespace:{{{namespace}}},counters_of_limit:{}",
serde_json::to_string(limit).unwrap()
)
.into_bytes()
}
}

Expand Down Expand Up @@ -182,7 +181,7 @@ pub mod bin {
impl<'a> From<&'a Counter> for IdCounterKey<'a> {
fn from(counter: &'a Counter) -> Self {
IdCounterKey {
id: counter.id().as_ref().unwrap().as_ref(),
id: counter.id().unwrap(),
variables: counter.variables_for_key(),
}
}
Expand Down

0 comments on commit a4e070c

Please sign in to comment.