Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Switch from dashmap to papaya #335

Merged
merged 1 commit into from
Jan 31, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,14 @@ rust-version = "1.81.0"
base64 = "0.22"
base64-serde = "0.8"
bytes = "1"
dashmap = "6"
futures = "0.3.1"
hex = "0.4"
ironcore-search-helpers = { version = "0.2", optional = true }
itertools = "0.14"
jsonwebtoken = "9"
lazy_static = "1.4"
log = "0.4"
papaya = "0.1.8"
percent-encoding = "2.1"
protobuf = { version = "3.0", features = ["with-bytes"] }
quick-error = "2"
Expand Down
24 changes: 14 additions & 10 deletions src/internal/document_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -712,8 +712,8 @@ where
F: Future<Output = Result<PolicyResponse, IronOxideErr>>,
{
// if there's a value in the cache, use it
if let Some(cached_policy) = policy_cache.get(grant) {
Ok((vec![], cached_policy.clone()))
if let Some(cached_policy) = policy_cache.pin_owned().get(grant).cloned() {
Ok((vec![], cached_policy))
} else {
// otherwise query the webservice and cache the result if there are no errors
get_policy_f
Expand All @@ -722,10 +722,11 @@ where
let (errs, public_keys) = process_policy(&policy_resp);
if errs.is_empty() {
//if the cache has grown too large, clear it prior to adding new entries
let policy_pin = policy_cache.pin();
if policy_cache.len() >= config.max_entries {
policy_cache.clear()
policy_pin.clear()
}
policy_cache.insert(grant.clone(), public_keys.clone());
policy_pin.insert(grant.clone(), public_keys.clone());
}
(errs, public_keys)
})
Expand Down Expand Up @@ -1359,15 +1360,15 @@ mod tests {

use super::*;
use crate::internal::RequestErrorCode;
use dashmap::DashMap;
use papaya::HashMap;
use std::borrow::Borrow;

#[tokio::test]
async fn get_policy_or() -> Result<(), IronOxideErr> {
let policy_json = r#"{ "usersAndGroups": [ { "type": "group", "id": "data_recovery_abcABC012_.$#|@/:;=+'-f1e11a54-8aa9-4641-aaf3-fb92079499f0", "masterPublicKey": { "x": "GE5XQYcRDRhBcyDpNwlu79x6tshNi111ym1IfxOTIxk=", "y": "amgLgcCEYIPQ4oxinLoAvsO3VG7XTFdRfkG/3tooaZE=" } } ], "invalidUsersAndGroups": [] }"#;

let policy_grant = PolicyGrant::default();
let policy_cache = DashMap::new();
let policy_cache = HashMap::new();
let config = PolicyCachingConfig::default();
let policy_resp: PolicyResponse =
serde_json::from_str(policy_json).expect("json should parse");
Expand All @@ -1388,7 +1389,10 @@ mod tests {

// we've now cached a policy and it's the same as the one that was returned
assert_eq!(1, policy_cache.len());
assert_eq!(policy.1, policy_cache.get(&policy_grant).unwrap().clone());
assert_eq!(
policy.1,
policy_cache.pin().get(&policy_grant).unwrap().clone()
);

// let's get the policy again, but if the policy future executes (cache miss) error
get_cached_policy_or(&config, &policy_grant, &policy_cache, async {
Expand All @@ -1403,7 +1407,7 @@ mod tests {
#[tokio::test]
async fn policy_404_gives_nice_error() -> Result<(), IronOxideErr> {
let policy_grant = PolicyGrant::default();
let policy_cache = DashMap::new();
let policy_cache = HashMap::new();
let config = PolicyCachingConfig::default();

// show transformation of RequestError - 404 for Policy GET to PolicyDoesNotExist
Expand All @@ -1428,7 +1432,7 @@ mod tests {
async fn policy_cache_max_size_honored() -> Result<(), IronOxideErr> {
let policy_json = r#"{ "usersAndGroups": [ { "type": "group", "id": "data_recovery_abcABC012_.$#|@/:;=+'-f1e11a54-8aa9-4641-aaf3-fb92079499f0", "masterPublicKey": { "x": "GE5XQYcRDRhBcyDpNwlu79x6tshNi111ym1IfxOTIxk=", "y": "amgLgcCEYIPQ4oxinLoAvsO3VG7XTFdRfkG/3tooaZE=" } } ], "invalidUsersAndGroups": [] }"#;
let policy_grant = PolicyGrant::default();
let policy_cache = DashMap::new();
let policy_cache = HashMap::new();
let config = PolicyCachingConfig { max_entries: 3 };
let policy_resp: PolicyResponse =
serde_json::from_str(policy_json).expect("json should parse");
Expand Down Expand Up @@ -1470,7 +1474,7 @@ mod tests {
// policy with 1 "good" group and one "bad" one
let policy_json = r#"{ "usersAndGroups": [ { "type": "group", "id": "data_recovery_abcABC012_.$#|@/:;=+'-f1e11a54-8aa9-4641-aaf3-fb92079499f0", "masterPublicKey": { "x": "GE5XQYcRDRhBcyDpNwlu79x6tshNi111ym1IfxOTIxk=", "y": "amgLgcCEYIPQ4oxinLoAvsO3VG7XTFdRfkG/3tooaZE=" } } ], "invalidUsersAndGroups": [{ "type": "group", "id": "group-that-does-not-exist" }] }"#;
let policy_grant = PolicyGrant::default();
let policy_cache = DashMap::new();
let policy_cache = HashMap::new();
let config = PolicyCachingConfig::default();
let policy_resp: PolicyResponse =
serde_json::from_str(policy_json).expect("json should parse");
Expand Down
8 changes: 4 additions & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -188,8 +188,8 @@ use crate::{
policy::PolicyGrant,
user::{UserId, UserResult, UserUpdatePrivateKeyResult},
};
use dashmap::DashMap;
use itertools::EitherOrBoth;
use papaya::HashMap;
use rand::{
rngs::{adapter::ReseedingRng, OsRng},
SeedableRng,
Expand All @@ -205,7 +205,7 @@ use vec1::Vec1;

/// A `Result` alias where the Err case is `IronOxideErr`
pub type Result<T> = std::result::Result<T, IronOxideErr>;
type PolicyCache = DashMap<PolicyGrant, Vec<WithKey<UserOrGroup>>>;
type PolicyCache = HashMap<PolicyGrant, Vec<WithKey<UserOrGroup>>>;

// This is where we export structs that don't fit into a single module.
// They were previously exported at the top level, but added clutter to the docs landing page.
Expand Down Expand Up @@ -428,7 +428,7 @@ impl IronOxide {
/// Returns the number of entries cleared from the cache.
pub fn clear_policy_cache(&self) -> usize {
let size = self.policy_eval_cache.len();
self.policy_eval_cache.clear();
self.policy_eval_cache.pin().clear();
size
}

Expand All @@ -448,7 +448,7 @@ impl IronOxide {
BYTES_BEFORE_RESEEDING,
OsRng,
)),
policy_eval_cache: DashMap::new(),
policy_eval_cache: HashMap::new(),
}
}

Expand Down
Loading