-
Notifications
You must be signed in to change notification settings - Fork 21
GH-249: adding RetryAfterError FeignException type to map 429 http response from Adobe APIs
#250
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
73 changes: 73 additions & 0 deletions
73
core/src/main/java/com/adobe/aio/exception/feign/RetryAfterError.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,73 @@ | ||
| /* | ||
| * Copyright 2017 Adobe. All rights reserved. | ||
| * This file is licensed to you under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. You may obtain a copy | ||
| * of the License at http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software distributed under | ||
| * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS | ||
| * OF ANY KIND, either express or implied. See the License for the specific language | ||
| * governing permissions and limitations under the License. | ||
| */ | ||
| package com.adobe.aio.exception.feign; | ||
|
|
||
| import static java.time.format.DateTimeFormatter.RFC_1123_DATE_TIME; | ||
|
|
||
| import feign.FeignException; | ||
| import feign.Response; | ||
| import java.time.Instant; | ||
| import java.time.format.DateTimeParseException; | ||
| import java.util.Optional; | ||
| import org.slf4j.Logger; | ||
| import org.slf4j.LoggerFactory; | ||
|
|
||
| public class RetryAfterError extends FeignException { | ||
|
|
||
| private static final Logger logger = LoggerFactory.getLogger(RetryAfterError.class); | ||
| private static final String ERROR_MESSAGE_TEMPLATE = "Rate limit exceeded. request-id: `%s`, retry-after: `%s` seconds. %s"; | ||
| private final String upStreamRequestId; | ||
| private final String retryAfter; | ||
|
|
||
| public RetryAfterError(Response response, FeignException exception, String requestId, | ||
| String retryAfter) { | ||
| super(response.status(), | ||
| String.format(ERROR_MESSAGE_TEMPLATE, requestId, retryAfter, exception.getMessage()), | ||
| response.request(), exception); | ||
| this.upStreamRequestId = requestId; | ||
| this.retryAfter = retryAfter; | ||
| } | ||
|
|
||
| public Optional<String> getUpStreamRequestId() { | ||
| return Optional.ofNullable(upStreamRequestId); | ||
| } | ||
| /** | ||
| * Get the retry-after value in seconds | ||
| * | ||
| * @return the retry-after value in seconds, or 0 if not available or invalid | ||
| */ | ||
| public long getRetryAfterInSeconds() { | ||
| if (retryAfter == null || retryAfter.trim().isEmpty()) { | ||
| return 0L; | ||
| } | ||
|
|
||
| String trimmedRetryAfterHeaderValue = retryAfter.trim(); | ||
|
|
||
| // First, try to parse as a number of seconds (delay-seconds) | ||
| try { | ||
| return Long.parseLong(trimmedRetryAfterHeaderValue); | ||
| } catch (NumberFormatException e) { | ||
| // If not a number, try to parse as HTTP-date | ||
| try { | ||
| // Parse HTTP-date format (e.g., "Tue, 3 Jun 2008 11:05:30 GMT") | ||
| Instant retryInstant = Instant.from(RFC_1123_DATE_TIME.parse(trimmedRetryAfterHeaderValue)); | ||
| Instant now = Instant.now(); | ||
| long secondsUntilRetry = retryInstant.getEpochSecond() - now.getEpochSecond(); | ||
| return Math.max(0L, secondsUntilRetry); // Ensure non-negative | ||
| } catch (DateTimeParseException dateTimeParseException) { | ||
| logger.warn("Invalid retry-after header value (neither delay-seconds nor HTTP-date): {}", | ||
| retryAfter, dateTimeParseException); | ||
| return 0L; | ||
| } | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
113 changes: 113 additions & 0 deletions
113
core/src/test/java/com/adobe/aio/exception/feign/RetryAfterErrorTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,113 @@ | ||
| /* | ||
| * Copyright 2017 Adobe. All rights reserved. | ||
| * This file is licensed to you under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. You may obtain a copy | ||
| * of the License at http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software distributed under | ||
| * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS | ||
| * OF ANY KIND, either express or implied. See the License for the specific language | ||
| * governing permissions and limitations under the License. | ||
| */ | ||
| package com.adobe.aio.exception.feign; | ||
|
|
||
| import static com.adobe.aio.util.feign.FeignTestUtils.DEFAULT_RETRY_AFTER_SECONDS_STR; | ||
| import static com.adobe.aio.util.feign.FeignTestUtils.create429Response; | ||
| import static com.adobe.aio.util.feign.FeignTestUtils.createResponseWithEmptyHeaders; | ||
| import static java.time.format.DateTimeFormatter.RFC_1123_DATE_TIME; | ||
| import static org.junit.jupiter.api.Assertions.assertEquals; | ||
| import static org.junit.jupiter.api.Assertions.assertTrue; | ||
|
|
||
| import feign.FeignException; | ||
| import feign.Response; | ||
| import java.time.ZonedDateTime; | ||
| import org.junit.jupiter.api.Test; | ||
| import org.slf4j.Logger; | ||
| import org.slf4j.LoggerFactory; | ||
|
|
||
| class RetryAfterErrorTest { | ||
|
|
||
| private static final String TEST_REQUEST_ID = "test-request-id"; | ||
| private static final String TEST_METHOD = "testMethod"; | ||
| private static final Logger logger = LoggerFactory.getLogger(RetryAfterErrorTest.class); | ||
|
|
||
| @Test | ||
| void testRetryAfterErrorCreation() { | ||
| Response response = create429Response(DEFAULT_RETRY_AFTER_SECONDS_STR); | ||
| RetryAfterError retryAfterError = createRetryAfterError(response, DEFAULT_RETRY_AFTER_SECONDS_STR); | ||
|
|
||
| // Test basic properties | ||
| assertBasicProperties(retryAfterError); | ||
|
|
||
| // Test retry-after functionality (delay-seconds format) | ||
| assertEquals(60L, retryAfterError.getRetryAfterInSeconds()); | ||
|
|
||
| // Test request ID | ||
| assertRequestIdProperties(retryAfterError); | ||
| } | ||
|
|
||
| @Test | ||
| void testRetryAfterErrorWithoutRetryAfter() { | ||
| Response response = createResponseWithEmptyHeaders(429, "Too Many Requests"); | ||
| RetryAfterError retryAfterError = createRetryAfterError(response, null); | ||
|
|
||
| // Test retry-after functionality when not available | ||
| assertEquals(0L, retryAfterError.getRetryAfterInSeconds()); | ||
| } | ||
|
|
||
| @Test | ||
| void testRetryAfterErrorWithInvalidRetryAfter() { | ||
| Response response = createResponseWithEmptyHeaders(429, "Too Many Requests"); | ||
| RetryAfterError retryAfterError = createRetryAfterError(response, "invalid"); | ||
|
|
||
| // Test retry-after functionality with invalid value | ||
| assertEquals(0L, retryAfterError.getRetryAfterInSeconds()); // Should return 0 for invalid values | ||
| } | ||
|
|
||
| @Test | ||
| void testRetryAfterErrorWithHttpDate() { | ||
| Response response = createResponseWithEmptyHeaders(429, "Too Many Requests"); | ||
| String httpDate = "Fri, 31 Dec 1999 23:59:59 GMT"; | ||
| RetryAfterError retryAfterError = createRetryAfterError(response, httpDate); | ||
|
|
||
| // Test with HTTP-date format (past date) | ||
| assertEquals(0L, retryAfterError.getRetryAfterInSeconds()); // Past date should return 0 | ||
| } | ||
|
|
||
| @Test | ||
| void testRetryAfterErrorWithFutureHttpDate() { | ||
| Response response = createResponseWithEmptyHeaders(429, "Too Many Requests"); | ||
| // Add a day to get a future date | ||
| ZonedDateTime futureDate = ZonedDateTime.now().plusDays(1); | ||
|
|
||
| // Format using RFC 1123 formatter | ||
| String formattedFutureDate = futureDate.format(RFC_1123_DATE_TIME); | ||
| logger.info("Future date: {}", formattedFutureDate); | ||
|
|
||
| RetryAfterError retryAfterError = createRetryAfterError(response, formattedFutureDate); | ||
|
|
||
| // Should return a positive number of seconds until the future date | ||
| long retrySeconds = retryAfterError.getRetryAfterInSeconds(); | ||
| assertTrue(retrySeconds > 0, "Should return positive seconds for future date"); | ||
| assertEquals(24 * 60 * 60, retrySeconds); | ||
| } | ||
|
|
||
| // Helper methods for creating test objects and assertions | ||
|
|
||
| private RetryAfterError createRetryAfterError(Response response, String retryAfter) { | ||
| FeignException originalException = FeignException.errorStatus(TEST_METHOD, response); | ||
| return new RetryAfterError(response, originalException, TEST_REQUEST_ID, retryAfter); | ||
| } | ||
|
|
||
| private void assertBasicProperties(RetryAfterError retryAfterError) { | ||
| assertEquals(429, retryAfterError.status()); | ||
| assertTrue(retryAfterError.getMessage().contains("Rate limit exceeded")); | ||
| assertTrue(retryAfterError.getMessage().contains(TEST_REQUEST_ID)); | ||
| assertTrue(retryAfterError.getMessage().contains(DEFAULT_RETRY_AFTER_SECONDS_STR)); | ||
| } | ||
|
|
||
| private void assertRequestIdProperties(RetryAfterError retryAfterError) { | ||
| assertTrue(retryAfterError.getUpStreamRequestId().isPresent()); | ||
| assertEquals(RetryAfterErrorTest.TEST_REQUEST_ID, retryAfterError.getUpStreamRequestId().get()); | ||
| } | ||
| } |
98 changes: 98 additions & 0 deletions
98
core/src/test/java/com/adobe/aio/util/feign/FeignTestUtils.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,98 @@ | ||
| /* | ||
| * Copyright 2017 Adobe. All rights reserved. | ||
| * This file is licensed to you under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. You may obtain a copy | ||
| * of the License at http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software distributed under | ||
| * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS | ||
| * OF ANY KIND, either express or implied. See the License for the specific language | ||
| * governing permissions and limitations under the License. | ||
| */ | ||
| package com.adobe.aio.util.feign; | ||
|
|
||
| import feign.Request; | ||
| import feign.Response; | ||
| import java.util.Collections; | ||
| import java.util.HashMap; | ||
| import java.util.Map; | ||
|
|
||
| /** | ||
| * Utility class for creating test objects in Feign-related tests | ||
| */ | ||
| public class FeignTestUtils { | ||
|
|
||
| private static final String DEFAULT_URL = "http://test.com"; | ||
| public static final String DEFAULT_REQUEST_ID = "test-request-id"; | ||
| public static final String DEFAULT_RETRY_AFTER_SECONDS_STR = "60"; | ||
|
|
||
| private FeignTestUtils() { | ||
| // Utility class - prevent instantiation | ||
| throw new IllegalStateException("Utility class must not be instantiated"); | ||
| } | ||
|
|
||
| /** | ||
| * Creates a basic HTTP request for testing | ||
| */ | ||
| public static Request createBasicRequest() { | ||
| return Request.create(Request.HttpMethod.GET, DEFAULT_URL, new HashMap<>(), null, null, null); | ||
| } | ||
|
|
||
| /** | ||
| * Creates a response builder with common defaults | ||
| */ | ||
| public static Response.Builder createResponseBuilder(int status, String reason) { | ||
| return Response.builder().status(status).reason(reason).request(createBasicRequest()); | ||
| } | ||
|
|
||
| /** | ||
| * Creates a response with retry-after and request-id headers | ||
| */ | ||
| public static Response createResponseWithHeaders(int status, String reason, String retryAfter, | ||
| String requestId) { | ||
| Map<String, java.util.Collection<String>> headers = new HashMap<>(); | ||
| if (retryAfter != null) { | ||
| headers.put("retry-after", Collections.singletonList(retryAfter)); | ||
| } | ||
| if (requestId != null) { | ||
| headers.put("x-request-id", Collections.singletonList(requestId)); | ||
| } | ||
|
|
||
| return createResponseBuilder(status, reason).headers(headers).build(); | ||
| } | ||
|
|
||
| /** | ||
| * Creates a 429 response with retry-after header | ||
| */ | ||
| public static Response create429Response(String retryAfter) { | ||
| return createResponseWithHeaders(429, "Too Many Requests", retryAfter, DEFAULT_REQUEST_ID); | ||
| } | ||
|
|
||
| /** | ||
| * Creates a 429 response without retry-after header | ||
| */ | ||
| public static Response create429ResponseWithoutRetryAfter() { | ||
| return createResponseWithHeaders(429, "Too Many Requests", null, DEFAULT_REQUEST_ID); | ||
| } | ||
|
|
||
| /** | ||
| * Creates a 500 response with request-id header | ||
| */ | ||
| public static Response create500Response() { | ||
| return createResponseWithHeaders(500, "Internal Server Error", null, DEFAULT_REQUEST_ID); | ||
| } | ||
|
|
||
| /** | ||
| * Creates a 400 response without special headers | ||
| */ | ||
| public static Response create400Response() { | ||
| return createResponseBuilder(400, "Bad Request").headers(new HashMap<>()).build(); | ||
| } | ||
|
|
||
| /** | ||
| * Creates a response with empty headers | ||
| */ | ||
| public static Response createResponseWithEmptyHeaders(int status, String reason) { | ||
| return createResponseBuilder(status, reason).headers(new HashMap<>()).build(); | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
actually @shikhartanwar we should use the
RetryableExceptioninstead hereto leverage the Feign retryer see #151
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The default implementation of ErrorDecoder only creates a RetryableExeception instance when the response contains the “Retry-After” header
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
But when I checked in the E2E logs it was always an upstream error crafted by us
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yes we had a bug in our ErrorDecoder I worked on a fix here : #251