mirror of
https://github.com/serai-dex/serai.git
synced 2025-12-08 20:29:23 +00:00
Use dedicated Queues for each from-to pair
Prevents one Processor's message from halting the entire pipeline.
This commit is contained in:
@@ -200,7 +200,7 @@ impl Processor {
|
||||
Serai::new(&self.serai_rpc).await.unwrap()
|
||||
}
|
||||
|
||||
/// Send a message to a processor as its coordinator.
|
||||
/// Send a message to the coordinator as a processor.
|
||||
pub async fn send_message(&mut self, msg: impl Into<ProcessorMessage>) {
|
||||
let msg: ProcessorMessage = msg.into();
|
||||
self
|
||||
@@ -217,14 +217,14 @@ impl Processor {
|
||||
self.next_send_id += 1;
|
||||
}
|
||||
|
||||
/// Receive a message from a processor as its coordinator.
|
||||
/// Receive a message from the coordinator as a processor.
|
||||
pub async fn recv_message(&mut self) -> CoordinatorMessage {
|
||||
let msg = tokio::time::timeout(Duration::from_secs(10), self.queue.next(self.next_recv_id))
|
||||
let msg = tokio::time::timeout(Duration::from_secs(10), self.queue.next(Service::Coordinator))
|
||||
.await
|
||||
.unwrap();
|
||||
assert_eq!(msg.from, Service::Coordinator);
|
||||
assert_eq!(msg.id, self.next_recv_id);
|
||||
self.queue.ack(self.next_recv_id).await;
|
||||
self.queue.ack(Service::Coordinator, msg.id).await;
|
||||
self.next_recv_id += 1;
|
||||
serde_json::from_slice(&msg.msg).unwrap()
|
||||
}
|
||||
|
||||
@@ -106,28 +106,54 @@ fn basic_functionality() {
|
||||
// Successfully get it
|
||||
let bitcoin = MessageQueue::new(
|
||||
Service::Processor(NetworkId::Bitcoin),
|
||||
rpc,
|
||||
rpc.clone(),
|
||||
Zeroizing::new(priv_keys[&NetworkId::Bitcoin]),
|
||||
);
|
||||
let msg = bitcoin.next(0).await;
|
||||
let msg = bitcoin.next(Service::Coordinator).await;
|
||||
assert_eq!(msg.from, Service::Coordinator);
|
||||
assert_eq!(msg.id, 0);
|
||||
assert_eq!(&msg.msg, b"Hello, World!");
|
||||
|
||||
// If we don't ack it, it should continue to be returned
|
||||
assert_eq!(msg, bitcoin.next(0).await);
|
||||
assert_eq!(msg, bitcoin.next(Service::Coordinator).await);
|
||||
|
||||
// Acknowledging it should yield the next message
|
||||
bitcoin.ack(0).await;
|
||||
bitcoin.ack(Service::Coordinator, 0).await;
|
||||
|
||||
let next_msg = bitcoin.next(1).await;
|
||||
let next_msg = bitcoin.next(Service::Coordinator).await;
|
||||
assert!(msg != next_msg);
|
||||
assert_eq!(next_msg.from, Service::Coordinator);
|
||||
assert_eq!(next_msg.id, 1);
|
||||
assert_eq!(&next_msg.msg, b"Hello, World, again!");
|
||||
bitcoin.ack(1).await;
|
||||
bitcoin.ack(Service::Coordinator, 1).await;
|
||||
|
||||
// No further messages should be available
|
||||
tokio::time::timeout(core::time::Duration::from_secs(10), bitcoin.next(2)).await.unwrap_err();
|
||||
tokio::time::timeout(core::time::Duration::from_secs(10), bitcoin.next(Service::Coordinator))
|
||||
.await
|
||||
.unwrap_err();
|
||||
|
||||
// Queueing to a distinct processor should work, with a unique ID
|
||||
coordinator
|
||||
.queue(
|
||||
Metadata {
|
||||
from: Service::Coordinator,
|
||||
to: Service::Processor(NetworkId::Monero),
|
||||
// Intents should be per-from-to, making this valid
|
||||
intent: b"intent".to_vec(),
|
||||
},
|
||||
b"Hello, World!".to_vec(),
|
||||
)
|
||||
.await;
|
||||
|
||||
let monero = MessageQueue::new(
|
||||
Service::Processor(NetworkId::Monero),
|
||||
rpc,
|
||||
Zeroizing::new(priv_keys[&NetworkId::Monero]),
|
||||
);
|
||||
assert_eq!(monero.next(Service::Coordinator).await.id, 0);
|
||||
monero.ack(Service::Coordinator, 0).await;
|
||||
tokio::time::timeout(core::time::Duration::from_secs(10), monero.next(Service::Coordinator))
|
||||
.await
|
||||
.unwrap_err();
|
||||
});
|
||||
}
|
||||
|
||||
@@ -222,13 +222,15 @@ impl Coordinator {
|
||||
|
||||
/// Receive a message from a processor as its coordinator.
|
||||
pub async fn recv_message(&mut self) -> ProcessorMessage {
|
||||
let msg =
|
||||
tokio::time::timeout(core::time::Duration::from_secs(10), self.queue.next(self.next_recv_id))
|
||||
.await
|
||||
.unwrap();
|
||||
let msg = tokio::time::timeout(
|
||||
core::time::Duration::from_secs(10),
|
||||
self.queue.next(Service::Processor(self.network)),
|
||||
)
|
||||
.await
|
||||
.unwrap();
|
||||
assert_eq!(msg.from, Service::Processor(self.network));
|
||||
assert_eq!(msg.id, self.next_recv_id);
|
||||
self.queue.ack(self.next_recv_id).await;
|
||||
self.queue.ack(Service::Processor(self.network), msg.id).await;
|
||||
self.next_recv_id += 1;
|
||||
serde_json::from_slice(&msg.msg).unwrap()
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user