-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add function to create all channels
commit-id:4cc36c62
- Loading branch information
1 parent
2085ff5
commit 7547018
Showing
1 changed file
with
38 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
use starknet_mempool_types::communication::MempoolRequestAndResponseSender; | ||
use tokio::sync::mpsc::{channel, Receiver, Sender}; | ||
|
||
pub struct ComponentCommunication<T: Send + Sync> { | ||
tx: Sender<T>, | ||
rx: Option<Receiver<T>>, | ||
} | ||
|
||
impl<T: Send + Sync> ComponentCommunication<T> { | ||
fn get_tx(&self) -> Sender<T> { | ||
self.tx.clone() | ||
} | ||
fn get_rx(&mut self) -> Receiver<T> { | ||
self.rx.take().expect("Receiver already taken") | ||
} | ||
} | ||
|
||
pub struct MempoolNodeCommunication { | ||
mempool_channel: ComponentCommunication<MempoolRequestAndResponseSender>, | ||
} | ||
|
||
impl MempoolNodeCommunication { | ||
pub fn get_mempool_tx(&self) -> Sender<MempoolRequestAndResponseSender> { | ||
self.mempool_channel.get_tx() | ||
} | ||
pub fn get_mempool_rx(&mut self) -> Receiver<MempoolRequestAndResponseSender> { | ||
self.mempool_channel.get_rx() | ||
} | ||
} | ||
|
||
pub fn create_node_channels() -> MempoolNodeCommunication { | ||
const MEMPOOL_INVOCATIONS_QUEUE_SIZE: usize = 32; | ||
let (tx_mempool, rx_mempool) = | ||
channel::<MempoolRequestAndResponseSender>(MEMPOOL_INVOCATIONS_QUEUE_SIZE); | ||
MempoolNodeCommunication { | ||
mempool_channel: ComponentCommunication { tx: tx_mempool, rx: Some(rx_mempool) }, | ||
} | ||
} |