-
Notifications
You must be signed in to change notification settings - Fork 159
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
Avoid to have unexpected exception on status code 429 TooManyRequests #433
Merged
Ifropc
merged 7 commits into
lightsail-network:master
from
vinamogit:fix-npe-toomanyrequests
May 1, 2023
Merged
Changes from 6 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
ffbf5f8
Avoid to have unexpected exception on status code 429 TooManyRequests
vinamogit ee44867
Make retryAfter to be optional
Ifropc 0ce3d55
Merge remote-tracking branch 'origin/master' into HEAD
Ifropc 09d7f45
Fix test
Ifropc 5487a36
Replace Java Optional with Guava
Ifropc 4033124
Use Integer
Ifropc 22d8d55
Optimize imports
Ifropc 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
This file contains 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 |
---|---|---|
@@ -1,5 +1,6 @@ | ||
package org.stellar.sdk.requests; | ||
|
||
import com.google.common.base.Optional; | ||
import com.google.gson.reflect.TypeToken; | ||
|
||
import org.stellar.sdk.responses.GsonSingleton; | ||
|
@@ -28,7 +29,14 @@ public T handleResponse(final Response response) throws IOException, TooManyRequ | |
try { | ||
// Too Many Requests | ||
if (response.code() == 429) { | ||
int retryAfter = Integer.parseInt(response.header("Retry-After")); | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. just for consideration, could remove
|
||
Integer retryAfter = null; | ||
String header = response.header("Retry-After"); | ||
if (header != null) { | ||
try { | ||
retryAfter = Integer.parseInt(header); | ||
} catch (NumberFormatException ignored) {} | ||
} | ||
throw new TooManyRequestsException(retryAfter); | ||
} | ||
|
||
|
14 changes: 9 additions & 5 deletions
14
src/main/java/org/stellar/sdk/requests/TooManyRequestsException.java
This file contains 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 |
---|---|---|
@@ -1,21 +1,25 @@ | ||
package org.stellar.sdk.requests; | ||
|
||
|
||
import com.google.common.base.Optional; | ||
|
||
/** | ||
* Exception thrown when too many requests were sent to the Horizon server. | ||
* @see <a href="https://developers.stellar.org/api/introduction/rate-limiting/" target="_blank">Rate Limiting</a> | ||
*/ | ||
public class TooManyRequestsException extends RuntimeException { | ||
private int retryAfter; | ||
private Integer retryAfter; | ||
|
||
public TooManyRequestsException(int retryAfter) { | ||
public TooManyRequestsException(Integer retryAfter) { | ||
super("The rate limit for the requesting IP address is over its alloted limit."); | ||
this.retryAfter = retryAfter; | ||
} | ||
|
||
/** | ||
* Returns number of seconds a client should wait before sending requests again. | ||
* Returns number of seconds a client should wait before sending requests again, | ||
* or -1 this time is unknown. | ||
*/ | ||
public int getRetryAfter() { | ||
return retryAfter; | ||
public Optional<Integer> getRetryAfter() { | ||
return Optional.fromNullable(retryAfter); | ||
} | ||
} |
66 changes: 66 additions & 0 deletions
66
src/test/java/org/stellar/sdk/requests/ResponseHandlerTest.java
This file contains 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,66 @@ | ||
package org.stellar.sdk.requests; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
|
||
import java.io.IOException; | ||
|
||
import com.google.common.base.Optional; | ||
import org.junit.Assert; | ||
import org.junit.Test; | ||
|
||
import okhttp3.OkHttpClient; | ||
import okhttp3.mockwebserver.MockResponse; | ||
import okhttp3.mockwebserver.MockWebServer; | ||
|
||
public class ResponseHandlerTest { | ||
|
||
@Test | ||
public void testTooManyRequests() throws IOException, InterruptedException { | ||
|
||
MockResponse response = new MockResponse(); | ||
response.setResponseCode(429); | ||
response.setHeader("Retry-After", "10"); | ||
|
||
MockWebServer mockWebServer = new MockWebServer(); | ||
mockWebServer.start(); | ||
mockWebServer.enqueue(response); | ||
|
||
OkHttpClient okHttpClient = new OkHttpClient().newBuilder().build(); | ||
try { | ||
|
||
AccountsRequestBuilder.execute(okHttpClient, mockWebServer.url("/")); | ||
Assert.fail(); | ||
} catch (TooManyRequestsException tmre) { | ||
assertEquals(Optional.of(10), tmre.getRetryAfter()); | ||
} finally { | ||
|
||
mockWebServer.shutdown(); | ||
mockWebServer.close(); | ||
} | ||
} | ||
|
||
@Test | ||
public void testTooManyRequestsNoHeader() throws IOException, InterruptedException { | ||
|
||
MockResponse response = new MockResponse(); | ||
response.setResponseCode(429); | ||
|
||
MockWebServer mockWebServer = new MockWebServer(); | ||
|
||
mockWebServer.start(); | ||
mockWebServer.enqueue(response); | ||
|
||
OkHttpClient okHttpClient = new OkHttpClient().newBuilder().build(); | ||
|
||
try { | ||
AccountsRequestBuilder.execute(okHttpClient, mockWebServer.url("/")); | ||
Assert.fail(); | ||
} catch (TooManyRequestsException tmre) { | ||
assertEquals(Optional.absent(), tmre.getRetryAfter()); | ||
} finally { | ||
|
||
mockWebServer.shutdown(); | ||
mockWebServer.close(); | ||
} | ||
} | ||
} |
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.
this looks like an un-used import now, does your IDE highlight/warn? if using Intellij, can do 'optimize imports', will re-order and remove unused.