diff --git a/Cargo.toml b/Cargo.toml index cd63b64..2ed41ff 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -8,3 +8,4 @@ edition = "2018" [dependencies] bytes = "1.0.1" +tokio = { version = "1.7.0", default-features = false, features = ["net"] } diff --git a/src/client.rs b/src/client.rs index 901e0bd..9671cac 100644 --- a/src/client.rs +++ b/src/client.rs @@ -1,6 +1,6 @@ use crate::metric::Metric; use std::io::Error; -use std::net::{ToSocketAddrs, UdpSocket}; +use tokio::net::{ToSocketAddrs, UdpSocket}; pub struct Client<'a> { socket: UdpSocket, @@ -8,27 +8,23 @@ pub struct Client<'a> { } impl<'a> Client<'a> { - pub fn new(host_address: &T, target_address: &'a str) -> Result { + pub async fn new( + host_address: &T, + target_address: &'a str, + ) -> Result, Error> { Ok(Self { - socket: UdpSocket::bind(host_address)?, + socket: UdpSocket::bind(host_address).await?, target: target_address, }) } - pub fn send(&self, metric: Metric) -> Result + pub async fn send(&self, metric: Metric<'_, I, T>) -> Result where I: IntoIterator, T: AsRef, { - self.socket.send_to(metric.to_bytes().as_ref(), self.target) - } -} - -impl Default for Client<'_> { - fn default() -> Self { - Self { - socket: UdpSocket::bind("127.0.0.1:0").unwrap(), - target: "127.0.0.1:8125", - } + self.socket + .send_to(metric.to_bytes().as_ref(), self.target) + .await } }