Skip to content

Commit

Permalink
Read blockchain timeouts from ENV (#317)
Browse files Browse the repository at this point in the history
* read timeouts from ENV

* update Rust version

* new version for release

Co-authored-by: sostrovskyi <sostrovskyi>
  • Loading branch information
awnion authored Jan 4, 2023
1 parent f83f555 commit 8123de5
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 8 deletions.
2 changes: 1 addition & 1 deletion git-remote-gosh/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ keywords = ['git-remote', 'git', 'gosh']
name = 'git-remote-gosh'
resolver = '2'
rust-version = "1.65"
version = '3.0.6'
version = '3.0.7'

[profile.profiling]
debug = 1
Expand Down
2 changes: 1 addition & 1 deletion git-remote-gosh/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# syntax=docker/dockerfile:1.4.2

# Base builder ---------------------------
FROM --platform=$BUILDPLATFORM rust:1.65-bullseye as rust-builder
FROM --platform=$BUILDPLATFORM rust:1.66-bullseye as rust-builder

WORKDIR /build

Expand Down
20 changes: 15 additions & 5 deletions git-remote-gosh/src/git_helper/ever_client.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
use crate::{blockchain::EverClient, config::Config};
use std::{env, sync::Arc};
use crate::{blockchain::EverClient, config::Config, utilities::env::env_var_as_duration_or};
use std::{env, sync::Arc, time::Duration};
use ton_client::{net::NetworkQueriesProtocol, ClientConfig, ClientContext};

// default timeout for all types of operation (e.g. message_processing, wait_for, query)
static DEFAULT_BLOCKCHAIN_TIMEOUT: Duration = Duration::from_secs(15 * 60);

#[instrument(level = "debug")]
pub fn create_client(config: &Config, network: &str) -> anyhow::Result<EverClient> {
let endpoints = config
Expand All @@ -11,6 +14,13 @@ pub fn create_client(config: &Config, network: &str) -> anyhow::Result<EverClien
.unwrap_or_else(|_| ".git".to_string())
.to_lowercase();

let blockchain_timeout =
env_var_as_duration_or("GOSH_BLOCKCHAIN_TIMEOUT_SEC", DEFAULT_BLOCKCHAIN_TIMEOUT)?;
let message_processing_timeout =
env_var_as_duration_or("GOSH_MESSAGE_PROCESSING_TIMEOUT_SEC", blockchain_timeout)?;
let wait_for_timeout = env_var_as_duration_or("GOSH_WAIT_FOR_TIMEOUT_SEC", blockchain_timeout)?;
let query_timeout = env_var_as_duration_or("GOSH_QUERY_TIMEOUT_SEC", blockchain_timeout)?;

let config = ClientConfig {
network: ton_client::net::NetworkConfig {
sending_endpoint_count: endpoints.len() as u8,
Expand All @@ -26,9 +36,9 @@ pub fn create_client(config: &Config, network: &str) -> anyhow::Result<EverClien
},
network_retries_count: 5,
message_retries_count: 10,
message_processing_timeout: 220000000,
wait_for_timeout: 220000000,
query_timeout: 220000000,
message_processing_timeout: message_processing_timeout.as_millis().try_into()?,
wait_for_timeout: wait_for_timeout.as_millis().try_into()?,
query_timeout: query_timeout.as_millis().try_into()?,
..Default::default()
},
..Default::default()
Expand Down
28 changes: 28 additions & 0 deletions git-remote-gosh/src/utilities/env.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
use std::{env, ffi::OsStr, time::Duration};

fn parse_duration(raw_str: impl AsRef<str>) -> anyhow::Result<Duration> {
raw_str
.as_ref()
.parse::<f64>()
.map_err(anyhow::Error::from)
.map(Duration::try_from_secs_f64)?
.map_err(anyhow::Error::from)
}

/// Parses [`Duration`] from env var or returns default if env var isn't set
///
/// Respects `Err` context in case of parsing/validation error
pub fn env_var_as_duration_or(
key: impl AsRef<OsStr>,
default: Duration,
) -> anyhow::Result<Duration> {
match env::var(&key) {
Err(_) => Ok(default),
Ok(raw_str) => parse_duration(raw_str).map_err(|err| {
err.context(format!(
"env {} can't be parsed as Duration (try positive float)",
key.as_ref().to_string_lossy()
))
}),
}
}
1 change: 1 addition & 0 deletions git-remote-gosh/src/utilities/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
pub mod env;
use crate::{blockchain::BlockchainContractAddress, config::Config};

#[derive(Debug, Clone)]
Expand Down
2 changes: 1 addition & 1 deletion version.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{
"version": "3.0.6"
"version": "3.0.7"
}

0 comments on commit 8123de5

Please sign in to comment.