googleapis / googleapis/google-http-java-client

allow overriding HttpResponse.getContent to install download throttler

未关闭
#586 1 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看
type: feature request
主要语言
Java
星标
1.4k
派生
473
PR 合并指标
30 天内没有已合并 PR

描述

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'
....
}
```

贡献指南

打开贡献指南

评估

这个 Issue 还没有评估数据。

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。