Skip to content

Commit

Permalink
feat(network)!: divide compression algorithm support in three categor…
Browse files Browse the repository at this point in the history
…ies: supported, preferred and disabled

BREAKING CHANGES: Compression in algorithm configuration format is changed as follows:
```toml
algorithms = { lz4 = "preferred" }
```
  • Loading branch information
nerodono committed Jan 16, 2025
1 parent f86970b commit f91660f
Show file tree
Hide file tree
Showing 4 changed files with 299 additions and 26 deletions.
34 changes: 29 additions & 5 deletions elfo-network/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,22 +58,46 @@ pub struct Config {
pub idle_timeout: Duration,
}

/// Preference.
#[derive(Debug, Clone, Copy, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum Preference {
/// This is preferred.
Preferred,

/// This is just supported.
Supported,
}

/// Preferences for the compression algorithms
/// selection.
#[derive(Debug, Deserialize, Default, Clone)]
pub struct CompressionPreference {
/// LZ4 compression algorithm.
pub lz4: Option<Preference>,
}

/// Compression settings.
#[derive(Debug, Default, Deserialize, Clone)]
pub struct CompressionConfig {
/// Compression algorithm.
/// Compression algorithms.
///
/// Example:
/// ```toml
/// algorithms = { lz4 = "preferred" }
/// ```
///
/// Preferred implies supported.
#[serde(default)]
pub algorithm: CompressionAlgorithm,
pub algorithms: CompressionPreference,
}

/// Compression algorithms.
#[derive(Debug, Default, PartialEq, Eq, Deserialize, Clone)]
pub enum CompressionAlgorithm {
/// LZ4 with default compression level.
Lz4,
/// Compression disabled.
#[default]
None,
Lz4,
}

fn default_ping_interval() -> Duration {
Expand Down
21 changes: 15 additions & 6 deletions elfo-network/src/discovery/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use elfo_core::{

use crate::{
codec::format::{NetworkAddr, NetworkEnvelope, NetworkEnvelopePayload},
config::{self, CompressionAlgorithm, Transport},
config::{self, Transport},
node_map::{NodeInfo, NodeMap},
protocol::{internode, DataConnectionFailed, GroupInfo, HandleConnection},
socket::{self, ReadError, Socket},
Expand Down Expand Up @@ -135,12 +135,21 @@ impl Discovery {
Ok(())
}

fn get_compression(&self) -> socket::Compression {
use socket::Algorithms as Algos;

let mut compression = socket::Compression::empty();
let cfg = &self.cfg.compression.algorithms;

compression.toggle(Algos::LZ4, cfg.lz4);

compression
}

fn get_capabilities(&self) -> socket::Capabilities {
let mut capabilities = socket::Capabilities::empty();
if self.cfg.compression.algorithm == CompressionAlgorithm::Lz4 {
capabilities |= socket::Capabilities::LZ4;
}
capabilities
let compression = self.get_compression();

socket::Capabilities::new(compression)
}

fn on_update_config(&mut self) {
Expand Down
16 changes: 15 additions & 1 deletion elfo-network/src/socket/handshake.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ use tokio::io;

use elfo_core::addr::{NodeLaunchId, NodeNo};

use super::{raw, Capabilities};
use crate::config::CompressionAlgorithm;

use super::{raw, Algorithms, Capabilities};

const THIS_NODE_VERSION: u8 = 0;

Expand Down Expand Up @@ -39,6 +41,18 @@ impl Handshake {
}
}

pub(super) fn choose_compression(&self) -> Option<CompressionAlgorithm> {
let compr = self.capabilities.compression().preferred();

// Actual selection logic is done in the [`Compression::intersection`],
// let's just check our preferences.
if compr.contains(Algorithms::LZ4) {
Some(CompressionAlgorithm::Lz4)
} else {
None
}
}

pub(super) fn as_bytes(&self) -> Result<Vec<u8>> {
let mut buf = Cursor::new(Self::make_containing_buf());

Expand Down
Loading

0 comments on commit f91660f

Please sign in to comment.