Skip to content

Commit 1a85d7b

Browse files
committed
Handle Retry-After header for 429 and 503 in gateway prober
When a gateway returns 429 Too Many Requests or 503 Service Unavailable, the Retry-After response header specifies how long to wait before retrying. Previously these responses fell back to static TTL defaults, ignoring the server-specified duration. Add a retry_after_or helper that parses the Retry-After header value as seconds and uses it as the cache TTL, falling back to the configured default when the header is absent. Closes #1474
1 parent 45d286f commit 1a85d7b

1 file changed

Lines changed: 13 additions & 2 deletions

File tree

payjoin-mailroom/src/ohttp_relay/gateway_prober.rs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,15 @@ impl Prober {
246246
}
247247

248248
/// Probes a target gateway by attempting to send a GET request.
249+
fn retry_after_or<B>(res: &hyper::Response<B>, default: Duration) -> Duration {
250+
res.headers()
251+
.get(hyper::header::RETRY_AFTER)
252+
.and_then(|v| v.to_str().ok())
253+
.and_then(|s| s.parse::<u64>().ok())
254+
.map(Duration::from_secs)
255+
.unwrap_or(default)
256+
}
257+
249258
async fn probe(&self, base_url: &GatewayUri) -> Policy {
250259
// Create a GET request without a body
251260
let req = hyper::Request::builder()
@@ -281,11 +290,13 @@ impl Prober {
281290
}
282291
} else if status == hyper::StatusCode::GATEWAY_TIMEOUT {
283292
ttls.http_504_gateway_timeout
293+
} else if status == hyper::StatusCode::TOO_MANY_REQUESTS {
294+
Self::retry_after_or(res, ttls.http_4xx)
284295
} else if status.is_client_error() {
285-
// TODO handle Retry-After for 429 too many requests
286296
ttls.http_4xx
297+
} else if status == hyper::StatusCode::SERVICE_UNAVAILABLE {
298+
Self::retry_after_or(res, ttls.http_5xx)
287299
} else if status.is_server_error() {
288-
// TODO handle Retry-After for 503 service unavailable
289300
ttls.http_5xx
290301
} else {
291302
ttls.default

0 commit comments

Comments
 (0)