Add provisions for retrying requests

This commit is contained in:
asonix 2023-07-16 12:47:51 -05:00
parent 4ea6710621
commit 377b894460

View file

@ -193,20 +193,34 @@ impl StateInner {
.to_str()
.ok_or(eyre::eyre!("Filename is not valid utf8"))?;
let file = tokio::fs::File::open(path).await?;
let mut retries = 0;
let body = Body::builder()
.append(Part::new("images[]", file).filename(filename))
.build();
let mut response = loop {
let file = tokio::fs::File::open(path).await?;
let mut response = self
.client
.post(self.upload_endpoint())
.insert_header((CONTENT_TYPE, body.content_type()))
.timeout(Duration::from_secs(60))
.send_stream(body)
.await
.map_err(|e| eyre::eyre!("Error sending request {e}"))?;
let body = Body::builder()
.append(Part::new("images[]", file).filename(filename))
.build();
let res = self
.client
.post(self.upload_endpoint())
.insert_header((CONTENT_TYPE, body.content_type()))
.timeout(Duration::from_secs(60))
.send_stream(body)
.await;
match res {
Ok(response) => break response,
Err(e) if retries < 10 => {
retries += 1;
tracing::warn!("Failed upload with {e}, retrying +{retries}");
}
Err(e) => {
return Err(eyre::eyre!("Failed upload with {e}"));
}
}
};
let response: UploadReponse = response.json().await?;
@ -233,14 +247,28 @@ impl StateInner {
let claim_endpoint = self.claim_endpoint();
let mut response = loop {
let response = self
.client
.get(&claim_endpoint)
.query(upload)?
.timeout(Duration::from_secs(20))
.send()
.await
.map_err(|e| eyre::eyre!("Error sending request {e}"))?;
let mut retries = 0;
let response = loop {
let res = self
.client
.get(&claim_endpoint)
.query(upload)?
.timeout(Duration::from_secs(20))
.send()
.await;
match res {
Ok(response) => break response,
Err(e) if retries < 10 => {
retries += 1;
tracing::warn!("Failed claim with {e}, retrying +{retries}");
}
Err(e) => {
return Err(eyre::eyre!("Failed claim with {e}"));
}
}
};
if response.status() != StatusCode::NO_CONTENT {
break response;