I am working on a rust application which will be sending data to server via HTTP. However the requests are being sent synchronously i.e before sending next request it waits for previous response. What I need to correct to send data asynchronously :-
pub struct Connector {
client: reqwest::r#async::Client,
}
impl Connector {
pub fn new() -> Self {
let client = reqwest::r#async::Client::new();
Self {
client,
}
}
pub fn send(&self, data: &PersonalData) -> impl Future<Item = (), Error = ()> {
let url = "http://localhost:9000/post";
println!("----DATA----");
println!("{:?}", data);
self.client.post(url)
.json(&data)
.send()
.map(|response| debug!("HTTP response: {:?}", response))
.map_err(|error| warn!("HTTP error: {:?}", error))
}
}
In the main code, on event trigger, I parse the URL out of event and pass this future to executor.
let connector = Arc::new(// connector instance);.
while (data) { // Event loop receiving data continuously.
let connector = connector.clone();
tokio::spawn(connector.send(data));
}