Skip to content

Commit

Permalink
optimize
Browse files Browse the repository at this point in the history
  • Loading branch information
lucky committed Nov 23, 2024
1 parent 6c0e3e0 commit 83d6c3a
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 9 deletions.
10 changes: 5 additions & 5 deletions src/collector.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
#[cfg(feature = "moex")]
use crate::source::moex;
use crate::{
config::Config,
graph,
source::{self, Config, Rate, RateType, Source},
source::{self, Rate, RateType, Source},
};
use rust_decimal::Decimal;
use std::sync::Arc;
Expand All @@ -13,7 +14,7 @@ use tokio::sync::mpsc;

pub async fn collect(
client: &reqwest::Client,
cfg: &Config,
cfg: Arc<Config>,
tx: mpsc::Sender<(Source, Vec<Rate>)>,
) {
#[cfg(feature = "moex")]
Expand All @@ -22,8 +23,7 @@ pub async fn collect(
.unwrap_or_default()
.is_empty()
});
let cfg = Arc::new(cfg.clone());
for src in Source::iter().filter(|v| cfg.is_enabled_for(*v)) {
for src in Source::iter().filter(|v| cfg.src.is_enabled_for(*v)) {
#[cfg(feature = "moex")]
if src == Source::MOEX && !MOEX_OK.clone() {
continue;
Expand All @@ -32,7 +32,7 @@ pub async fn collect(
let cfg = cfg.clone();
let tx = tx.clone();
tokio::spawn(async move {
let result = source::collect(&client, &cfg, src).await;
let result = source::collect(&client, &cfg.src, src).await;
match result {
Ok(rates) => {
let rates = rates
Expand Down
9 changes: 6 additions & 3 deletions src/generate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,10 @@ pub fn src_table(
mod tests {
use super::*;
use crate::{collector, config::Config};
use std::{sync::LazyLock, time::Duration};
use std::{
sync::{Arc, LazyLock},
time::Duration,
};
use strum::{EnumCount, IntoEnumIterator};
use tokio::sync::mpsc;

Expand Down Expand Up @@ -227,11 +230,11 @@ mod tests {
let mut result = HashMap::new();
let (tx, mut rx) = mpsc::channel(Source::COUNT);
let client = client.clone();
let cfg = CFG.clone();
let cfg = Arc::new(CFG.clone());
{
let tx = tx.clone();
tokio::spawn(async move {
collector::collect(&client, &cfg.src, tx).await;
collector::collect(&client, cfg, tx).await;
});
}
drop(tx);
Expand Down
2 changes: 1 addition & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ async fn collect_loop(db: Arc<Database>, cfg: Arc<Config>) -> anyhow::Result<()>
{
let tx = tx.clone();
tokio::spawn(async move {
collector::collect(&client, &cfg.src, tx).await;
collector::collect(&client, cfg, tx).await;
});
}
drop(tx);
Expand Down

0 comments on commit 83d6c3a

Please sign in to comment.