Skip to content

Commit

Permalink
[rust] Include request timeout and improve error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
bonigarcia committed Nov 23, 2023
1 parent ee3cbb8 commit deed17c
Showing 1 changed file with 12 additions and 2 deletions.
14 changes: 12 additions & 2 deletions rust/src/stats.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,16 @@ use reqwest::header::CONTENT_TYPE;
use reqwest::header::USER_AGENT;
use reqwest::Client;
use serde::{Deserialize, Serialize};
use std::error::Error;
use std::time::Duration;

const PLAUSIBLE_URL: &str = "https://plausible.io/api/event";
const SM_USER_AGENT: &str = "Selenium Manager {}";
const APP_JSON: &str = "application/json";
const PAGE_VIEW: &str = "pageview";
const SELENIUM_DOMAIN: &str = "selenium.dev";
const SM_STATS_URL: &str = "https://{}/sm-stats";
const REQUEST_TIMEOUT_SEC: u64 = 3;

#[derive(Default, Serialize, Deserialize)]
pub struct Data {
Expand Down Expand Up @@ -65,10 +68,17 @@ pub async fn send_stats_to_plausible(http_client: &Client, props: Props, log: &L
.post(PLAUSIBLE_URL)
.header(USER_AGENT, user_agent)
.header(CONTENT_TYPE, APP_JSON)
.timeout(Duration::from_secs(REQUEST_TIMEOUT_SEC))
.body(body);

let handle = tokio::spawn(async move { request.send().await });
if let Err(err) = handle.await {
log.warn(format!("Error sending stats to {}: {}", PLAUSIBLE_URL, err));
match handle.await {
Ok(Err(err)) => log_warn(log, Box::new(err)),
Err(err) => log_warn(log, Box::new(err)),
_ => {}
}
}

fn log_warn(log: &Logger, err: Box<dyn Error>) {
log.warn(format!("Error sending stats to {}: {}", PLAUSIBLE_URL, err));
}

0 comments on commit deed17c

Please sign in to comment.