googleapis / googleapis/google-http-java-client

allow overriding HttpResponse.getContent to install download throttler

Đang mở
#586 1 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ả

We need to throttle downloads from cloud storage in order to minimize GC disturbances during (data) reload. I was unable to find a clean way to do this via existing API. Instead, the following workaround was implemented which suffers from extending `HttpURLConnection`. Is it possible to provide a proper hook for intercepting `InputStream`?

```java
protected Storage installHttpThrottler(StorageOptions.Builder storageOptions) {
return storageOptions
.setTransportOptions(HttpTransportOptions.newBuilder().setHttpTransportFactory(new ThrottledHttpTransportFactory()).build())
.build()
.getService();
}
```

```java
class ThrottledHttpTransportFactory implements HttpTransportFactory {
public HttpTransport create() { return new NetHttpTransport.Builder().setConnectionFactory(new ThrottledConnectionFactory()).build(); }
}
```

```java
class ThrottledConnectionFactory extends DefaultConnectionFactory {
@Override
public HttpURLConnection openConnection(URL url) throws IOException {
return new ThrottledUrlConnection(super.openConnection(url), url);
}
}
```

```java
class ThrottledUrlConnection extends HttpURLConnection {
HttpURLConnection delegate;

ThrottledUrlConnection(HttpURLConnection delegate, URL url) {
super(url);
this.delegate = delegate;
}

@Override
public InputStream getInputStream() throws IOException {
return new ThrottledInputStream(delegate.getInputStream(), rateLimiter, throughputMeter);
}
// followed by a long list of overridden methods to redirect via 'delegate'
....
}
```

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.