RFR: 8267140: Support closing the HttpClient by making it auto-closable [v11]
Jaikiran Pai
jpai at openjdk.org
Thu Mar 23 07:43:49 UTC 2023
On Wed, 22 Mar 2023 11:15:38 GMT, Daniel Fuchs <dfuchs at openjdk.org> wrote:
>> Please find here an RFE that makes the `java.net.HttpClient` auto-closeable.
>>
>> The API has been modeled on `ExecutorService`.
>>
>> HttpClient::close() is a graceful shutdown and will wait until all operations are terminated before returning.
>> If a request is in progress, and the caller doesn't pull the corresponding data (for instance, the request was sent with a BodyHandler.ofInputStream(), but the caller stopped reading the input stream) then close() may never return.
>>
>> Therefore, additional methods similar to those present in `ExecutorService` are also proposed. In summary:
>>
>> - `shutdown()`: initiate a graceful shutdown, but doesn't wait for termination.
>> - `shutdownNow()`: initiate an immediate shutdown, attempting to cancel all operations in progress. Doesn't wait for termination.
>> - `awaitTermination(Duration)`: await for termination within the given delay
>> - `isTerminated()` tells whether the client is terminated
>>
>> New tests have been added to test the proposed behaviors.
>>
>> HttpClient tests (new and old) are still stable.
>
> Daniel Fuchs has updated the pull request incrementally with two additional commits since the last revision:
>
> - Minor updates. Added some links
> - Define operations. Clarify some of the things that may stall an orderly shutdown
test/jdk/java/net/httpclient/offline/FixedResponseHttpClient.java line 302:
> 300: long timeout = duration.compareTo(max) > 0 ? Long.MAX_VALUE : duration.toMillis();
> 301: try {
> 302: all.exceptionally((t) -> null).get(timeout, TimeUnit.MILLISECONDS);
This implementation of `awaitTermination` only waits for the current (snapshot) of requests that are currently in progress. If a new request happens to be initiated (and is in-progress) after this snapshot of `responses.toArray()` was taken, then this implementation of `awaitTermination` can possibly return an incorrect `true` (indicating the client has terminated) while there currently might be some requests in progress. This `FixedResponseHttpClient` is just a test specific implementation which appears to be used in one single test class (`OfflineTesting`) and as far as I can see, that test won't be impacted with the implementation of `awaitTermination()`, so I think this implementation might be OK for now.
-------------
PR Review Comment: https://git.openjdk.org/jdk/pull/13019#discussion_r1145790403
More information about the net-dev
mailing list