-
Notifications
You must be signed in to change notification settings - Fork 1.2k
feat: unreliable datagram support (QUIC + libp2p-datagram) #6489
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
Open
royzah
wants to merge
17
commits into
libp2p:master
Choose a base branch
from
royzah:feat/quic-datagrams
base: master
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.
Open
Changes from 5 commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
2129a42
feat(quic): unreliable datagram support + libp2p-datagram behaviour
royzah 77bf5cb
Merge branch 'master' into feat/quic-datagrams
royzah 986572d
docs: fill changelog PR links (6489)
royzah 4e33535
fix: fold changes into pending 0.44/0.48/0.14 release, add datagram c…
royzah d3164a8
ci(semver): exclude unpublished libp2p-datagram from semver-checks
royzah e04e3a3
feat(core,swarm,quic): expose transport stream id via StreamMuxer::su…
royzah 4844599
feat(datagram): implement specs#680 /dg/1 control stream and framing
royzah 39a828b
docs(metrics): changelog for substream_id forward (6489)
royzah 6a771ce
perf(datagram): bound the pre-establish outbound backlog
royzah 2955f0e
Merge branch 'master' into feat/quic-datagrams
royzah c88803f
Merge branch 'master' into feat/quic-datagrams
royzah 6042d6f
docs(datagram): note the intentional silent-drop on bad/unknown strea…
royzah 15062bd
fix(swarm): log datagram send failure at error level
royzah 8037022
ci: run semver-checks on libp2p-datagram
royzah 033702c
Merge branch 'master' into feat/quic-datagrams
jxs f6bb59b
feat(datagram): report per-connection max datagram size to the sender
royzah 35a6375
refactor(datagram): centralize inbound datagram routing at the connec…
royzah 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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
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 |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| ## 0.1.0 | ||
|
|
||
| - Initial release: a `Behaviour` and `Control` for sending and receiving | ||
| unreliable datagrams over libp2p connections (QUIC only). | ||
| See [PR 6489](https://github.com/libp2p/rust-libp2p/pull/6489). |
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 |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| [package] | ||
| name = "libp2p-datagram" | ||
| version = "0.1.0" | ||
| edition.workspace = true | ||
| rust-version.workspace = true | ||
| description = "Unreliable datagram protocol for libp2p" | ||
| license = "MIT" | ||
| repository = "https://github.com/libp2p/rust-libp2p" | ||
| keywords = ["peer-to-peer", "libp2p", "networking"] | ||
| categories = ["network-programming", "asynchronous"] | ||
|
|
||
| [dependencies] | ||
| bytes = { workspace = true } | ||
| futures = { workspace = true } | ||
| libp2p-core = { workspace = true } | ||
| libp2p-identity = { workspace = true, features = ["peerid"] } | ||
| libp2p-swarm = { workspace = true } | ||
| thiserror = { workspace = true } | ||
|
|
||
| [dev-dependencies] | ||
| libp2p-identity = { workspace = true, features = ["ed25519", "rand"] } | ||
| libp2p-quic = { workspace = true, features = ["tokio"] } | ||
| libp2p-swarm = { workspace = true, features = ["tokio"] } | ||
| tokio = { workspace = true, features = ["full"] } | ||
| tracing-subscriber = { workspace = true, features = ["env-filter"] } | ||
|
|
||
| [lints] | ||
| workspace = true |
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 |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| # libp2p-datagram | ||
|
|
||
| Unreliable datagrams over libp2p connections. | ||
|
|
||
| Datagrams may be dropped, reordered, or duplicated and carry no flow control; | ||
| the caller owns reliability. Only QUIC carries them today; sends on other | ||
| transports fail with `SendError`. | ||
|
|
||
| ```rust,no_run | ||
| use libp2p_datagram as datagram; | ||
|
|
||
| let mut behaviour = datagram::Behaviour::new(); | ||
| let mut control = behaviour.new_control(); | ||
| let mut incoming = behaviour.incoming_datagrams().unwrap(); | ||
|
|
||
| // add `behaviour` to your Swarm, then: | ||
| // control.send_datagram(peer, bytes)?; | ||
| // while let Some((from, bytes)) = incoming.next().await { ... } | ||
| ``` | ||
|
|
||
| ## License | ||
|
|
||
| Licensed under MIT. |
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 |
|---|---|---|
| @@ -0,0 +1,125 @@ | ||
| use std::{ | ||
| pin::Pin, | ||
| task::{Context, Poll}, | ||
| }; | ||
|
|
||
| use bytes::Bytes; | ||
| use futures::{Stream, StreamExt, channel::mpsc}; | ||
| use libp2p_core::{Endpoint, Multiaddr, transport::PortUse}; | ||
| use libp2p_identity::PeerId; | ||
| use libp2p_swarm::{ | ||
| ConnectionDenied, ConnectionId, FromSwarm, NetworkBehaviour, NotifyHandler, THandler, | ||
| THandlerInEvent, THandlerOutEvent, ToSwarm, | ||
| }; | ||
|
|
||
| use crate::{Control, handler::Handler}; | ||
|
|
||
| const CHANNEL_BUFFER: usize = 256; | ||
|
|
||
| /// Sends and receives unreliable datagrams. | ||
| pub struct Behaviour { | ||
| outbound_tx: mpsc::Sender<OutboundDatagram>, | ||
| outbound_rx: mpsc::Receiver<OutboundDatagram>, | ||
| inbound_tx: mpsc::Sender<(PeerId, Bytes)>, | ||
| incoming: Option<mpsc::Receiver<(PeerId, Bytes)>>, | ||
| } | ||
|
|
||
| pub(crate) struct OutboundDatagram { | ||
| pub(crate) peer: PeerId, | ||
| pub(crate) connection: Option<ConnectionId>, | ||
| pub(crate) data: Bytes, | ||
| } | ||
|
|
||
| impl Default for Behaviour { | ||
| fn default() -> Self { | ||
| Self::new() | ||
| } | ||
| } | ||
|
|
||
| impl Behaviour { | ||
| pub fn new() -> Self { | ||
| let (outbound_tx, outbound_rx) = mpsc::channel(CHANNEL_BUFFER); | ||
| let (inbound_tx, incoming) = mpsc::channel(CHANNEL_BUFFER); | ||
| Self { | ||
| outbound_tx, | ||
| outbound_rx, | ||
| inbound_tx, | ||
| incoming: Some(incoming), | ||
| } | ||
| } | ||
|
|
||
| /// A new [`Control`] for sending datagrams. | ||
| pub fn new_control(&self) -> Control { | ||
| Control::new(self.outbound_tx.clone()) | ||
| } | ||
|
|
||
| /// Stream of inbound `(sender, payload)`. `None` after the first call. | ||
| pub fn incoming_datagrams(&mut self) -> Option<IncomingDatagrams> { | ||
| self.incoming.take().map(IncomingDatagrams) | ||
| } | ||
| } | ||
|
|
||
| /// Stream of inbound datagrams tagged with the sending [`PeerId`]. | ||
| pub struct IncomingDatagrams(mpsc::Receiver<(PeerId, Bytes)>); | ||
|
|
||
| impl Stream for IncomingDatagrams { | ||
| type Item = (PeerId, Bytes); | ||
|
|
||
| fn poll_next(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> { | ||
| self.0.poll_next_unpin(cx) | ||
| } | ||
| } | ||
|
|
||
| impl NetworkBehaviour for Behaviour { | ||
| type ConnectionHandler = Handler; | ||
| type ToSwarm = (); | ||
|
|
||
| fn handle_established_inbound_connection( | ||
| &mut self, | ||
| _: ConnectionId, | ||
| peer: PeerId, | ||
| _: &Multiaddr, | ||
| _: &Multiaddr, | ||
| ) -> Result<THandler<Self>, ConnectionDenied> { | ||
| Ok(Handler::new(peer, self.inbound_tx.clone())) | ||
| } | ||
|
|
||
| fn handle_established_outbound_connection( | ||
| &mut self, | ||
| _: ConnectionId, | ||
| peer: PeerId, | ||
| _: &Multiaddr, | ||
| _: Endpoint, | ||
| _: PortUse, | ||
| ) -> Result<THandler<Self>, ConnectionDenied> { | ||
| Ok(Handler::new(peer, self.inbound_tx.clone())) | ||
| } | ||
|
|
||
| fn on_swarm_event(&mut self, _event: FromSwarm) {} | ||
|
|
||
| fn on_connection_handler_event( | ||
| &mut self, | ||
| _: PeerId, | ||
| _: ConnectionId, | ||
| event: THandlerOutEvent<Self>, | ||
| ) { | ||
| libp2p_core::util::unreachable(event); | ||
| } | ||
|
|
||
| fn poll( | ||
| &mut self, | ||
| cx: &mut Context<'_>, | ||
| ) -> Poll<ToSwarm<Self::ToSwarm, THandlerInEvent<Self>>> { | ||
| if let Poll::Ready(Some(out)) = self.outbound_rx.poll_next_unpin(cx) { | ||
| return Poll::Ready(ToSwarm::NotifyHandler { | ||
| peer_id: out.peer, | ||
| handler: match out.connection { | ||
| Some(id) => NotifyHandler::One(id), | ||
| None => NotifyHandler::Any, | ||
| }, | ||
| event: out.data, | ||
| }); | ||
| } | ||
| Poll::Pending | ||
| } | ||
| } |
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.