Remove unnecessary arc and mutex for rpc notifications (#8351)
This commit is contained in:
parent
ab361a8073
commit
01697a9f5c
|
@ -11,7 +11,6 @@ use solana_sdk::{
|
||||||
account::Account, clock::Slot, pubkey::Pubkey, signature::Signature, transaction,
|
account::Account, clock::Slot, pubkey::Pubkey, signature::Signature, transaction,
|
||||||
};
|
};
|
||||||
use solana_vote_program::vote_state::MAX_LOCKOUT_HISTORY;
|
use solana_vote_program::vote_state::MAX_LOCKOUT_HISTORY;
|
||||||
use std::ops::DerefMut;
|
|
||||||
use std::sync::atomic::{AtomicBool, Ordering};
|
use std::sync::atomic::{AtomicBool, Ordering};
|
||||||
use std::sync::mpsc::{Receiver, RecvTimeoutError, SendError, Sender};
|
use std::sync::mpsc::{Receiver, RecvTimeoutError, SendError, Sender};
|
||||||
use std::thread::{Builder, JoinHandle};
|
use std::thread::{Builder, JoinHandle};
|
||||||
|
@ -48,8 +47,6 @@ impl std::fmt::Debug for NotificationEntry {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
type NotificationSend = Arc<Mutex<NotificationEntry>>;
|
|
||||||
|
|
||||||
type RpcAccountSubscriptions =
|
type RpcAccountSubscriptions =
|
||||||
RwLock<HashMap<Pubkey, HashMap<SubscriptionId, (Sink<RpcAccount>, Confirmations)>>>;
|
RwLock<HashMap<Pubkey, HashMap<SubscriptionId, (Sink<RpcAccount>, Confirmations)>>>;
|
||||||
type RpcProgramSubscriptions =
|
type RpcProgramSubscriptions =
|
||||||
|
@ -204,7 +201,7 @@ pub struct RpcSubscriptions {
|
||||||
program_subscriptions: Arc<RpcProgramSubscriptions>,
|
program_subscriptions: Arc<RpcProgramSubscriptions>,
|
||||||
signature_subscriptions: Arc<RpcSignatureSubscriptions>,
|
signature_subscriptions: Arc<RpcSignatureSubscriptions>,
|
||||||
slot_subscriptions: Arc<RpcSlotSubscriptions>,
|
slot_subscriptions: Arc<RpcSlotSubscriptions>,
|
||||||
notification_sender: Arc<Mutex<Sender<Arc<Mutex<NotificationEntry>>>>>,
|
notification_sender: Arc<Mutex<Sender<NotificationEntry>>>,
|
||||||
t_cleanup: Option<JoinHandle<()>>,
|
t_cleanup: Option<JoinHandle<()>>,
|
||||||
exit: Arc<AtomicBool>,
|
exit: Arc<AtomicBool>,
|
||||||
}
|
}
|
||||||
|
@ -226,8 +223,8 @@ impl Drop for RpcSubscriptions {
|
||||||
impl RpcSubscriptions {
|
impl RpcSubscriptions {
|
||||||
pub fn new(exit: &Arc<AtomicBool>) -> Self {
|
pub fn new(exit: &Arc<AtomicBool>) -> Self {
|
||||||
let (notification_sender, notification_receiver): (
|
let (notification_sender, notification_receiver): (
|
||||||
Sender<NotificationSend>,
|
Sender<NotificationEntry>,
|
||||||
Receiver<NotificationSend>,
|
Receiver<NotificationEntry>,
|
||||||
) = std::sync::mpsc::channel();
|
) = std::sync::mpsc::channel();
|
||||||
|
|
||||||
let account_subscriptions = Arc::new(RpcAccountSubscriptions::default());
|
let account_subscriptions = Arc::new(RpcAccountSubscriptions::default());
|
||||||
|
@ -397,7 +394,7 @@ impl RpcSubscriptions {
|
||||||
.notification_sender
|
.notification_sender
|
||||||
.lock()
|
.lock()
|
||||||
.unwrap()
|
.unwrap()
|
||||||
.send(Arc::new(Mutex::new(notification_entry)))
|
.send(notification_entry)
|
||||||
{
|
{
|
||||||
Ok(()) => (),
|
Ok(()) => (),
|
||||||
Err(SendError(notification)) => {
|
Err(SendError(notification)) => {
|
||||||
|
@ -411,7 +408,7 @@ impl RpcSubscriptions {
|
||||||
|
|
||||||
fn process_notifications(
|
fn process_notifications(
|
||||||
exit: Arc<AtomicBool>,
|
exit: Arc<AtomicBool>,
|
||||||
notification_receiver: Receiver<Arc<Mutex<NotificationEntry>>>,
|
notification_receiver: Receiver<NotificationEntry>,
|
||||||
account_subscriptions: Arc<RpcAccountSubscriptions>,
|
account_subscriptions: Arc<RpcAccountSubscriptions>,
|
||||||
program_subscriptions: Arc<RpcProgramSubscriptions>,
|
program_subscriptions: Arc<RpcProgramSubscriptions>,
|
||||||
signature_subscriptions: Arc<RpcSignatureSubscriptions>,
|
signature_subscriptions: Arc<RpcSignatureSubscriptions>,
|
||||||
|
@ -422,57 +419,54 @@ impl RpcSubscriptions {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
match notification_receiver.recv_timeout(Duration::from_millis(RECEIVE_DELAY_MILLIS)) {
|
match notification_receiver.recv_timeout(Duration::from_millis(RECEIVE_DELAY_MILLIS)) {
|
||||||
Ok(notification_entry) => {
|
Ok(notification_entry) => match notification_entry {
|
||||||
let mut notification_entry = notification_entry.lock().unwrap();
|
NotificationEntry::Slot(slot_info) => {
|
||||||
match notification_entry.deref_mut() {
|
let subscriptions = slot_subscriptions.read().unwrap();
|
||||||
NotificationEntry::Slot(slot_info) => {
|
for (_, sink) in subscriptions.iter() {
|
||||||
let subscriptions = slot_subscriptions.read().unwrap();
|
sink.notify(Ok(slot_info)).wait().unwrap();
|
||||||
for (_, sink) in subscriptions.iter() {
|
|
||||||
sink.notify(Ok(*slot_info)).wait().unwrap();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
NotificationEntry::Bank((current_slot, bank_forks)) => {
|
|
||||||
let pubkeys: Vec<_> = {
|
|
||||||
let subs = account_subscriptions.read().unwrap();
|
|
||||||
subs.keys().cloned().collect()
|
|
||||||
};
|
|
||||||
for pubkey in &pubkeys {
|
|
||||||
Self::check_account(
|
|
||||||
pubkey,
|
|
||||||
*current_slot,
|
|
||||||
&bank_forks,
|
|
||||||
account_subscriptions.clone(),
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
let programs: Vec<_> = {
|
|
||||||
let subs = program_subscriptions.read().unwrap();
|
|
||||||
subs.keys().cloned().collect()
|
|
||||||
};
|
|
||||||
for program_id in &programs {
|
|
||||||
Self::check_program(
|
|
||||||
program_id,
|
|
||||||
*current_slot,
|
|
||||||
&bank_forks,
|
|
||||||
program_subscriptions.clone(),
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
let signatures: Vec<_> = {
|
|
||||||
let subs = signature_subscriptions.read().unwrap();
|
|
||||||
subs.keys().cloned().collect()
|
|
||||||
};
|
|
||||||
for signature in &signatures {
|
|
||||||
Self::check_signature(
|
|
||||||
signature,
|
|
||||||
*current_slot,
|
|
||||||
&bank_forks,
|
|
||||||
signature_subscriptions.clone(),
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
NotificationEntry::Bank((current_slot, bank_forks)) => {
|
||||||
|
let pubkeys: Vec<_> = {
|
||||||
|
let subs = account_subscriptions.read().unwrap();
|
||||||
|
subs.keys().cloned().collect()
|
||||||
|
};
|
||||||
|
for pubkey in &pubkeys {
|
||||||
|
Self::check_account(
|
||||||
|
pubkey,
|
||||||
|
current_slot,
|
||||||
|
&bank_forks,
|
||||||
|
account_subscriptions.clone(),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
let programs: Vec<_> = {
|
||||||
|
let subs = program_subscriptions.read().unwrap();
|
||||||
|
subs.keys().cloned().collect()
|
||||||
|
};
|
||||||
|
for program_id in &programs {
|
||||||
|
Self::check_program(
|
||||||
|
program_id,
|
||||||
|
current_slot,
|
||||||
|
&bank_forks,
|
||||||
|
program_subscriptions.clone(),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
let signatures: Vec<_> = {
|
||||||
|
let subs = signature_subscriptions.read().unwrap();
|
||||||
|
subs.keys().cloned().collect()
|
||||||
|
};
|
||||||
|
for signature in &signatures {
|
||||||
|
Self::check_signature(
|
||||||
|
signature,
|
||||||
|
current_slot,
|
||||||
|
&bank_forks,
|
||||||
|
signature_subscriptions.clone(),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
Err(RecvTimeoutError::Timeout) => {
|
Err(RecvTimeoutError::Timeout) => {
|
||||||
// not a problem - try reading again
|
// not a problem - try reading again
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue