use solana_sdk::pubkey::Pubkey;
use crate::common::AnyResult;
use crate::streaming::common::EventBatchProcessor;
use crate::streaming::event_parser::common::filter::EventTypeFilter;
use crate::streaming::event_parser::{Protocol, UnifiedEvent};
use crate::streaming::shred::{ShredEventProcessor, ShredStreamHandler, TransactionWithSlot};
use super::ShredStreamGrpc;
impl ShredStreamGrpc {
pub async fn shredstream_subscribe<F>(
&self,
protocols: Vec<Protocol>,
bot_wallet: Option<Pubkey>,
event_type_filter: Option<EventTypeFilter>,
callback: F,
) -> AnyResult<()>
where
F: Fn(Box<dyn UnifiedEvent>) + Send + Sync + 'static,
{
if self.config.enable_metrics {
self.metrics_manager.start_auto_monitoring().await;
}
let client = (*self.shredstream_client).clone();
let (_stream_task, rx) = ShredStreamHandler::start_stream_processing(
client,
self.config.backpressure.channel_size,
)
.await?;
if self.config.batch.enabled {
self.process_with_batch(rx, protocols, bot_wallet, event_type_filter, callback).await
} else {
self.process_immediate(rx, protocols, bot_wallet, event_type_filter, callback).await
}
}
async fn process_with_batch<F>(
&self,
mut rx: futures::channel::mpsc::Receiver<TransactionWithSlot>,
protocols: Vec<Protocol>,
bot_wallet: Option<Pubkey>,
event_type_filter: Option<EventTypeFilter>,
callback: F,
) -> AnyResult<()>
where
F: Fn(Box<dyn UnifiedEvent>) + Send + Sync + 'static,
{
use futures::StreamExt;
let batch_callback = move |events: Vec<Box<dyn UnifiedEvent>>| {
for event in events {
callback(event);
}
};
let mut batch_processor = EventBatchProcessor::new(
batch_callback,
self.config.batch.batch_size,
self.config.batch.batch_timeout_ms,
);
let event_processor =
ShredEventProcessor::new(self.metrics_manager.clone(), self.config.clone());
while let Some(transaction_with_slot) = rx.next().await {
if let Err(e) = event_processor
.process_transaction_with_batch(
transaction_with_slot,
protocols.clone(),
bot_wallet,
&mut batch_processor,
event_type_filter.clone(),
)
.await
{
log::error!("Error processing transaction: {e:?}");
}
}
batch_processor.flush();
Ok(())
}
async fn process_immediate<F>(
&self,
mut rx: futures::channel::mpsc::Receiver<TransactionWithSlot>,
protocols: Vec<Protocol>,
bot_wallet: Option<Pubkey>,
event_type_filter: Option<EventTypeFilter>,
callback: F,
) -> AnyResult<()>
where
F: Fn(Box<dyn UnifiedEvent>) + Send + Sync + 'static,
{
use futures::StreamExt;
let event_processor =
ShredEventProcessor::new(self.metrics_manager.clone(), self.config.clone());
while let Some(transaction_with_slot) = rx.next().await {
if let Err(e) = event_processor
.process_transaction_immediate(
transaction_with_slot,
protocols.clone(),
bot_wallet,
event_type_filter.clone(),
&callback,
)
.await
{
log::error!("Error processing transaction: {e:?}");
}
}
Ok(())
}
}