Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 65 additions & 0 deletions src/main/java/com/microsoft/graph/httpcore/ChaosHttpHandler.java
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;
Copy link
Contributor

Choose a reason for hiding this comment

The 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.

Copy link
Member Author

Choose a reason for hiding this comment

The 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?

Copy link
Contributor

Choose a reason for hiding this comment

The 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);
}
}

}
3 changes: 3 additions & 0 deletions src/main/java/com/microsoft/graph/httpcore/RetryHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,9 @@ public Response intercept(Chain chain) throws IOException {
while(retryRequest(response, executionCount, request, retryOption)) {
request = request.newBuilder().addHeader(RETRY_ATTEMPT_HEADER, String.valueOf(executionCount)).build();
executionCount++;
if(response != null && response.body() != null) {
response.body().close();
}
response = chain.proceed(request);
}
return response;
Expand Down