Skip to content

Commit

Permalink
Reconnect telemetry
Browse files Browse the repository at this point in the history
Signed-off-by: Alexey Kalita <kalita.alexey@outlook.com>
  • Loading branch information
KalitaAlexey committed Nov 8, 2021
1 parent ac0ad74 commit 7567334
Show file tree
Hide file tree
Showing 8 changed files with 510 additions and 96 deletions.
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.

2 changes: 1 addition & 1 deletion core/genesis.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"name": "wonderland",
"accounts": {},
"asset_definitions": {},
"metadata": {}
"metadata": {}
}
}
}
Expand Down
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::types::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>,
}

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 types;
pub mod ws;

pub use config::Configuration;
Expand Down
41 changes: 41 additions & 0 deletions telemetry/src/types.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/// 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 {
self.min_period + 2_u64.saturating_mul(self.exponent.into())
}
}

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

0 comments on commit 7567334

Please sign in to comment.