GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — expose HttpRequest.setInterceptor to allow for throttled uploads · Issue #3534 · googleapis/google-cloud-java

expose HttpRequest.setInterceptor to allow for throttled uploads #3534

New issue
Copy link
New issue
Copy link
Closed
Closed
expose HttpRequest.setInterceptor to allow for throttled uploads#3534
Copy link
Assignees
andrey-qlogic
Labels
api: storageIssues related to the Cloud Storage API.type: feature request‘Nice-to-have’ improvement, new feature or different behavior or design.

Description

@srosenberg
srosenberg
opened on Aug 6, 2018
Issue body actions

We have a multi-threaded uploader which transfers large files from local disk to GCS. The uploader is co-located with our low-latency app. inside the JVM. To control latency, we must throttle the uploader. Our best workaround involves resorting to reflection in order to install HttpRequest interceptor which effectively wraps HttpContent with a throttled OutputStream; e.g.,

Object storageRpc = FieldUtils.getField(storage.getClass(), "storageRpc", true).get(storage);
Object serviceStorage = FieldUtils.getField(storageRpc.getClass(), "storage", true).get(storageRpc);
Object requestFactory = FieldUtils.getField(serviceStorage.getClass(), "requestFactory", true).get(serviceStorage);
Field initializerField = FieldUtils.getField(requestFactory.getClass(), "initializer", true);
Object initializer = initializerField.get(requestFactory);
initializerField.set(requestFactory, new InterceptingHttpRequestInitializer((HttpRequestInitializer) initializer));

Activity

  1. yihanzhen
    added
    type: feature request‘Nice-to-have’ improvement, new feature or different behavior or design.
    api: storageIssues related to the Cloud Storage API.
    priority: p2Moderately-important priority. Fix may not be included in next release.
    on Aug 6, 2018
  2. yihanzhen commented on Aug 6, 2018

    @yihanzhen
    yihanzhen
    on Aug 6, 2018
    Contributor
    More actions

    This would require work in google-http-java-client. cc/ @chingor13

  3. chingor13
    self-assigned this
    on Aug 13, 2018
  4. chingor13 commented on Aug 27, 2018

    @chingor13
    chingor13
    on Aug 27, 2018
    Contributor
    More actions

    Is there a reason this needs to be modifiable after construction?

    It looks like you can accomplish wrapping an additional HttpRequestInitializer by extending the HttpTransportOptions class and wrapping your InterceptingHttpRequestInitializer around the parent one:

    class MyTransportOptions extends HttpTransportOptions {
      // TODO: make your own constructor or builder
      @Override
      public HttpRequestInitializer getHttpRequestInitializer(final ServiceOptions<?, ?> serviceOptions) {
        return new InterceptingHttpRequestInitializer(super.getHttpRequestInitiallizer(serviceOptions));
      }
    }
    
    StorageOptions options = StorageOptions
      .newBuilder()
      .setTransportOptions(new MyTransportOptions())
      .build();
    Storage storage = options.getService();
  5. srosenberg commented on Aug 30, 2018

    @srosenberg
    srosenberg
    on Aug 30, 2018
    Author
    More actions

    @chingor13 That works for our use-case, thanks. I must have missed that HttpTransportOptions is used by default (and getHttpRequestInitializer can be overridden); much better than using reflection hack.

    I can't think of another use-case which would require modifying RequestInitializer after construction; in fact, it seems like a good practice to make it immutable; e.g., modifying it while request is in progress is a risky proposition. Feel free to close the issue.

  6. sduskis
    removed
    priority: p2Moderately-important priority. Fix may not be included in next release.
    on Oct 9, 2018
  7. ajaaym commented on Dec 6, 2018

    @ajaaym
    ajaaym
    on Dec 6, 2018
    Contributor
    More actions

    closing this issue as its resolved.

  8. ajaaym
    closed this as completedon Dec 6, 2018
  9. srosenberg commented on Jan 15, 2019

    @srosenberg
    srosenberg
    on Jan 15, 2019
    Author
    More actions

    @chingor13 We're only now revisiting this issue... what you suggested is not feasible (statically) because HttpTransportOptions has a private constructor,

    https://github.com/googleapis/google-cloud-java/blob/master/google-cloud-clients/google-cloud-core-http/src/main/java/com/google/cloud/http/HttpTransportOptions.java#L123

    @ajaaym could you please reopen the issue unless there is a less hacky workaround as I'd originally suggested?

  10. ajaaym
    reopened this on Jan 15, 2019
  11. andrey-qlogic commented on Jan 18, 2019

    @andrey-qlogic
    andrey-qlogic
    on Jan 18, 2019
    More actions

    @srosenberg , @sduskis what is new feature might be needed from the api: storage now by this feature request?

  12. sduskis
    assigned
    andrey-qlogic
    and unassigned
    chingor13
    on Feb 4, 2019
  13. srosenberg commented on Feb 5, 2019

    @srosenberg
    srosenberg
    on Feb 5, 2019
    Author
    More actions

    @ajaaym Recently, we had to also throttle downloads 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 using reflection (see second snippet) and extending HttpURLConnection. Is it possible to provide a hook for intercepting InputStream?

    protected Storage installHttpThrottler(StorageOptions.Builder storageOptions) {
            return storageOptions
                    .setTransportOptions(HttpTransportOptions.newBuilder().setHttpTransportFactory(new ThrottledHttpTransportFactory()).build())
                    .build()
                    .getService();
        }
    class ThrottledHttpTransportFactory implements HttpTransportFactory {
            public HttpTransport create() {
                NetHttpTransport result = new NetHttpTransport();
    
                try {
                    //TODO(SR): resorting to reflection hack since there is no other way to update connectionFactory
                    //see https://github.com/GoogleCloudPlatform/google-cloud-java/issues/3534
                    FieldUtils.writeDeclaredField(result, "connectionFactory", new ThrottledConnectionFactory(), true);
                } catch (Exception ex) {
                    throw new RuntimeException("Unable to install ThrottledConnectionFactory", ex);
                }
                return result;
            }
        }
    class ThrottledConnectionFactory extends DefaultConnectionFactory {
            @Override
            public HttpURLConnection openConnection(URL url) throws IOException {
                return new ThrottledUrlConnection(super.openConnection(url), url);
            }
        }
  14. ajaaym commented on Feb 5, 2019

    @ajaaym
    ajaaym
    on Feb 5, 2019
    Contributor
    More actions

    @srosenberg Do you want to intercept inputstream or connection factory? For inputstream, do you mean by getting raw inputstream?

    You can create transport factory like below without using reflection.

        NetHttpTransport.Builder builder = new Builder();
        builder.setConnectionFactory(new ThrottledConnectionFactory());
        return builder.build();
    
  15. srosenberg commented on Feb 5, 2019

    @srosenberg
    srosenberg
    on Feb 5, 2019
    Author
    More actions

    @ajaaym Thanks, somehow I missed the (availability of) factory; that solves the reflection hack. The other remaining issue is having to extend HttpURLConnection in order to intercept InputStream,

    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'
    ....
    }
  16. ajaaym commented on Feb 5, 2019

    @ajaaym
    ajaaym
    on Feb 5, 2019
    Contributor
    More actions

    @srosenberg as of now you can use response interceptor, but you have to use reflection to set the content after calling getContent(). You can create new feature request in that repo to allow overriding content in HttpResponse.

  17. sduskis
    closed this as completedin #4439on Feb 7, 2019
  18. srosenberg commented on Feb 10, 2019

    @srosenberg
    srosenberg
    on Feb 10, 2019
    Author
    More actions

    @ajaaym I am happy to create a new feature request for response interceptor in google-http-java-client so long that it makes it into this repo (via transitive dependency); we need it in conjunction with google-cloud-storage.

  19. srosenberg commented on Feb 10, 2019

    @srosenberg
    srosenberg
    on Feb 10, 2019
    Author
    More actions

    Issue created: googleapis/google-http-java-client#586

  20. suztomo
    added a commit that references this issue on Mar 9, 2026

    docs: Update SimpleApp to explicitly set project id (#3534)

    903a0f7
  21. chingor13
    added 2 commits that reference this issue on Mar 24, 2026

    chore: add internal option for statement executor type (#3534)

    99a6a46

    chore: support for UUID type (#3572)

    1d30d3c
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

  • @andrey-qlogic
    andrey-qlogic

Labels

api: storageIssues related to the Cloud Storage API.type: feature request‘Nice-to-have’ improvement, new feature or different behavior or design.

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

    You are visitor 01402775 — GeezerVison does not store the pages you look up.
    Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.