Put a semaphore for connection limit as a temporary hack

This commit is contained in:
Matteo Settenvini 2022-08-02 00:51:11 +02:00
parent 9ccec9878a
commit 04fadd3d89
Signed by: matteo
GPG key ID: 8576CC1AD97D42DF
3 changed files with 272 additions and 262 deletions

View file

@ -4,11 +4,12 @@
use {
crate::config::Config, crate::constants, anyhow::anyhow, anyhow::Result,
base64::write::EncoderWriter as Base64Encoder, chrono::DateTime, icalendar::CalendarComponent,
reqwest::Url, std::io::Write, std::ops::Range,
reqwest::Url, std::io::Write, std::ops::Range, std::sync::Arc, tokio::sync::Semaphore,
};
pub struct ApiClient {
rest: reqwest::Client,
pub(crate) rest_semaphore: Arc<Semaphore>, // TODO: wrap in dereferentiable struct
base_url: Url,
caldav_base_url: Url,
username: String,
@ -59,6 +60,7 @@ impl ApiClient {
caldav_base_url,
username: server.login_name.clone(),
rest: rest_client,
rest_semaphore: Arc::new(Semaphore::new(5)),
})
}

View file

@ -97,6 +97,7 @@ async fn get_all_recipes(api_client: &ApiClient) -> Result<HashMap<String, Rc<re
"Cannot fetch recipe {} with id {}",
rm.name, rm.id
));
response.json::<recipe::Recipe>().await.map(|r| Rc::new(r))
});
@ -134,7 +135,7 @@ where
&helpers::ical_escape_text(&ev.recipe.name)
);
log::info!(
let info_message = format!(
"Saving event at {} for '{}'",
&ev.ends_at.date(),
&ev.recipe.name
@ -157,13 +158,20 @@ where
1,
);
api_client
// TODO: wrap this
let _ = api_client.rest_semaphore.acquire().await.unwrap();
log::info!("{}", info_message);
let response = api_client
.rest()
.put(url)
.header("Content-Type", "text/calendar; charset=utf-8")
.body(cal_as_string)
.send()
.await
.await;
// TODO: magic numbers are bad...
std::thread::sleep(std::time::Duration::from_millis(300));
response
});
let responses = try_join_all(update_requests).await?;