Skip to content
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(network): divide compression algorithm support in three categories #150

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading