tustvold commented on code in PR #7183:
URL: https://github.com/apache/arrow-rs/pull/7183#discussion_r1967366857
##########
object_store/src/client/retry.rs:
##########
@@ -284,164 +334,97 @@ impl RetryableRequest {
return Ok(r);
}
- let status = r.status();
- let headers = r.headers().clone();
-
- let bytes = r.bytes().await.map_err(|e| Error::Reqwest
{
- retries,
- max_retries,
- elapsed: now.elapsed(),
- retry_timeout,
- source: e,
- })?;
-
- let response_body = String::from_utf8_lossy(&bytes);
- debug!("Checking for error in response_body: {}",
response_body);
+ let (parts, body) = r.into_parts();
+ let body = match body.text().await {
+ Ok(body) => body,
+ Err(e) => return
Err(ctx.err(RequestError::Http(e))),
+ };
- if !body_contains_error(&response_body) {
+ if !body_contains_error(&body) {
// Success response and no error, clone and return
response
- let mut success_response =
hyper::Response::new(bytes);
- *success_response.status_mut() = status;
- *success_response.headers_mut() = headers;
-
- return
Ok(reqwest::Response::from(success_response));
+ return Ok(HttpResponse::from_parts(parts,
body.into()));
} else {
// Retry as if this was a 5xx response
- if retries == max_retries || now.elapsed() >
retry_timeout {
- return Err(Error::Server {
- body: Some(response_body.into_owned()),
- status,
- });
+ if ctx.exhausted() {
+ return Err(ctx.err(RequestError::Response {
body, status }));
}
let sleep = backoff.next();
- retries += 1;
+ ctx.retries += 1;
info!(
"Encountered a response status of {} but body
contains Error, backing off for {} seconds, retry {} of {}",
status,
sleep.as_secs_f32(),
- retries,
- max_retries,
+ ctx.retries,
+ ctx.max_retries,
);
tokio::time::sleep(sleep).await;
}
- }
- Ok(r) if r.status() == StatusCode::NOT_MODIFIED => {
- return Err(Error::Client {
- body: None,
- status: StatusCode::NOT_MODIFIED,
- })
- }
- Ok(r) => {
- let is_bare_redirect =
- r.status().is_redirection() &&
!r.headers().contains_key(LOCATION);
+ } else if status == StatusCode::NOT_MODIFIED {
+ return Err(ctx.err(RequestError::Status { status,
body: None }));
+ } else if status.is_redirection() {
+ let is_bare_redirect =
!r.headers().contains_key(LOCATION);
return match is_bare_redirect {
- true => Err(Error::BareRedirect),
- // Not actually sure if this is reachable, but
here for completeness
- false => Err(Error::Client {
+ true => Err(ctx.err(RequestError::BareRedirect)),
+ false => Err(ctx.err(RequestError::Status {
body: None,
status: r.status(),
- }),
+ })),
};
- }
- Err(e) => {
- let e = sanitize_err(e);
+ } else {
let status = r.status();
- if retries == max_retries
- || now.elapsed() > retry_timeout
+ if ctx.exhausted()
|| !(status.is_server_error()
|| (self.retry_on_conflict && status ==
StatusCode::CONFLICT))
{
- return Err(match status.is_client_error() {
- true => match r.text().await {
- Ok(body) => Error::Client {
- body: Some(body).filter(|b|
!b.is_empty()),
+ let source = match status.is_client_error() {
+ true => match r.into_body().text().await {
+ Ok(body) => RequestError::Status {
status,
+ body: Some(body),
},
- Err(e) => Error::Reqwest {
- retries,
- max_retries,
- elapsed: now.elapsed(),
- retry_timeout,
- source: e,
- },
+ Err(e) => RequestError::Http(e),
},
- false => Error::Reqwest {
- retries,
- max_retries,
- elapsed: now.elapsed(),
- retry_timeout,
- source: e,
- },
- });
- }
+ false => RequestError::Status { status, body:
None },
+ };
+ return Err(ctx.err(source));
+ };
let sleep = backoff.next();
- retries += 1;
+ ctx.retries += 1;
info!(
- "Encountered server error, backing off for {}
seconds, retry {} of {}: {}",
+ "Encountered server error, backing off for {}
seconds, retry {} of {}",
sleep.as_secs_f32(),
- retries,
- max_retries,
- e,
+ ctx.retries,
+ ctx.max_retries,
);
tokio::time::sleep(sleep).await;
}
- },
+ }
Err(e) => {
- let e = sanitize_err(e);
-
- let mut do_retry = false;
- if e.is_connect()
- || e.is_body()
- || (e.is_request() && !e.is_timeout())
- || (is_idempotent && e.is_timeout())
+ // let e = sanitize_err(e);
+
+ let do_retry = match e.kind() {
+ HttpErrorKind::Connect | HttpErrorKind::Request =>
true, // Request not sent, can retry
+ HttpErrorKind::Timeout => is_idempotent,
+ HttpErrorKind::Unknown
+ | HttpErrorKind::Interrupted
Review Comment:
Note: I think we should retry Interrupted if request is_idempotent
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]