saluki_io/net/util/retry/classifier/
http.rs

1use http::StatusCode;
2
3use super::RetryClassifier;
4
5/// A standard HTTP response classifier.
6///
7/// Generally treats all client (4xx) and server (5xx) errors as retryable, with the exception of a few specific client
8/// errors that should not be retried:
9///
10/// - 400 Bad Request (likely a client-side bug)
11/// - 401 Unauthorized (likely a client-side misconfiguration)
12/// - 403 Forbidden (likely a client-side misconfiguration)
13/// - 413 Payload Too Large (likely a client-side bug)
14#[derive(Clone)]
15pub struct StandardHttpClassifier;
16
17impl<B, Error> RetryClassifier<http::Response<B>, Error> for StandardHttpClassifier {
18    fn should_retry(&self, response: &Result<http::Response<B>, Error>) -> bool {
19        match response {
20            Ok(resp) => match resp.status() {
21                // There's some status codes that likely indicate a fundamental misconfiguration or bug on the client
22                // side which won't be resolved by retrying the request.
23                StatusCode::BAD_REQUEST
24                | StatusCode::UNAUTHORIZED
25                | StatusCode::FORBIDDEN
26                | StatusCode::PAYLOAD_TOO_LARGE => false,
27
28                // For all other status codes, we'll only retry if they're in the client/server error range.
29                status => status.is_client_error() || status.is_server_error(),
30            },
31            Err(_) => true,
32        }
33    }
34}