From 48332fa76f4411f1500cca0593a660b74d097fb9 Mon Sep 17 00:00:00 2001 From: Christian Grothoff Date: Wed, 14 Oct 2026 16:15:14 +0200 Subject: [PATCH 4/9] api: remember whether a database error is worth retrying Every connection runs with SERIALIZABLE isolation, so a handler that reads, decides and then writes can lose the race and has to repeat the whole thing; turning the sqlx error into an ApiError threw away the one bit that says so. --- common/taler-api/src/error.rs | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/common/taler-api/src/error.rs b/common/taler-api/src/error.rs index f6494ca..50bed41 100644 --- a/common/taler-api/src/error.rs +++ b/common/taler-api/src/error.rs @@ -136,6 +136,8 @@ pub struct ApiError { path: Option>, headers: Option>, extra: Option>, + /// Set when this was made from a database error that a retry can clear. + retryable: bool, } impl Display for ApiError { @@ -158,9 +160,23 @@ impl ApiError { path: None, headers: None, extra: None, + retryable: false, } } + /// Whether the database asked for the work to be done again: a + /// serialization failure or a deadlock (40001, 40P01) under the + /// `SERIALIZABLE` isolation every connection runs with. + /// + /// A handler that reads, decides and then writes cannot retry such a + /// failure where it happens -- the whole read-modify-write has to be + /// repeated on a snapshot that includes the other writer -- so the one + /// bit that says whether repeating it is worth anything has to survive + /// the trip up to it. + pub fn is_retryable(&self) -> bool { + self.retryable + } + pub fn with_hint(self, hint: impl Display) -> Self { Self { hint: Some(hint.to_string().into_boxed_str()), @@ -210,6 +226,8 @@ impl ApiError { impl From for ApiError { fn from(value: sqlx::Error) -> Self { + use crate::db::PgError; + let retryable = value.is_retryable_err(); let (code, status) = match value { sqlx::Error::Configuration(_) => { (ErrorCode::GENERIC_DB_SETUP_FAILED, StatusCode::BAD_GATEWAY) @@ -230,6 +248,7 @@ impl From for ApiError { path: None, headers: None, extra: None, + retryable, } } } @@ -301,6 +320,8 @@ impl IntoResponse for ApiError { path, headers, extra, + // Ours, not the client's business. + retryable: _, } = self; let status_code = status .unwrap_or_else(|| StatusCode::from_u16(code.status()).expect("Invalid status code")); -- 2.47.3