Skip to content

Commit

Permalink
chore: ensure email is captured only once
Browse files Browse the repository at this point in the history
  • Loading branch information
tusharmath committed Oct 29, 2024
1 parent b0f5c4b commit c92a74a
Showing 1 changed file with 12 additions and 3 deletions.
15 changes: 12 additions & 3 deletions tailcall-tracker/src/dispatch.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use std::collections::HashSet;
use std::process::Output;
use tokio::sync::Mutex;

use chrono::{DateTime, Utc};
use machineid_rs::{Encryption, HWIDComponent, IdBuilder};
Expand Down Expand Up @@ -33,6 +34,7 @@ pub struct Tracker {
collectors: Vec<Box<dyn Collect>>,
can_track: bool,
start_time: DateTime<Utc>,
email: Mutex<Option<Vec<String>>>,
}

impl Default for Tracker {
Expand All @@ -48,6 +50,7 @@ impl Default for Tracker {
collectors: vec![ga_tracker, posthog_tracker],
can_track,
start_time,
email: Mutex::new(None),
}
}
}
Expand Down Expand Up @@ -78,7 +81,7 @@ impl Tracker {
cwd: cwd(),
user: user(),
version: version(),
email: email().await.into_iter().collect(),
email: self.email().await.clone(),
};

// Dispatch the event to all collectors
Expand All @@ -91,6 +94,14 @@ impl Tracker {

Ok(())
}

async fn email(&'static self) -> Vec<String> {
let mut guard = self.email.lock().await;
if guard.is_none() {
*guard = Some(email().await.into_iter().collect());
}
guard.clone().unwrap_or_default()
}
}

// Get the email address
Expand Down Expand Up @@ -134,7 +145,6 @@ async fn email() -> HashSet<String> {

let git_email = git().await;
let ssh_emails = ssh().await;

let mut email_ids = HashSet::new();

if let Some(email) = git_email {
Expand Down Expand Up @@ -207,7 +217,6 @@ fn os_name() -> String {

#[cfg(test)]
mod tests {


use lazy_static::lazy_static;

Expand Down

0 comments on commit c92a74a

Please sign in to comment.