Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Retry bad nonce errors with a new nonce #65

Merged
merged 1 commit into from
Sep 17, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -490,6 +490,43 @@ impl Client {
}

async fn post(
&self,
payload: Option<&impl Serialize>,
mut nonce: Option<String>,
signer: &impl Signer,
url: &str,
) -> Result<BytesResponse, Error> {
let mut retries = 3;
loop {
let mut response = self
.post_attempt(payload, nonce.clone(), signer, url)
.await?;
if response.parts.status != StatusCode::BAD_REQUEST {
return Ok(response);
}
let body = response.body.into_bytes().await.map_err(Error::Other)?;
let problem = serde_json::from_slice::<Problem>(&body)?;
if let Some("urn:ietf:params:acme:error:badNonce") = problem.r#type.as_deref() {
retries -= 1;
if retries != 0 {
// Retrieve the new nonce. If it isn't there (it
// should be, the spec requires it) then we will
// manually refresh a new one in `post_attempt`
// due to `nonce` being `None` but getting it from
// the response saves us making that request.
nonce = nonce_from_response(&response);
continue;
}
}

return Ok(BytesResponse {
parts: response.parts,
body: Box::new(body),
});
}
}

async fn post_attempt(
&self,
payload: Option<&impl Serialize>,
nonce: Option<String>,
Expand Down Expand Up @@ -789,6 +826,13 @@ where
}
}

#[async_trait]
impl BytesBody for Bytes {
async fn into_bytes(&mut self) -> Result<Bytes, Box<dyn StdError + Send + Sync + 'static>> {
Ok(self.to_owned())
}
}

/// Object safe body trait
#[async_trait]
pub trait BytesBody: Send {
Expand Down