Skip to content

Commit

Permalink
Optimize link selection metadata
Browse files Browse the repository at this point in the history
This reduces the size of metadata fields used in link selection as they
can substantially increase the size of HELLO messages.

The abbreviations "rel" and "prio" are used for "priorities" and
"reliability" respectively. Furthermore, the numbers 0 and 1 are used to
denote "best_effort" and "reliable" reliabilities.
  • Loading branch information
fuzzypixelz committed Oct 18, 2024
1 parent 8a6dad2 commit 5a86ebb
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 23 deletions.
4 changes: 2 additions & 2 deletions commons/zenoh-protocol/src/core/endpoint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -187,8 +187,8 @@ impl fmt::Debug for AddressMut<'_> {
pub struct Metadata<'a>(pub(super) &'a str);

impl<'a> Metadata<'a> {
pub const RELIABILITY: &'static str = "reliability";
pub const PRIORITIES: &'static str = "priorities";
pub const RELIABILITY: &'static str = "rel";
pub const PRIORITIES: &'static str = "prio";

pub fn as_str(&self) -> &'a str {
self.0
Expand Down
44 changes: 24 additions & 20 deletions commons/zenoh-protocol/src/core/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -457,24 +457,14 @@ impl TryFrom<u8> for Priority {
#[derive(Debug, Default, Copy, Clone, PartialEq, Eq, Hash, Serialize)]
#[repr(u8)]
pub enum Reliability {
BestEffort = 0,
#[default]
Reliable,
BestEffort,
Reliable = 1,
}

impl Reliability {
pub const DEFAULT: Self = Self::Reliable;

const BEST_EFFORT_STR: &'static str = "best_effort";
const RELIABLE_STR: &'static str = "reliable";

pub fn as_str(&self) -> &str {
match self {
Reliability::BestEffort => Reliability::BEST_EFFORT_STR,
Reliability::Reliable => Reliability::RELIABLE_STR,
}
}

#[cfg(feature = "test")]
pub fn rand() -> Self {
use rand::Rng;
Expand Down Expand Up @@ -508,6 +498,12 @@ impl From<Reliability> for bool {
}
}

impl Display for Reliability {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", *self as u8)
}
}

#[derive(Debug)]
pub struct InvalidReliability {
found: String,
Expand All @@ -518,8 +514,8 @@ impl Display for InvalidReliability {
write!(
f,
"invalid Reliability string, expected `{}` or `{}` but found {}",
Reliability::BEST_EFFORT_STR,
Reliability::RELIABLE_STR,
Reliability::Reliable as u8,
Reliability::BestEffort as u8,
self.found
)
}
Expand All @@ -532,12 +528,20 @@ impl FromStr for Reliability {
type Err = InvalidReliability;

fn from_str(s: &str) -> Result<Self, Self::Err> {
match s {
Reliability::RELIABLE_STR => Ok(Reliability::Reliable),
Reliability::BEST_EFFORT_STR => Ok(Reliability::BestEffort),
other => Err(InvalidReliability {
found: other.to_string(),
}),
let Ok(desc) = s.parse::<u8>() else {
return Err(InvalidReliability {
found: s.to_string(),
});
};

if desc == Reliability::BestEffort as u8 {
Ok(Reliability::BestEffort)
} else if desc == Reliability::Reliable as u8 {
Ok(Reliability::Reliable)
} else {
return Err(InvalidReliability {
found: s.to_string(),
});
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion io/zenoh-link-commons/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ impl Link {
let mut locator = locator.clone();
let mut metadata = locator.metadata_mut();
reliability
.map(|r| metadata.insert(Metadata::RELIABILITY, r.as_str()))
.map(|r| metadata.insert(Metadata::RELIABILITY, r.to_string()))
.transpose()
.expect("adding `reliability` to Locator metadata should not fail");
priorities
Expand Down

0 comments on commit 5a86ebb

Please sign in to comment.