Skip to content

Commit

Permalink
Use RwLock rather than Mutex (#5239)
Browse files Browse the repository at this point in the history
  • Loading branch information
hpeebles authored Jan 23, 2024
1 parent b5bac41 commit 50391ec
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 7 deletions.
4 changes: 2 additions & 2 deletions backend/notification_pusher/core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use crate::pusher::Pusher;
use crate::reader::Reader;
use crate::subscription_remover::SubscriptionRemover;
use index_store::IndexStore;
use std::sync::{Arc, Mutex};
use std::sync::{Arc, RwLock};
use tracing::info;
use types::{CanisterId, UserId};
use web_push::SubscriptionInfo;
Expand Down Expand Up @@ -36,7 +36,7 @@ pub async fn run_notifications_pusher<I: IndexStore + 'static>(
tokio::spawn(reader.run());
}

let invalid_subscriptions = Arc::new(Mutex::default());
let invalid_subscriptions = Arc::new(RwLock::default());
for _ in 0..pusher_count {
let pusher = Pusher::new(
receiver.clone(),
Expand Down
10 changes: 5 additions & 5 deletions backend/notification_pusher/core/src/pusher.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::Notification;
use async_channel::{Receiver, Sender};
use std::collections::{BinaryHeap, HashMap};
use std::sync::{Arc, Mutex};
use std::sync::{Arc, RwLock};
use std::time::{SystemTime, UNIX_EPOCH};
use tracing::{error, info};
use types::{Error, TimestampMillis, UserId};
Expand All @@ -17,15 +17,15 @@ pub struct Pusher {
web_push_client: HyperWebPushClient,
sig_builder: PartialVapidSignatureBuilder,
subscriptions_to_remove_sender: Sender<(UserId, String)>,
invalid_subscriptions: Arc<Mutex<HashMap<String, TimestampMillis>>>,
invalid_subscriptions: Arc<RwLock<HashMap<String, TimestampMillis>>>,
}

impl Pusher {
pub fn new(
receiver: Receiver<Notification>,
vapid_private_pem: &str,
subscriptions_to_remove_sender: Sender<(UserId, String)>,
invalid_subscriptions: Arc<Mutex<HashMap<String, TimestampMillis>>>,
invalid_subscriptions: Arc<RwLock<HashMap<String, TimestampMillis>>>,
) -> Self {
Self {
receiver,
Expand All @@ -38,7 +38,7 @@ impl Pusher {

pub async fn run(self) {
while let Ok(notification) = self.receiver.recv().await {
if let Ok(map) = self.invalid_subscriptions.lock() {
if let Ok(map) = self.invalid_subscriptions.read() {
if map.contains_key(&notification.subscription_info.endpoint) {
continue;
}
Expand Down Expand Up @@ -70,7 +70,7 @@ impl Pusher {

let timestamp = SystemTime::now().duration_since(UNIX_EPOCH).unwrap().as_millis() as u64;

if let Ok(mut map) = self.invalid_subscriptions.lock() {
if let Ok(mut map) = self.invalid_subscriptions.write() {
if map.len() > 10000 {
prune_invalid_subscriptions(&mut map);
}
Expand Down

0 comments on commit 50391ec

Please sign in to comment.