-
Notifications
You must be signed in to change notification settings - Fork 237
refactor: use a separate queue for inbound disco packets from relays #3309
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
Draft
Frando
wants to merge
10
commits into
main
Choose a base branch
from
Frando/relay-disco-recv
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
243c88f
refactor: use a separate queue for inbound disco packets from relays
Frando d6769b8
fix: do not double-check if relay packets are disco packets
Frando e365392
fix: first split then test for disco
Frando e592069
refactor: use a separate task for the relay disco recv queue
Frando f4034ef
chore: clippy
Frando c5ebe47
debug log actor exit
Frando 68b164c
refactor: apply backpressure without blocking the actor loop
Frando ceaca9f
refactor: make the new code paths cleaner
Frando c52390a
fix: store a list of send wakers on the relay data queue
Frando ba9c167
cleanups and docs
Frando File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
This file contains hidden or 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 |
---|---|---|
|
@@ -1094,11 +1094,6 @@ impl MagicSock { | |
return None; | ||
} | ||
|
||
if self.handle_relay_disco_message(&dm.buf, &dm.url, dm.src) { | ||
// DISCO messages are handled internally in the MagicSock, do not pass to Quinn. | ||
return None; | ||
} | ||
|
||
let quic_mapped_addr = self.node_map.receive_relay(&dm.url, dm.src); | ||
|
||
// Normalize local_ip | ||
|
@@ -1119,32 +1114,6 @@ impl MagicSock { | |
Some((dm.src, meta, dm.buf)) | ||
} | ||
|
||
fn handle_relay_disco_message( | ||
&self, | ||
msg: &[u8], | ||
url: &RelayUrl, | ||
relay_node_src: PublicKey, | ||
) -> bool { | ||
match disco::source_and_box(msg) { | ||
Some((source, sealed_box)) => { | ||
if relay_node_src != source { | ||
// TODO: return here? | ||
warn!("Received relay disco message from connection for {}, but with message from {}", relay_node_src.fmt_short(), source.fmt_short()); | ||
} | ||
self.handle_disco_message( | ||
source, | ||
sealed_box, | ||
DiscoMessageSource::Relay { | ||
url: url.clone(), | ||
key: relay_node_src, | ||
}, | ||
); | ||
true | ||
} | ||
None => false, | ||
} | ||
} | ||
|
||
/// Handles a discovery message. | ||
#[instrument("disco_in", skip_all, fields(node = %sender.fmt_short(), %src))] | ||
fn handle_disco_message(&self, sender: PublicKey, sealed_box: &[u8], src: DiscoMessageSource) { | ||
|
@@ -1827,7 +1796,13 @@ impl Handle { | |
|
||
let mut actor_tasks = JoinSet::default(); | ||
|
||
let relay_actor = RelayActor::new(msock.clone(), relay_datagram_recv_queue, relay_protocol); | ||
let (relay_disco_recv_tx, mut relay_disco_recv_rx) = tokio::sync::mpsc::channel(1024); | ||
let relay_actor = RelayActor::new( | ||
msock.clone(), | ||
relay_datagram_recv_queue, | ||
relay_disco_recv_tx, | ||
relay_protocol, | ||
); | ||
let relay_actor_cancel_token = relay_actor.cancel_token(); | ||
actor_tasks.spawn( | ||
async move { | ||
|
@@ -1837,6 +1812,23 @@ impl Handle { | |
} | ||
.instrument(info_span!("relay-actor")), | ||
); | ||
actor_tasks.spawn({ | ||
let msock = msock.clone(); | ||
async move { | ||
while let Some(message) = relay_disco_recv_rx.recv().await { | ||
msock.handle_disco_message( | ||
message.source, | ||
&message.sealed_box, | ||
DiscoMessageSource::Relay { | ||
url: message.relay_url, | ||
key: message.relay_remote_node_id, | ||
}, | ||
); | ||
} | ||
debug!("relay-disco-recv actor closed"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: "actor closed" is sufficient since you already have an info span with the actor name. But I don't mind if you do this either. |
||
} | ||
.instrument(info_span!("relay-disco-recv")) | ||
}); | ||
|
||
#[cfg(not(wasm_browser))] | ||
let _ = actor_tasks.spawn({ | ||
|
@@ -2123,15 +2115,17 @@ impl RelayDatagramSendChannelReceiver { | |
#[derive(Debug)] | ||
struct RelayDatagramRecvQueue { | ||
queue: ConcurrentQueue<RelayRecvDatagram>, | ||
waker: AtomicWaker, | ||
recv_waker: AtomicWaker, | ||
send_wakers: ConcurrentQueue<Waker>, | ||
} | ||
|
||
impl RelayDatagramRecvQueue { | ||
/// Creates a new, empty queue with a fixed size bound of 512 items. | ||
fn new() -> Self { | ||
Self { | ||
queue: ConcurrentQueue::bounded(512), | ||
waker: AtomicWaker::new(), | ||
recv_waker: AtomicWaker::new(), | ||
send_wakers: ConcurrentQueue::unbounded(), | ||
} | ||
} | ||
|
||
|
@@ -2144,10 +2138,49 @@ impl RelayDatagramRecvQueue { | |
item: RelayRecvDatagram, | ||
) -> Result<(), concurrent_queue::PushError<RelayRecvDatagram>> { | ||
self.queue.push(item).inspect(|_| { | ||
self.waker.wake(); | ||
self.recv_waker.wake(); | ||
}) | ||
} | ||
|
||
/// Polls for whether the queue has free slots for sending items. | ||
/// | ||
/// If the queue has free slots, this returns [`Poll::Ready`]. | ||
/// If the queue is full, [`Poll::Pending`] is returned and the waker | ||
/// is stored and woken once the queue has free slots. | ||
/// | ||
/// This can be called from multiple tasks concurrently. If a slot becomes | ||
/// available, all stored wakers will be woken simultaneously. | ||
/// This also means that even if [`Poll::Ready`] is returned, it is not | ||
/// guaranteed that [`Self::try_send`] will return `Ok` on the next call, | ||
/// because another send task could have used the slot already. | ||
fn poll_send_ready(&self, cx: &mut Context<'_>) -> Poll<Result<()>> { | ||
if self.queue.is_closed() { | ||
Poll::Ready(Err(anyhow!("Queue closed"))) | ||
} else if !self.queue.is_full() { | ||
Poll::Ready(Ok(())) | ||
} else { | ||
match self.send_wakers.push(cx.waker().clone()) { | ||
Ok(()) => Poll::Pending, | ||
Err(concurrent_queue::PushError::Full(_)) => { | ||
unreachable!("Send waker queue is unbounded") | ||
} | ||
Err(concurrent_queue::PushError::Closed(_)) => { | ||
Poll::Ready(Err(anyhow!("Queue closed"))) | ||
} | ||
} | ||
} | ||
} | ||
|
||
async fn send_ready(&self) -> Result<()> { | ||
std::future::poll_fn(|cx| self.poll_send_ready(cx)).await | ||
} | ||
|
||
fn wake_senders(&self) { | ||
while let Ok(waker) = self.send_wakers.pop() { | ||
waker.wake(); | ||
} | ||
} | ||
|
||
/// Polls for new items in the queue. | ||
/// | ||
/// Although this method is available from `&self`, it must not be | ||
|
@@ -2162,23 +2195,31 @@ impl RelayDatagramRecvQueue { | |
/// to be able to poll from `&self`. | ||
fn poll_recv(&self, cx: &mut Context) -> Poll<Result<RelayRecvDatagram>> { | ||
match self.queue.pop() { | ||
Ok(value) => Poll::Ready(Ok(value)), | ||
Ok(value) => { | ||
self.wake_senders(); | ||
Poll::Ready(Ok(value)) | ||
} | ||
Err(concurrent_queue::PopError::Empty) => { | ||
self.waker.register(cx.waker()); | ||
self.recv_waker.register(cx.waker()); | ||
|
||
match self.queue.pop() { | ||
Ok(value) => { | ||
self.waker.take(); | ||
self.recv_waker.take(); | ||
self.wake_senders(); | ||
Poll::Ready(Ok(value)) | ||
} | ||
Err(concurrent_queue::PopError::Empty) => Poll::Pending, | ||
Err(concurrent_queue::PopError::Closed) => { | ||
self.waker.take(); | ||
self.recv_waker.take(); | ||
self.wake_senders(); | ||
Poll::Ready(Err(anyhow!("Queue closed"))) | ||
} | ||
} | ||
} | ||
Err(concurrent_queue::PopError::Closed) => Poll::Ready(Err(anyhow!("Queue closed"))), | ||
Err(concurrent_queue::PopError::Closed) => { | ||
self.wake_senders(); | ||
Poll::Ready(Err(anyhow!("Queue closed"))) | ||
} | ||
} | ||
} | ||
} | ||
|
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.