Skip to content

Commit

Permalink
hanlde the throttling error in the response header
Browse files Browse the repository at this point in the history
Signed-off-by: Xun Zhang <[email protected]>
  • Loading branch information
Zhangxunmt committed May 13, 2024
1 parent 4f7dc90 commit c7358be
Showing 1 changed file with 18 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import java.util.List;
import java.util.Map;

import org.apache.commons.collections.MapUtils;
import org.apache.http.HttpStatus;
import org.apache.logging.log4j.util.Strings;
import org.opensearch.OpenSearchStatusException;
Expand Down Expand Up @@ -80,6 +81,23 @@ public void onHeaders(SdkHttpResponse response) {
SdkHttpFullResponse sdkResponse = (SdkHttpFullResponse) response;
log.debug("received response headers: " + sdkResponse.headers());
this.statusCode = sdkResponse.statusCode();
if (MapUtils.isEmpty(sdkResponse.headers())) {
return;
}
List<String> errorsInHeader = sdkResponse.headers().get("x-amzn-ErrorType");
if (errorsInHeader == null || errorsInHeader.isEmpty()) {
return;
}
boolean containsThrottlingException = errorsInHeader.stream().anyMatch(str -> str.startsWith("ThrottlingException"));
if (containsThrottlingException) {
actionListener
.onFailure(
new OpenSearchStatusException(
REMOTE_SERVICE_ERROR + "The request was denied due to request throttling.",
RestStatus.fromCode(statusCode)
)
);
}
}

@Override
Expand Down

0 comments on commit c7358be

Please sign in to comment.