From db0bb2fd34d8dbb96b39af0d3b7fd5d97a64a752 Mon Sep 17 00:00:00 2001 From: Erik Moqvist Date: Wed, 21 Dec 2022 23:25:20 +0100 Subject: [PATCH] Optional request timeout --- appinsights/src/channel/memory.rs | 2 +- appinsights/src/config.rs | 17 +++++++++++++++++ appinsights/src/transmitter.rs | 12 ++++++++---- 3 files changed, 26 insertions(+), 5 deletions(-) diff --git a/appinsights/src/channel/memory.rs b/appinsights/src/channel/memory.rs index e3ff032..40d00c5 100644 --- a/appinsights/src/channel/memory.rs +++ b/appinsights/src/channel/memory.rs @@ -27,7 +27,7 @@ impl InMemoryChannel { let (command_sender, command_receiver) = futures_channel::mpsc::unbounded(); let worker = Worker::new( - Transmitter::new(config.endpoint()), + Transmitter::new(config.endpoint(), config.request_timeout()), items.clone(), command_receiver, config.interval(), diff --git a/appinsights/src/config.rs b/appinsights/src/config.rs index 7d25f4c..2bd1c3a 100644 --- a/appinsights/src/config.rs +++ b/appinsights/src/config.rs @@ -30,6 +30,9 @@ pub struct TelemetryConfig { /// Maximum time to wait until send a batch of telemetry. interval: Duration, + + /// Maximum time from connecting to response body has been received. + request_timeout: Option, } impl TelemetryConfig { @@ -57,6 +60,11 @@ impl TelemetryConfig { pub fn interval(&self) -> Duration { self.interval } + + /// Returns maximum time from connecting to response body has been received. + pub fn request_timeout(&self) -> Option { + self.request_timeout + } } /// Constructs a new instance of a [`TelemetryConfig`](struct.TelemetryConfig.html) with required @@ -74,6 +82,7 @@ impl DefaultTelemetryConfigBuilder { i_key: i_key.into(), endpoint: "https://dc.services.visualstudio.com/v2/track".into(), interval: Duration::from_secs(2), + request_timeout: None, } } } @@ -83,6 +92,7 @@ pub struct TelemetryConfigBuilder { i_key: String, endpoint: String, interval: Duration, + request_timeout: Option, } impl TelemetryConfigBuilder { @@ -110,12 +120,19 @@ impl TelemetryConfigBuilder { self } + /// Initializes a builder with a maximum time from connecting to response body has been received. + pub fn request_timeout(mut self, request_timeout: Option) -> Self { + self.request_timeout = request_timeout; + self + } + /// Constructs a new instance of a [`TelemetryConfig`](struct.TelemetryConfig.html) with custom settings. pub fn build(self) -> TelemetryConfig { TelemetryConfig { i_key: self.i_key, endpoint: self.endpoint, interval: self.interval, + request_timeout: self.request_timeout, } } } diff --git a/appinsights/src/transmitter.rs b/appinsights/src/transmitter.rs index c6bc910..2c23be1 100644 --- a/appinsights/src/transmitter.rs +++ b/appinsights/src/transmitter.rs @@ -1,7 +1,8 @@ +use std::time::Duration; use chrono::{DateTime, Utc}; use http::{header::RETRY_AFTER, StatusCode}; use log::debug; -use reqwest::Client; +use reqwest::{Client, ClientBuilder}; use crate::{ contracts::{Envelope, Transmission, TransmissionItem}, @@ -24,11 +25,14 @@ pub struct Transmitter { impl Transmitter { /// Creates a new instance of telemetry items sender. - pub fn new(url: &str) -> Self { - let client = Client::new(); + pub fn new(url: &str, request_timeout: Option) -> Self { + let mut client_builder = ClientBuilder::new(); + if let Some(request_timeout) = request_timeout { + client_builder = client_builder.timeout(request_timeout); + } Self { url: url.into(), - client, + client: client_builder.build().unwrap_or(Client::new()), } }