Skip to content

Commit babda3e

Browse files
committed
Refactor
1 parent bb3a535 commit babda3e

File tree

3 files changed

+22
-19
lines changed

3 files changed

+22
-19
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ edition = "2021"
77
anyhow = "1.0.98"
88
borsh = "0.9.3"
99
clap = { version = "4.5.39", features = ["derive", "env"] }
10+
futures = "0.3.31"
1011
hex = { version = "0.4.3", features = ["serde"] }
1112
prost = "0.14.1"
1213
reqwest = { version = "0.12.19", features = ["json"] }

src/main.rs

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ use {
66
api_client::{ApiClient, Observation},
77
borsh::BorshDeserialize,
88
clap::Parser,
9+
futures::future::join_all,
910
posted_message::PostedMessageUnreliableData,
1011
prost::Message,
1112
secp256k1::{rand::rngs::OsRng, Secp256k1},
@@ -147,27 +148,27 @@ async fn run_listener<T: Signer + 'static>(
147148
Err(_) => continue,
148149
};
149150

150-
input.api_clients.iter().for_each(|api_client| {
151-
tokio::spawn({
152-
let (unreliable_data, api_client, signer) = (
153-
unreliable_data.clone(),
154-
api_client.clone(),
155-
input.signer.clone()
156-
);
157-
async move {
158-
let body = message_data_to_body(&unreliable_data);
159-
match Observation::try_new(body.clone(), signer.clone()) {
160-
Ok(observation) => {
161-
if let Err(e) = api_client.post_observation(observation).await {
162-
tracing::error!(url = api_client.get_base_url().to_string(), error = ?e, "Failed to post observation");
163-
} else {
164-
tracing::info!(url = api_client.get_base_url().to_string(), "Observation posted successfully");
165-
};
166-
}
167-
Err(e) => tracing::error!(error = ?e, "Failed to create observation"),
151+
tokio::spawn({
152+
let (api_clients, signer) = (input.api_clients.clone(), input.signer.clone());
153+
async move {
154+
let body = message_data_to_body(&unreliable_data);
155+
match Observation::try_new(body.clone(), signer.clone()) {
156+
Ok(observation) => {
157+
join_all(api_clients.iter().map(|api_client| {
158+
let observation = observation.clone();
159+
let api_client = api_client.clone();
160+
async move {
161+
if let Err(e) = api_client.post_observation(observation).await {
162+
tracing::error!(url = api_client.get_base_url().to_string(), error = ?e, "Failed to post observation");
163+
} else {
164+
tracing::info!(url = api_client.get_base_url().to_string(), "Observation posted successfully");
165+
}
166+
}
167+
})).await;
168168
}
169+
Err(e) => tracing::error!(error = ?e, "Failed to create observation"),
169170
}
170-
});
171+
}
171172
});
172173
}
173174

0 commit comments

Comments
 (0)