Skip to content

Commit

Permalink
refactor(blocking): use sync sender
Browse files Browse the repository at this point in the history
  • Loading branch information
nikhil-prabhu committed Jan 2, 2025
1 parent cf6dea2 commit d70b692
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 13 deletions.
6 changes: 3 additions & 3 deletions src/blocking/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
pub(crate) mod providers;

use std::sync::mpsc::Sender;
use std::sync::mpsc::SyncSender;
use std::sync::{mpsc, Arc, LazyLock, Mutex};
use std::time::Duration;

Expand All @@ -13,7 +13,7 @@ use crate::{ProviderId, DEFAULT_DETECTION_TIMEOUT};
#[allow(dead_code)]
pub(crate) trait Provider: Send + Sync {
fn identifier(&self) -> ProviderId;
fn identify(&self, tx: Sender<ProviderId>, timeout: Duration);
fn identify(&self, tx: SyncSender<ProviderId>, timeout: Duration);
}

type P = Arc<dyn Provider>;
Expand Down Expand Up @@ -51,7 +51,7 @@ pub fn supported_providers() -> Result<Vec<String>> {

pub fn detect(timeout: Option<u64>) -> Result<ProviderId> {
let timeout = Duration::from_secs(timeout.unwrap_or(DEFAULT_DETECTION_TIMEOUT));
let (tx, rx) = mpsc::channel::<ProviderId>();
let (tx, rx) = mpsc::sync_channel::<ProviderId>(1);
let guard = PROVIDERS
.lock()
.map_err(|_| anyhow::anyhow!("Error locking providers"))?;
Expand Down
4 changes: 2 additions & 2 deletions src/blocking/providers/alibaba.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
use std::fs;
use std::path::Path;
use std::sync::mpsc::Sender;
use std::sync::mpsc::SyncSender;
use std::time::Duration;

use tracing::{debug, error, info, instrument};
Expand All @@ -23,7 +23,7 @@ impl Provider for Alibaba {
}

#[instrument(skip_all)]
fn identify(&self, tx: Sender<ProviderId>, timeout: Duration) {
fn identify(&self, tx: SyncSender<ProviderId>, timeout: Duration) {
info!("Checking Alibaba Cloud");
if self.check_vendor_file(VENDOR_FILE) || self.check_metadata_server(METADATA_URI, timeout)
{
Expand Down
4 changes: 2 additions & 2 deletions src/blocking/providers/aws.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//! Amazon Web Services (AWS).
use std::path::Path;
use std::sync::mpsc::Sender;
use std::sync::mpsc::SyncSender;
use std::time::Duration;

use reqwest::blocking::Client;
Expand Down Expand Up @@ -33,7 +33,7 @@ impl Provider for Aws {
IDENTIFIER
}

fn identify(&self, tx: Sender<ProviderId>, timeout: Duration) {
fn identify(&self, tx: SyncSender<ProviderId>, timeout: Duration) {
info!("Checking Amazon Web Services");
if self.check_product_version_file(PRODUCT_VERSION_FILE)
|| self.check_bios_vendor_file(BIOS_VENDOR_FILE)
Expand Down
8 changes: 2 additions & 6 deletions src/blocking/providers/azure.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
use std::fs;
use std::path::Path;
use std::sync::mpsc::Sender;
use std::sync::mpsc::SyncSender;
use std::time::Duration;

use reqwest::blocking::Client;
Expand All @@ -12,13 +12,9 @@ use tracing::{debug, error, info, instrument};
use crate::blocking::Provider;
use crate::ProviderId;

#[allow(unused)]
const METADATA_URI: &str = "http://169.254.169.254";
#[allow(unused)]
const METADATA_PATH: &str = "/metadata/instance?api-version=2017-12-01";
#[allow(unused)]
const VENDOR_FILE: &str = "/sys/class/dmi/id/sys_vendor";
#[allow(unused)]
const IDENTIFIER: ProviderId = ProviderId::Azure;

#[derive(Serialize, Deserialize)]
Expand All @@ -40,7 +36,7 @@ impl Provider for Azure {
}

#[instrument(skip_all)]
fn identify(&self, tx: Sender<ProviderId>, timeout: Duration) {
fn identify(&self, tx: SyncSender<ProviderId>, timeout: Duration) {
info!("Checking Microsoft Azure");
if self.check_vendor_file(VENDOR_FILE) || self.check_metadata_server(METADATA_URI, timeout)
{
Expand Down

0 comments on commit d70b692

Please sign in to comment.