-
Notifications
You must be signed in to change notification settings - Fork 29
- fixes a bug where the retry handler could leak responses #74
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,65 @@ | ||
| package com.microsoft.graph.httpcore; | ||
|
|
||
| import java.io.IOException; | ||
| import java.util.concurrent.ThreadLocalRandom; | ||
|
|
||
| import com.microsoft.graph.httpcore.middlewareoption.MiddlewareType; | ||
| import com.microsoft.graph.httpcore.middlewareoption.TelemetryOptions; | ||
|
|
||
| import okhttp3.Interceptor; | ||
| import okhttp3.MediaType; | ||
| import okhttp3.Protocol; | ||
| import okhttp3.Request; | ||
| import okhttp3.Response; | ||
| import okhttp3.ResponseBody; | ||
|
|
||
| /** | ||
| * DO NOT USE IN PRODUCTION | ||
| * interceptor that randomly fails the responses for unit testing purposes | ||
| */ | ||
| public class ChaosHttpHandler implements Interceptor { | ||
| public final MiddlewareType MIDDLEWARE_TYPE = MiddlewareType.RETRY; | ||
|
Contributor
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. nit: mixed tabs and spaces. Most of the code I have seen in msgraph repos are using spaces. I don't know your preference, but even if you prefer tabs, please make it all tabs within a specific file, because different tools assign different widths to tabs. For example, Github shows little wider than 4 spaces which makes it harder to follow when we have mixed tabs and spaces.
Member
Author
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. I usually go with tabs. I'm not sure why but my tabs seem to be replaced by spaces on properties/fields.... Maybe because I copied the file from another handler to begin with. Do you know any command that normalizes tabbing in vs code?
Contributor
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. VS had this but I don't know if they have it for VSCode: https://marketplace.visualstudio.com/items?itemName=VisualStudioPlatformTeam.FixMixedTabs I think these are the relevant settings for VSCode: https://stackoverflow.com/a/29972553 And on a funny note: https://stackoverflow.blog/2017/06/15/developers-use-spaces-make-money-use-tabs/ ;) |
||
| /* | ||
| * constant string being used | ||
| */ | ||
| private final String RETRY_AFTER = "Retry-After"; | ||
| /** | ||
| * Denominator for the failure rate (i.e. 1/X) | ||
| */ | ||
| private final Integer failureRate = 3; | ||
| /** | ||
| * default value to return on retry after | ||
| */ | ||
| private final String retryAfterValue = "10"; | ||
| /** | ||
| * body to respond on failed requests | ||
| */ | ||
| private final String responseBody = "{\"error\": {\"code\": \"TooManyRequests\",\"innerError\": {\"code\": \"429\",\"date\": \"2020-08-18T12:51:51\",\"message\": \"Please retry after\",\"request-id\": \"94fb3b52-452a-4535-a601-69e0a90e3aa2\",\"status\": \"429\"},\"message\": \"Please retry again later.\"}}"; | ||
| public static final int MSClientErrorCodeTooManyRequests = 429; | ||
|
|
||
| @Override | ||
| public Response intercept(Chain chain) throws IOException { | ||
| Request request = chain.request(); | ||
|
|
||
| if(request.tag(TelemetryOptions.class) == null) | ||
| request = request.newBuilder().tag(TelemetryOptions.class, new TelemetryOptions()).build(); | ||
| request.tag(TelemetryOptions.class).setFeatureUsage(TelemetryOptions.RETRY_HANDLER_ENABLED_FLAG); | ||
|
|
||
| final Integer dice = ThreadLocalRandom.current().nextInt(1, Integer.MAX_VALUE); | ||
|
|
||
| if(dice % failureRate == 0) { | ||
| return new Response | ||
| .Builder() | ||
| .request(request) | ||
| .protocol(Protocol.HTTP_1_1) | ||
| .code(MSClientErrorCodeTooManyRequests) | ||
| .message("Too Many Requests") | ||
| .addHeader(RETRY_AFTER, retryAfterValue) | ||
| .body(ResponseBody.create(MediaType.get("application/json"), responseBody)) | ||
| .build(); | ||
| } else { | ||
| return chain.proceed(request); | ||
| } | ||
| } | ||
|
|
||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.