Skip to content

Commit

Permalink
perf(rust): support concurrent readers (Mutax -> RwLock):
Browse files Browse the repository at this point in the history
Accroding to https://stackoverflow.com/a/27826181/6739302 :

  You can also use a RwLock instead of a Mutex to allow multiple concurrent readers.

Currently, 'suffix_is_blacklisted()' (thus 'is_valid()') is limited to 1 concurrent reader due to the introductoin of Mutax in commit 'feat(rust): add add_custom_domains :'. This commit fixes this bottleneck.
  • Loading branch information
choznerol committed Nov 9, 2021
1 parent fbc93e9 commit 1f5d70d
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 8 deletions.
8 changes: 4 additions & 4 deletions platform/rust/src/lib.rs

Large diffs are not rendered by default.

8 changes: 4 additions & 4 deletions platform/rust/src/lib.tmpl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@ extern crate fast_chemail;

use lazy_static::lazy_static;
use std::collections::HashSet;
use std::sync::Mutex;
use std::sync::RwLock;

static BLACKLIST: &'static [&'static str] = &[{{ &listSTR }}];

lazy_static! {
static ref CUSTOM_DOMAINS: Mutex<HashSet<&'static str>> = Mutex::new(HashSet::new());
static ref CUSTOM_DOMAINS: RwLock<HashSet<&'static str>> = RwLock::new(HashSet::new());
}

/// # Usage
Expand Down Expand Up @@ -93,7 +93,7 @@ fn all_domain_suffixes(email: &str) -> Vec<String> {


fn suffix_is_blacklisted(domain: &str) -> bool{
return BLACKLIST.contains(&domain) || CUSTOM_DOMAINS.lock().unwrap().contains(&domain)
return BLACKLIST.contains(&domain) || CUSTOM_DOMAINS.read().unwrap().contains(&domain)
}

/// # Usage
Expand Down Expand Up @@ -133,7 +133,7 @@ pub fn blacklist() -> Vec<&'static str> {
/// assert_eq!(true, mailchecker::is_valid("ok@gmail.com"));
/// ```
pub fn add_custom_domains(domains: Vec<&'static str>) -> () {
return CUSTOM_DOMAINS.lock().unwrap().extend(domains.iter().copied());
return CUSTOM_DOMAINS.write().unwrap().extend(domains.iter().copied());
}

// Helpers
Expand Down

0 comments on commit 1f5d70d

Please sign in to comment.