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

After fanatids review #356

Merged
merged 2 commits into from
Mar 12, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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 core/src/structures/rotating_queue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ impl<T: Clone> RotatingQueue<T> {
if !self.elements.is_empty() {
let current = self.current.fetch_add(1, Ordering::Relaxed);
let index = current % (self.elements.len());
Some(self.elements[index].clone())
self.elements.get(index).cloned()
} else {
None
}
Expand Down
69 changes: 44 additions & 25 deletions services/src/tx_sender.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::time::{Duration, Instant};
use std::time::Duration;

use anyhow::bail;
use chrono::Utc;
Expand Down Expand Up @@ -78,32 +78,51 @@ impl TxSender {
notifier: Option<NotificationSender>,
) -> AnyhowJoinHandle {
tokio::spawn(async move {
let mut instant = Instant::now();
let mut notifications = vec![];
while let Some(transaction_info) = recv.recv().await {
self.forward_txs(&transaction_info).await;

// send transaction notifications in batches
let mut interval = tokio::time::interval(INTERVAL_PER_BATCH_IN_MS);
let notify_transaction_messages = |notifications: &mut Vec<TransactionNotification>| {
if notifications.is_empty() {
// no notifications to send
return;
}
if let Some(notifier) = &notifier {
let forwarded_slot = self.data_cache.slot_cache.get_current_slot();
let forwarded_local_time = Utc::now();
let tx_notification = TransactionNotification {
signature: transaction_info.signature,
recent_slot: transaction_info.slot,
forwarded_slot,
forwarded_local_time,
processed_slot: None,
cu_consumed: None,
cu_requested: None,
quic_response: 0,
};
notifications.push(tx_notification);
if instant.elapsed() > INTERVAL_PER_BATCH_IN_MS {
// send notification for sent transactions
let _ = notifier.send(NotificationMsg::TxNotificationMsg(
notifications.drain(..).collect_vec(),
));
instant = Instant::now();
// send notification for sent transactions
let _ = notifier.send(NotificationMsg::TxNotificationMsg(
notifications.drain(..).collect_vec(),
));
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

std::mem::take does not need iterator, but maybe resulted generated code would be same

}
};

loop {
tokio::select! {
transaction_info = recv.recv() => {
if let Some(transaction_info) = transaction_info {
self.forward_txs(&transaction_info).await;

if notifier.is_some() {
let forwarded_slot = self.data_cache.slot_cache.get_current_slot();
let forwarded_local_time = Utc::now();
let tx_notification = TransactionNotification {
signature: transaction_info.signature,
recent_slot: transaction_info.slot,
forwarded_slot,
forwarded_local_time,
processed_slot: None,
cu_consumed: None,
cu_requested: None,
quic_response: 0,
};
notifications.push(tx_notification);
}
} else {
notify_transaction_messages(&mut notifications);
log::warn!("TxSender reciever broken");
break;
}

},
_ = interval.tick() => {
notify_transaction_messages(&mut notifications);
}
}
}
Expand Down
Loading