googleapis / googleapis/google-http-java-client

Inspecting HttpResponse content in BackOffRequired results in IOException later

Đang mở
#1,403 0 bình luận 0 reaction 0 người được giao Xem trên GitHub
type: feature request
Ngôn ngữ chính
Java
Star
1.4k
Fork
473
Chỉ số merge pull request
Không có pull request nào được merge trong 30 ngày

Mô tả

When implementing retries for Compute client one needs to read and parse `HttpResponse` as JSON in order to find `rateLimitExceeded` error in details (we could rely on the status code but unfortunately it's a generic `403 Forbidden` in this case). Later when the library attempts to read the response again to throw `GoogleJsonResponseException` it won't be able to fill `details` field because the HTTP content has been already read (it also `printStackTrace`s an `IOException`) .

```java
private HttpBackOffUnsuccessfulResponseHandler createRetryHandler() {
BackOff backOff = new ExponentialBackOff.Builder()
.setInitialIntervalMillis(1000)
.setMaxIntervalMillis(5000)
.setMaxElapsedTimeMillis(60_000)
.build();
HttpBackOffUnsuccessfulResponseHandler retryHandler = new HttpBackOffUnsuccessfulResponseHandler(backOff) {
@Override
public boolean handleResponse(HttpRequest request, HttpResponse response, boolean supportsRetry)
throws IOException {

return httpCredentialsAdapter.handleResponse(request, response, supportsRetry) ||
super.handleResponse(request, response, supportsRetry);
}
};
retryHandler.setBackOffRequired(resp -> {
GoogleJsonResponseException exc = GoogleJsonResponseException.from(JacksonFactory.getDefaultInstance(),
resp);
return
exc.getDetails().getErrors().stream().anyMatch(err -> "rateLimitExceeded".equals(err.getReason()));
});
return retryHandler;
}
```
```
java.io.IOException: Stream closed
at java.base/java.util.zip.GZIPInputStream.ensureOpen(GZIPInputStream.java:63)
at java.base/java.util.zip.GZIPInputStream.read(GZIPInputStream.java:114)
at org.apache.http.client.entity.LazyDecompressingInputStream.read(LazyDecompressingInputStream.java:70)
at com.fasterxml.jackson.core.json.ByteSourceJsonBootstrapper.ensureLoaded(ByteSourceJsonBootstrapper.java:539)
at com.fasterxml.jackson.core.json.ByteSourceJsonBootstrapper.detectEncoding(ByteSourceJsonBootstrapper.java:133)
at com.fasterxml.jackson.core.json.ByteSourceJsonBootstrapper.constructParser(ByteSourceJsonBootstrapper.java:256)
at com.fasterxml.jackson.core.JsonFactory._createParser(JsonFactory.java:1656)
at com.fasterxml.jackson.core.JsonFactory.createParser(JsonFactory.java:1085)
at com.fasterxml.jackson.core.JsonFactory.createJsonParser(JsonFactory.java:1466)
at com.google.api.client.json.jackson2.JacksonFactory.createJsonParser(JacksonFactory.java:85)
at com.google.api.client.googleapis.json.GoogleJsonResponseException.from(GoogleJsonResponseException.java:102)
at com.google.api.client.googleapis.services.json.AbstractGoogleJsonClientRequest.newExceptionOnError(AbstractGoogleJsonClientRequest.java:118)
at com.google.api.client.googleapis.services.json.AbstractGoogleJsonClientRequest.newExceptionOnError(AbstractGoogleJsonClientRequest.java:37)
at com.google.api.client.googleapis.services.AbstractGoogleClientRequest$1.interceptResponse(AbstractGoogleClientRequest.java:428)
at com.google.api.client.http.HttpRequest.execute(HttpRequest.java:1108)
at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.executeUnparsed(AbstractGoogleClientRequest.java:514)
at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.executeUnparsed(AbstractGoogleClientRequest.java:455)
at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.execute(AbstractGoogleClientRequest.java:565)
```

Hướng dẫn đóng góp

Mở hướng dẫn đóng góp

Đánh giá

Issue này chưa được đánh giá.

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.