Skip to content
This repository has been archived by the owner on Jul 16, 2021. It is now read-only.

Commit

Permalink
make client async
Browse files Browse the repository at this point in the history
  • Loading branch information
SebRollen committed Jun 17, 2021
1 parent 814085f commit c03385a
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 14 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ edition = "2018"

[dependencies]
bytes = "1.0.1"
tokio = { version = "1.7.0", default-features = false, features = ["net"] }
24 changes: 10 additions & 14 deletions src/client.rs
Original file line number Diff line number Diff line change
@@ -1,34 +1,30 @@
use crate::metric::Metric;
use std::io::Error;
use std::net::{ToSocketAddrs, UdpSocket};
use tokio::net::{ToSocketAddrs, UdpSocket};

pub struct Client<'a> {
socket: UdpSocket,
target: &'a str,
}

impl<'a> Client<'a> {
pub fn new<T: ToSocketAddrs>(host_address: &T, target_address: &'a str) -> Result<Self, Error> {
pub async fn new<T: ToSocketAddrs>(
host_address: &T,
target_address: &'a str,
) -> Result<Client<'a>, Error> {
Ok(Self {
socket: UdpSocket::bind(host_address)?,
socket: UdpSocket::bind(host_address).await?,
target: target_address,
})
}

pub fn send<I, T>(&self, metric: Metric<I, T>) -> Result<usize, Error>
pub async fn send<I, T>(&self, metric: Metric<'_, I, T>) -> Result<usize, Error>
where
I: IntoIterator<Item = T>,
T: AsRef<str>,
{
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
}
}

0 comments on commit c03385a

Please sign in to comment.