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

Reconnect telemetry #1574

Merged
Merged
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
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

24 changes: 24 additions & 0 deletions docs/source/references/config.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,8 @@ Configuration of iroha is done via options in the following document. Here is de
"TELEMETRY": {
"NAME": null,
"URL": null,
"MIN_PERIOD": 1,
"MAX_EXPONENT": 4,
"FILE": null
},
"NETWORK": {
Expand Down Expand Up @@ -530,6 +532,8 @@ Has type `iroha_telemetry::Configuration`. Can be configured via environment var
```json
{
"FILE": null,
"MAX_EXPONENT": 4,
"MIN_PERIOD": 1,
"NAME": null,
"URL": null
}
Expand All @@ -545,6 +549,26 @@ Has type `Option<PathBuf>`. Can be configured via environment variable `TELEMETR
null
```

### `telemetry.max_exponent`

The maximum exponent of 2 that is used for increasing delay between reconnections

Has type `u8`. Can be configured via environment variable `TELEMETRY_MAX_EXPONENT`

```json
4
```

### `telemetry.min_period`

The minimum period of time in seconds to wait before reconnecting

Has type `u64`. Can be configured via environment variable `TELEMETRY_MIN_PERIOD`

```json
1
```

### `telemetry.name`

The node's name to be seen on the telemetry
Expand Down
1 change: 1 addition & 0 deletions telemetry/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ build = "build.rs"
dev-telemetry = []

[dependencies]
async-trait = "0.1"
chrono = "0.4"
eyre = "0.6.5"
futures = { version = "0.3.17", default-features = false, features = ["std", "async-await"] }
Expand Down
32 changes: 31 additions & 1 deletion telemetry/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,12 @@ use iroha_config::derive::Configurable;
use serde::{Deserialize, Serialize};
use url::Url;

use crate::retry_period::RetryPeriod;

/// Configuration parameters container
#[derive(Clone, Default, Deserialize, Serialize, Debug, Configurable, PartialEq, Eq)]
#[derive(Clone, Deserialize, Serialize, Debug, Configurable, PartialEq, Eq)]
#[serde(rename_all = "UPPERCASE")]
#[serde(default)]
#[config(env_prefix = "TELEMETRY_")]
pub struct Configuration {
/// The node's name to be seen on the telemetry
Expand All @@ -16,8 +19,35 @@ pub struct Configuration {
/// The url of the telemetry, e.g., ws://127.0.0.1:8001/submit
#[config(serde_as_str)]
pub url: Option<Url>,
/// The minimum period of time in seconds to wait before reconnecting
#[serde(default = "default_min_period")]
pub min_period: u64,
/// The maximum exponent of 2 that is used for increasing delay between reconnections
#[serde(default = "default_max_exponent")]
pub max_exponent: u8,
/// The filepath that to write dev-telemetry to
#[cfg(feature = "dev-telemetry")]
#[config(serde_as_str)]
pub file: Option<PathBuf>,
}

KalitaAlexey marked this conversation as resolved.
Show resolved Hide resolved
impl Default for Configuration {
fn default() -> Self {
Self {
name: None,
url: None,
min_period: RetryPeriod::DEFAULT_MIN_PERIOD,
max_exponent: RetryPeriod::DEFAULT_MAX_EXPONENT,
#[cfg(feature = "dev-telemetry")]
file: None,
}
}
}

fn default_min_period() -> u64 {
RetryPeriod::DEFAULT_MIN_PERIOD
}

fn default_max_exponent() -> u8 {
RetryPeriod::DEFAULT_MAX_EXPONENT
}
1 change: 1 addition & 0 deletions telemetry/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ mod config;
#[cfg(feature = "dev-telemetry")]
pub mod dev;
pub mod futures;
mod retry_period;
pub mod ws;

pub use config::Configuration;
Expand Down
42 changes: 42 additions & 0 deletions telemetry/src/retry_period.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/// Encapsulates the retry period that is calculated as `min_period * 2 ^ min(exponent, max_exponent)`
pub struct RetryPeriod {
/// The minimum period
min_period: u64,
/// The maximum exponent
max_exponent: u8,
/// The current exponent
exponent: u8,
}

impl RetryPeriod {
pub const DEFAULT_MIN_PERIOD: u64 = 1;
pub const DEFAULT_MAX_EXPONENT: u8 = 4;

/// Constructs a new object
pub fn new(min_period: u64, max_exponent: u8) -> Self {
Self {
min_period,
max_exponent,
exponent: 0,
}
}

/// Increases the exponent if it isn't at its maximum
pub fn increase_exponent(&mut self) {
if self.exponent < self.max_exponent {
self.exponent += 1;
}
}

/// Returns the period
pub fn period(&mut self) -> u64 {
let mult = 2_u64.saturating_pow(self.exponent.into());
self.min_period.saturating_mul(mult)
}
}

impl Default for RetryPeriod {
fn default() -> Self {
Self::new(Self::DEFAULT_MIN_PERIOD, Self::DEFAULT_MAX_EXPONENT)
}
}
Loading