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

Optional request timeout #312

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
2 changes: 1 addition & 1 deletion appinsights/src/channel/memory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down
17 changes: 17 additions & 0 deletions appinsights/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Duration>,
}

impl TelemetryConfig {
Expand Down Expand Up @@ -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<Duration> {
self.request_timeout
}
}

/// Constructs a new instance of a [`TelemetryConfig`](struct.TelemetryConfig.html) with required
Expand All @@ -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,
}
}
}
Expand All @@ -83,6 +92,7 @@ pub struct TelemetryConfigBuilder {
i_key: String,
endpoint: String,
interval: Duration,
request_timeout: Option<Duration>,
}

impl TelemetryConfigBuilder {
Expand Down Expand Up @@ -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<Duration>) -> 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,
}
}
}
Expand Down
12 changes: 8 additions & 4 deletions appinsights/src/transmitter.rs
Original file line number Diff line number Diff line change
@@ -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},
Expand All @@ -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<Duration>) -> 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()),
}
}

Expand Down