From 0448980dd9e24b1e0f1e5d80f02a0fa05a86b375 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20Christian=20Gr=C3=BCnhage?= Date: Wed, 8 Apr 2020 12:01:19 +0200 Subject: [PATCH] fix interval timings for concurrent pings --- CHANGELOG.md | 2 ++ src/ping.rs | 17 ++++++----------- 2 files changed, 8 insertions(+), 11 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 57baa85..ce5c7ca 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,8 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). ## [Unreleased] +### Fixed +- fix interval timings concurrent pings ## [v0.2.2] - 2020-04-06 ### Added diff --git a/src/ping.rs b/src/ping.rs index 0ecf05c..565d1ca 100644 --- a/src/ping.rs +++ b/src/ping.rs @@ -18,13 +18,12 @@ * along with this program. If not, see . * ********************************************************************************/ use crate::config::Config; -use futures_util::stream::StreamExt; use lazy_static::lazy_static; use log::{error, info, trace}; use prometheus::*; use std::net::IpAddr; use std::time::Duration; -use tokio_ping::Pinger; +use tokio_ping::{PingFuture, Pinger}; lazy_static! { static ref PING_HISTOGRAM: HistogramVec = register_histogram_vec!( @@ -58,21 +57,17 @@ pub(crate) async fn start_pinging_hosts( } async fn ping_host(pinger: Pinger, host: IpAddr, interval: u64) { - let pingchain = pinger.chain(host).timeout(Duration::from_secs(3)); - let host = host.to_string(); - let mut stream = pingchain.stream(); + let mut pingchain = pinger.chain(host).timeout(Duration::from_secs(3)); let mut interval = tokio::time::interval(Duration::from_millis(interval)); + let host_string = host.to_string(); loop { interval.tick().await; - handle_ping_result(stream.next().await.unwrap(), &host).await; + tokio::spawn(handle_ping_result(pingchain.send(), host_string.clone())); } } -async fn handle_ping_result( - result: std::result::Result, tokio_ping::Error>, - host: &str, -) { - let pong = match result { +async fn handle_ping_result(result: PingFuture, host: String) { + let pong = match result.await { Ok(pong) => pong, Err(error) => { error!("Couldn't ping {}: {}", &host, error);