all lists on lists.proxmox.com
 help / color / mirror / Atom feed
From: Christian Ebner <c.ebner@proxmox.com>
To: pbs-devel@lists.proxmox.com
Subject: [PATCH proxmox v3 03/11] s3-client: detect throttling request from the response body on error
Date: Thu, 12 Nov 2026 19:01:11 +0100	[thread overview]
Message-ID: <20261112180119.660923-4-c.ebner@proxmox.com> (raw)
In-Reply-To: <20261112180119.660923-1-c.ebner@proxmox.com>

AWS S3 signals throttling with a service unavailable response and the
SlowDown error code, which is now classified as transient errors
since the previous change introducing this distinction and therefore
retried after the short backoff of a transient error.

Follow the AWS SDKs implementation guide on this and try to identify
retryable errors by the error code of the response body if present
instead, the body will be discarded on retry anyways.
Using the longer throttling backoff delay for retries introduced by
the previous change for the known throttling codes.

Other S3 compatible providers do use similar codes to signal rate
limiting [0].

[0] https://forum.proxmox.com/threads/184371/post-858173
[1] https://forum.proxmox.com/threads/176950/

Link: https://docs.aws.amazon.com/sdkref/latest/guide/feature-retry-behavior.html
Signed-off-by: Christian Ebner <c.ebner@proxmox.com>
---
 proxmox-s3-client/src/client.rs          | 71 ++++++++++++++++++++++++
 proxmox-s3-client/src/response_reader.rs | 36 ++++++++++++
 2 files changed, 107 insertions(+)

diff --git a/proxmox-s3-client/src/client.rs b/proxmox-s3-client/src/client.rs
index 677557f9..c43d0973 100644
--- a/proxmox-s3-client/src/client.rs
+++ b/proxmox-s3-client/src/client.rs
@@ -5,6 +5,7 @@ use std::sync::{Arc, LazyLock, Mutex, RwLock};
 use std::time::{Duration, Instant};
 
 use anyhow::{Context, Error, bail, format_err};
+use http_body_util::BodyExt;
 use hyper::body::{Bytes, Incoming};
 use hyper::http::method::Method;
 use hyper::http::uri::{Authority, Parts, PathAndQuery, Scheme};
@@ -52,6 +53,23 @@ const MAX_S3_HTTP_REQUEST_RETRY: usize = 3;
 const S3_RETRY_BASE_DELAY_TRANSIENT: Duration = Duration::from_millis(50);
 const S3_RETRY_BASE_DELAY_THROTTLING: Duration = Duration::from_secs(1);
 const S3_RETRY_MAX_BACKOFF: Duration = Duration::from_secs(20);
+// Response body error codes interpreted as retry with throttled delay instead of transient delay.
+const S3_THROTTLING_ERROR_CODES: &[&str] = &[
+    "Throttling",
+    "ThrottlingException",
+    "ThrottledException",
+    "RequestThrottledException",
+    "TooManyRequestsException",
+    "ProvisionedThroughputExceededException",
+    "LimitExceededException",
+    "PriorRequestNotComplete",
+    "RequestThrottled",
+    "RequestLimitExceeded",
+    "SlowDown",
+    "BandwidthLimitExceeded",
+];
+// Maximum time to wait when reading an error response body to check for throttling codes.
+const S3_ERROR_BODY_READ_TIMEOUT: Duration = Duration::from_secs(5);
 
 static SHARED_COUNTER_FLUSHER: LazyLock<RwLock<MmapFlusher>> =
     LazyLock::new(|| RwLock::new(MmapFlusher::new()));
@@ -540,6 +558,28 @@ impl S3Client {
                 return Ok(response?);
             };
 
+            // The response is discarded on retry, so use it to check the error code to detect
+            // throttling signaled with a server error status.
+            let retry_reason = match response {
+                Ok(response) if retry_reason == RetryReason::Transient => {
+                    let response_body = response.into_body().collect();
+                    let read_deadline = tokio::time::Instant::now() + S3_ERROR_BODY_READ_TIMEOUT;
+                    let body_deadline = match deadline {
+                        Some(deadline) => read_deadline.min(deadline),
+                        None => read_deadline,
+                    };
+
+                    if let Ok(Ok(body)) =
+                        tokio::time::timeout_at(body_deadline, response_body).await
+                    {
+                        RetryReason::from_error_response_body(&body.to_bytes())
+                    } else {
+                        RetryReason::Transient
+                    }
+                }
+                _ => retry_reason,
+            };
+
             tokio::time::sleep(retry_backoff(retry_reason, retry)).await;
         }
 
@@ -1037,6 +1077,16 @@ enum RetryReason {
 }
 
 impl RetryReason {
+    fn from_error_response_body(body: &[u8]) -> Self {
+        if let Some(error_code) = crate::response_reader::parse_error_code(body)
+            && S3_THROTTLING_ERROR_CODES.contains(&error_code.as_str())
+        {
+            return Self::Throttling;
+        }
+
+        Self::Transient
+    }
+
     fn base_delay(self) -> Duration {
         match self {
             Self::Transient => S3_RETRY_BASE_DELAY_TRANSIENT,
@@ -1083,6 +1133,27 @@ mod tests {
         );
     }
 
+    #[test]
+    fn retry_reason_from_error_code() {
+        let error_body = |code: &str| format!("<Error><Code>{code}</Code></Error>");
+        assert_eq!(
+            RetryReason::from_error_response_body(error_body("SlowDown").as_bytes()),
+            RetryReason::Throttling
+        );
+        assert_eq!(
+            RetryReason::from_error_response_body(error_body("InternalError").as_bytes()),
+            RetryReason::Transient
+        );
+        assert_eq!(
+            RetryReason::from_error_response_body(b""),
+            RetryReason::Transient
+        );
+        assert_eq!(
+            RetryReason::from_error_response_body(b"<html>Service Unavailable</html>"),
+            RetryReason::Transient
+        );
+    }
+
     #[test]
     fn retry_backoff_is_capped() {
         assert_eq!(
diff --git a/proxmox-s3-client/src/response_reader.rs b/proxmox-s3-client/src/response_reader.rs
index 1a3a9add..f941aaa2 100644
--- a/proxmox-s3-client/src/response_reader.rs
+++ b/proxmox-s3-client/src/response_reader.rs
@@ -605,6 +605,42 @@ impl ResponseReader {
     }
 }
 
+#[derive(Deserialize)]
+#[serde(rename_all = "PascalCase")]
+/// Common error response body of S3 api calls, only the error code is of interest.
+/// https://docs.aws.amazon.com/AmazonS3/latest/API/ErrorResponses.html#RESTErrorResponses
+struct ErrorResponseBody {
+    code: Option<String>,
+}
+
+/// Parse the error code from an S3 api error response body, if any.
+pub(crate) fn parse_error_code(body: &[u8]) -> Option<String> {
+    let body = std::str::from_utf8(body).ok()?;
+    let error: ErrorResponseBody = serde_xml_rs::from_str(body).ok()?;
+    error.code
+}
+
+#[test]
+fn parse_error_code_test() {
+    let response_body = r#"<?xml version="1.0" encoding="UTF-8"?>
+        <Error>
+            <Code>SlowDown</Code>
+            <Message>Please reduce your request rate.</Message>
+            <RequestId>4442587FB7D0A2F9</RequestId>
+        </Error>"#;
+    assert_eq!(
+        parse_error_code(response_body.as_bytes()),
+        Some("SlowDown".to_string())
+    );
+    assert_eq!(
+        parse_error_code(b"<Error><Message>no code</Message></Error>"),
+        None
+    );
+    assert_eq!(parse_error_code(b""), None);
+    assert_eq!(parse_error_code(b"<html>Bad Gateway"), None);
+    assert_eq!(parse_error_code(&[0xff, 0xfe]), None);
+}
+
 #[test]
 fn parse_list_objects_v2_response_test() {
     let response_body = r#"<?xml version="1.0" encoding="UTF-8"?>
-- 
2.47.3





  parent reply	other threads:[~2026-11-12 18:02 UTC|newest]

Thread overview: 14+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-12 18:01 [PATCH proxmox{,-backup} v3 00/11] fix #7606: verify: s3: handle transient fetch errors and add retry Christian Ebner
2026-11-12 18:01 ` [PATCH proxmox v3 01/11] s3-client: also retry requests on bad gateway responses Christian Ebner
2026-11-12 18:01 ` [PATCH proxmox v3 02/11] s3-client: distinguish transient errors and throttled requests Christian Ebner
2026-11-12 18:01 ` Christian Ebner [this message]
2026-11-12 18:01 ` [PATCH proxmox v3 04/11] s3-client: avoid retry bursts by adding jitter to retry backoff delay Christian Ebner
2026-11-12 18:01 ` [PATCH proxmox v3 05/11] s3-client: honor response header directed retry delays Christian Ebner
2026-11-13  8:13   ` Christian Ebner
2026-11-12 18:01 ` [PATCH proxmox v3 06/11] s3-client: add additional backoff delay between upload retries Christian Ebner
2026-11-12 18:01 ` [PATCH proxmox v3 07/11] s3-client: add helper to fetch object contents with retry Christian Ebner
2026-11-12 18:01 ` [PATCH proxmox-backup v3 08/11] s3: retry fetching object contents on body collection errors Christian Ebner
2026-11-12 18:01 ` [PATCH proxmox-backup v3 09/11] datastore: snapshot: propagate errors in manifest update callback Christian Ebner
2026-11-12 18:01 ` [PATCH proxmox-backup v3 10/11] fix #7606: verify: s3: handle transient chunk load errors Christian Ebner
2026-11-12 18:01 ` [PATCH proxmox-backup v3 11/11] verify: s3: stop early if the endpoint appears unreachable Christian Ebner
2026-11-13 14:23 ` superseded: [PATCH proxmox{,-backup} v3 00/11] fix #7606: verify: s3: handle transient fetch errors and add retry Christian Ebner

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261112180119.660923-4-c.ebner@proxmox.com \
    --to=c.ebner@proxmox.com \
    --cc=pbs-devel@lists.proxmox.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.
Service provided by Proxmox Server Solutions GmbH | Privacy | Legal