|
| 1 | +use std::fmt; |
| 2 | + |
| 3 | +use aws_smithy_runtime_api::client::interceptors::context::InterceptorContext; |
| 4 | +use aws_smithy_runtime_api::client::retries::classifiers::{ |
| 5 | + ClassifyRetry, |
| 6 | + RetryAction, |
| 7 | + RetryClassifierPriority, |
| 8 | +}; |
| 9 | +use tracing::debug; |
| 10 | + |
| 11 | +const MONTHLY_LIMIT_ERROR_MARKER: &str = "MONTHLY_REQUEST_COUNT"; |
| 12 | +const HIGH_LOAD_ERROR_MESSAGE: &str = |
| 13 | + "Encountered unexpectedly high load when processing the request, please try again."; |
| 14 | +const SERVICE_UNAVAILABLE_EXCEPTION: &str = "ServiceUnavailableException"; |
| 15 | + |
| 16 | +#[derive(Debug, Default)] |
| 17 | +pub struct QCliRetryClassifier; |
| 18 | + |
| 19 | +impl QCliRetryClassifier { |
| 20 | + pub fn new() -> Self { |
| 21 | + Self |
| 22 | + } |
| 23 | + |
| 24 | + pub fn priority() -> RetryClassifierPriority { |
| 25 | + RetryClassifierPriority::run_after(RetryClassifierPriority::transient_error_classifier()) |
| 26 | + } |
| 27 | + |
| 28 | + fn extract_response_body(ctx: &InterceptorContext) -> Option<&str> { |
| 29 | + let bytes = ctx.response()?.body().bytes()?; |
| 30 | + std::str::from_utf8(bytes).ok() |
| 31 | + } |
| 32 | + |
| 33 | + fn is_monthly_limit_error(body_str: &str) -> bool { |
| 34 | + let is_monthly_limit = body_str.contains(MONTHLY_LIMIT_ERROR_MARKER); |
| 35 | + debug!( |
| 36 | + "QCliRetryClassifier: Monthly limit error detected: {}", |
| 37 | + is_monthly_limit |
| 38 | + ); |
| 39 | + is_monthly_limit |
| 40 | + } |
| 41 | + |
| 42 | + fn is_service_overloaded_error(ctx: &InterceptorContext, body_str: &str) -> bool { |
| 43 | + let Some(resp) = ctx.response() else { |
| 44 | + return false; |
| 45 | + }; |
| 46 | + |
| 47 | + if resp.status().as_u16() != 500 { |
| 48 | + return false; |
| 49 | + } |
| 50 | + |
| 51 | + let is_overloaded = |
| 52 | + body_str.contains(HIGH_LOAD_ERROR_MESSAGE) || body_str.contains(SERVICE_UNAVAILABLE_EXCEPTION); |
| 53 | + |
| 54 | + debug!( |
| 55 | + "QCliRetryClassifier: Service overloaded error detected (status 500): {}", |
| 56 | + is_overloaded |
| 57 | + ); |
| 58 | + is_overloaded |
| 59 | + } |
| 60 | +} |
| 61 | + |
| 62 | +impl ClassifyRetry for QCliRetryClassifier { |
| 63 | + fn classify_retry(&self, ctx: &InterceptorContext) -> RetryAction { |
| 64 | + let Some(body_str) = Self::extract_response_body(ctx) else { |
| 65 | + return RetryAction::NoActionIndicated; |
| 66 | + }; |
| 67 | + |
| 68 | + if Self::is_monthly_limit_error(body_str) { |
| 69 | + return RetryAction::RetryForbidden; |
| 70 | + } |
| 71 | + |
| 72 | + if Self::is_service_overloaded_error(ctx, body_str) { |
| 73 | + return RetryAction::throttling_error(); |
| 74 | + } |
| 75 | + |
| 76 | + RetryAction::NoActionIndicated |
| 77 | + } |
| 78 | + |
| 79 | + fn name(&self) -> &'static str { |
| 80 | + "Q CLI Custom Retry Classifier" |
| 81 | + } |
| 82 | + |
| 83 | + fn priority(&self) -> RetryClassifierPriority { |
| 84 | + Self::priority() |
| 85 | + } |
| 86 | +} |
| 87 | + |
| 88 | +impl fmt::Display for QCliRetryClassifier { |
| 89 | + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 90 | + write!(f, "QCliRetryClassifier") |
| 91 | + } |
| 92 | +} |
| 93 | + |
| 94 | +#[cfg(test)] |
| 95 | +mod tests { |
| 96 | + use aws_smithy_runtime_api::client::interceptors::context::{ |
| 97 | + Input, |
| 98 | + InterceptorContext, |
| 99 | + }; |
| 100 | + use aws_smithy_types::body::SdkBody; |
| 101 | + use http::Response; |
| 102 | + |
| 103 | + use super::*; |
| 104 | + |
| 105 | + #[test] |
| 106 | + fn test_monthly_limit_error_classification() { |
| 107 | + let classifier = QCliRetryClassifier::new(); |
| 108 | + let mut ctx = InterceptorContext::new(Input::doesnt_matter()); |
| 109 | + |
| 110 | + let response_body = r#"{"__type":"ThrottlingException","message":"Maximum Request reached for this month.","reason":"MONTHLY_REQUEST_COUNT"}"#; |
| 111 | + let response = Response::builder() |
| 112 | + .status(400) |
| 113 | + .body(response_body) |
| 114 | + .unwrap() |
| 115 | + .map(SdkBody::from); |
| 116 | + |
| 117 | + ctx.set_response(response.try_into().unwrap()); |
| 118 | + |
| 119 | + let result = classifier.classify_retry(&ctx); |
| 120 | + assert_eq!(result, RetryAction::RetryForbidden); |
| 121 | + } |
| 122 | + |
| 123 | + #[test] |
| 124 | + fn test_service_unavailable_exception_classification() { |
| 125 | + let classifier = QCliRetryClassifier::new(); |
| 126 | + let mut ctx = InterceptorContext::new(Input::doesnt_matter()); |
| 127 | + |
| 128 | + let response_body = r#"{"__type":"ServiceUnavailableException","message":"The service is temporarily unavailable. Please try again later."}"#; |
| 129 | + let response = Response::builder() |
| 130 | + .status(500) |
| 131 | + .body(response_body) |
| 132 | + .unwrap() |
| 133 | + .map(SdkBody::from); |
| 134 | + |
| 135 | + ctx.set_response(response.try_into().unwrap()); |
| 136 | + |
| 137 | + let result = classifier.classify_retry(&ctx); |
| 138 | + assert_eq!(result, RetryAction::throttling_error()); |
| 139 | + } |
| 140 | + |
| 141 | + #[test] |
| 142 | + fn test_high_load_error_classification() { |
| 143 | + let classifier = QCliRetryClassifier::new(); |
| 144 | + let mut ctx = InterceptorContext::new(Input::doesnt_matter()); |
| 145 | + |
| 146 | + let response_body = |
| 147 | + r#"{"error": "Encountered unexpectedly high load when processing the request, please try again."}"#; |
| 148 | + let response = Response::builder() |
| 149 | + .status(500) |
| 150 | + .body(response_body) |
| 151 | + .unwrap() |
| 152 | + .map(SdkBody::from); |
| 153 | + |
| 154 | + ctx.set_response(response.try_into().unwrap()); |
| 155 | + |
| 156 | + let result = classifier.classify_retry(&ctx); |
| 157 | + assert_eq!(result, RetryAction::throttling_error()); |
| 158 | + } |
| 159 | + |
| 160 | + #[test] |
| 161 | + fn test_500_error_without_specific_message_not_retried() { |
| 162 | + let classifier = QCliRetryClassifier::new(); |
| 163 | + let mut ctx = InterceptorContext::new(Input::doesnt_matter()); |
| 164 | + |
| 165 | + let response_body = r#"{"__type":"InternalServerException","message":"Some other error"}"#; |
| 166 | + let response = Response::builder() |
| 167 | + .status(500) |
| 168 | + .body(response_body) |
| 169 | + .unwrap() |
| 170 | + .map(SdkBody::from); |
| 171 | + |
| 172 | + ctx.set_response(response.try_into().unwrap()); |
| 173 | + |
| 174 | + let result = classifier.classify_retry(&ctx); |
| 175 | + assert_eq!(result, RetryAction::NoActionIndicated); |
| 176 | + } |
| 177 | + |
| 178 | + #[test] |
| 179 | + fn test_no_action_for_other_status_codes() { |
| 180 | + let classifier = QCliRetryClassifier::new(); |
| 181 | + let mut ctx = InterceptorContext::new(Input::doesnt_matter()); |
| 182 | + |
| 183 | + let response = Response::builder() |
| 184 | + .status(400) |
| 185 | + .body("Bad Request") |
| 186 | + .unwrap() |
| 187 | + .map(SdkBody::from); |
| 188 | + |
| 189 | + ctx.set_response(response.try_into().unwrap()); |
| 190 | + |
| 191 | + let result = classifier.classify_retry(&ctx); |
| 192 | + assert_eq!(result, RetryAction::NoActionIndicated); |
| 193 | + } |
| 194 | +} |
0 commit comments