Skip to content

Commit c7358be

Browse files
committed
hanlde the throttling error in the response header
Signed-off-by: Xun Zhang <xunzh@amazon.com>
1 parent 4f7dc90 commit c7358be

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

ml-algorithms/src/main/java/org/opensearch/ml/engine/algorithms/remote/MLSdkAsyncHttpResponseHandler.java

+18
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import java.util.List;
1717
import java.util.Map;
1818

19+
import org.apache.commons.collections.MapUtils;
1920
import org.apache.http.HttpStatus;
2021
import org.apache.logging.log4j.util.Strings;
2122
import org.opensearch.OpenSearchStatusException;
@@ -80,6 +81,23 @@ public void onHeaders(SdkHttpResponse response) {
8081
SdkHttpFullResponse sdkResponse = (SdkHttpFullResponse) response;
8182
log.debug("received response headers: " + sdkResponse.headers());
8283
this.statusCode = sdkResponse.statusCode();
84+
if (MapUtils.isEmpty(sdkResponse.headers())) {
85+
return;
86+
}
87+
List<String> errorsInHeader = sdkResponse.headers().get("x-amzn-ErrorType");
88+
if (errorsInHeader == null || errorsInHeader.isEmpty()) {
89+
return;
90+
}
91+
boolean containsThrottlingException = errorsInHeader.stream().anyMatch(str -> str.startsWith("ThrottlingException"));
92+
if (containsThrottlingException) {
93+
actionListener
94+
.onFailure(
95+
new OpenSearchStatusException(
96+
REMOTE_SERVICE_ERROR + "The request was denied due to request throttling.",
97+
RestStatus.fromCode(statusCode)
98+
)
99+
);
100+
}
83101
}
84102

85103
@Override

0 commit comments

Comments
 (0)