-
Notifications
You must be signed in to change notification settings - Fork 1k
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
feat(core)!: make ConnectionId
s globally unique
#3327
Changes from 12 commits
3a84c14
458aa53
d0d8b77
11e5f74
47cf01f
341d096
b337ec1
1beec8c
eb48acb
3d91173
aa19d69
0f53692
244c171
b536773
64a333d
d6b19e4
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -45,30 +45,30 @@ use libp2p_core::upgrade::{InboundUpgradeApply, OutboundUpgradeApply}; | |
use libp2p_core::{upgrade, UpgradeError}; | ||
use libp2p_core::{Endpoint, PeerId}; | ||
use std::future::Future; | ||
use std::sync::atomic::{AtomicUsize, Ordering}; | ||
use std::task::Waker; | ||
use std::time::Duration; | ||
use std::{fmt, io, mem, pin::Pin, task::Context, task::Poll}; | ||
|
||
static NEXT_CONNECTION_ID: AtomicUsize = AtomicUsize::new(1); | ||
|
||
/// Connection identifier. | ||
#[derive(Debug, Copy, Clone, Hash, PartialEq, Eq, PartialOrd, Ord)] | ||
pub struct ConnectionId(usize); | ||
|
||
impl ConnectionId { | ||
/// Creates a `ConnectionId` from a non-negative integer. | ||
/// A "dummy" [`ConnectionId`]. | ||
/// | ||
/// This is primarily useful for creating connection IDs | ||
/// in test environments. There is in general no guarantee | ||
/// that all connection IDs are based on non-negative integers. | ||
pub fn new(id: usize) -> Self { | ||
Self(id) | ||
} | ||
} | ||
|
||
impl std::ops::Add<usize> for ConnectionId { | ||
type Output = Self; | ||
|
||
fn add(self, other: usize) -> Self { | ||
Self(self.0 + other) | ||
/// Really, you should not use this, not even for testing but it is here if you need it. | ||
#[deprecated( | ||
since = "0.42.0", | ||
note = "Don't use this, it will be removed at a later stage again." | ||
)] | ||
pub const DUMMY: ConnectionId = ConnectionId(0); | ||
Comment on lines
+60
to
+67
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. I am not very happy about this but I propose to accept this technical debt to be able to move forward with the connection management PR. Long-term, we would need to re-write the gossipsub and kademlia tests to not manually call lifecycle functions and thus only have 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. @libp2p/rust-libp2p-maintainers Please voice your opinion on this. 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. Yeah it would be nice if this was possible. Other than that I don't see a better alternative for here. |
||
|
||
/// Returns the next available [`ConnectionId`]. | ||
pub(crate) fn next() -> Self { | ||
Self(NEXT_CONNECTION_ID.fetch_add(1, Ordering::SeqCst)) | ||
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. this is very elegant. 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. Credits go to @rkuhn ! |
||
} | ||
} | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We start IDs at
1
so they can never clash with theDUMMY
one below.