Skip to content

Commit

Permalink
Merge pull request #43 from 3scale-labs/update-to-infinispan-0.2.0
Browse files Browse the repository at this point in the history
Update to infinispan 0.2.0
  • Loading branch information
davidor authored May 19, 2021
2 parents 3799839 + 2f72a67 commit d10f17d
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 26 deletions.
4 changes: 2 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion limitador/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ lazy_static = "1.4"
redis = { version = "0.17", optional = true, features = ["connection-manager"] }
r2d2 = { version = "0.8", optional = true }
tokio = { version = "0.2", optional = true, features = ["rt-core", "macros", "time"] }
infinispan = { version = "0.1", optional = true }
infinispan = { version = "0.2", optional = true }
reqwest = { version = "0.10", optional = true }

# reqwest dependency. Needed here to avoid problems when compiling
Expand Down
24 changes: 12 additions & 12 deletions limitador/src/storage/infinispan/counters.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,20 +74,20 @@ pub async fn decrement_by(
.await?;

if response.status() == 404 {
// TODO: we could use "reset" here, but it's not implemented in the
// client yet. So for now, delete and create.
let _ = infinispan
.run(&request::counters::delete(&counter_key))
let reset_resp = infinispan
.run(&request::counters::reset(&counter_key))
.await?;

// TODO: the type of counter and its attributes should be configurable.
// For now let's use "weak" counters with default attributes.
let _ = infinispan
.run(
&request::counters::create_weak(&counter_key)
.with_value(create_counter_opts.initial_value - delta),
)
.await?;
if reset_resp.status() == 404 {
// TODO: the type of counter and its attributes should be configurable.
// For now let's use "weak" counters with default attributes.
let _ = infinispan
.run(
&request::counters::create_weak(&counter_key)
.with_value(create_counter_opts.initial_value - delta),
)
.await?;
}

let _ = infinispan
.run(
Expand Down
30 changes: 19 additions & 11 deletions limitador/src/storage/infinispan/infinispan_storage.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crate::counter::Counter;
use crate::limit::{Limit, Namespace};
use crate::storage::infinispan::counters::CounterOpts;
use crate::storage::infinispan::response::response_to_string;
use crate::storage::infinispan::{counters, sets};
use crate::storage::keys::*;
use crate::storage::{AsyncStorage, Authorization, StorageErr};
Expand Down Expand Up @@ -194,21 +195,12 @@ impl AsyncStorage for InfinispanStorage {
}

async fn clear(&self) -> Result<(), StorageErr> {
// TODO: Flush the cache instead of deleting and re-creating. There
// isn't a way to do this using the infinispan client for now.
//
// TODO: delete all counters (they don't belong to any Infinispan
// cache). There isn't a way to do this using the client for now.

let _ = self
.infinispan
.run(&request::caches::delete(INFINISPAN_LIMITS_CACHE_NAME))
.run(&request::caches::clear(INFINISPAN_LIMITS_CACHE_NAME))
.await?;

let _ = self
.infinispan
.run(&request::caches::create_local(INFINISPAN_LIMITS_CACHE_NAME))
.await?;
let _ = self.delete_all_counters().await?;

Ok(())
}
Expand Down Expand Up @@ -247,6 +239,22 @@ impl InfinispanStorage {
Ok(())
}

async fn delete_all_counters(&self) -> Result<(), StorageErr> {
let resp = self.infinispan.run(&request::counters::list()).await?;

let counter_names: HashSet<String> =
serde_json::from_str(&response_to_string(resp).await).unwrap();

for counter_name in counter_names {
let _ = self
.infinispan
.run(&request::counters::delete(counter_name))
.await?;
}

Ok(())
}

async fn counter_keys_of_limit(
&self,
limit: &Limit,
Expand Down

0 comments on commit d10f17d

Please sign in to comment.