Skip to content

[http-client] Make clients AutoCloseable #512

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion http-client/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@
<dependency>
<groupId>io.avaje</groupId>
<artifactId>avaje-http-api</artifactId>
<version>2.9-SNAPSHOT</version>
<version>${project.version}</version>
<scope>test</scope>
</dependency>

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package io.avaje.http.client;

import java.lang.invoke.MethodHandles;
import java.lang.invoke.MethodType;
import java.lang.reflect.Type;
import java.net.http.HttpHeaders;
import java.net.http.HttpRequest;
Expand All @@ -9,6 +11,7 @@
import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.atomic.AtomicReference;
import java.util.concurrent.atomic.LongAccumulator;
import java.util.concurrent.atomic.LongAdder;
Expand Down Expand Up @@ -340,4 +343,17 @@ private String authToken() {
String maxResponseBody(String body) {
return body.length() > 1_000 ? body.substring(0, 1_000) + " <truncated> ..." : body;
}

@Override
public void close() {
if (Integer.getInteger("java.specification.version") >= 21) {
try {
MethodHandles.lookup()
.findVirtual(java.net.http.HttpClient.class, "close", MethodType.methodType(void.class))
.invokeExact(httpClient);
} catch (Throwable t) {
throw new IllegalStateException("Failed to close java.net.http.HttpClient instance");
}
}
}
}
16 changes: 14 additions & 2 deletions http-client/src/main/java/io/avaje/http/client/HttpClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import java.net.Authenticator;
import java.net.CookieHandler;
import java.net.ProxySelector;
import java.net.http.HttpRequest;
import java.time.Duration;
import java.util.Map;
import java.util.concurrent.Executor;
Expand Down Expand Up @@ -33,7 +32,7 @@
*
* }</pre>
*/
public interface HttpClient {
public interface HttpClient extends AutoCloseable {

/**
* Return the builder to config and build the client context.
Expand Down Expand Up @@ -93,6 +92,19 @@ static Builder builder() {
*/
HttpClient.Metrics metrics(boolean reset);

/**
* Note: invoking this method has no effect on JDK versions less than 21.
*
* <p>Initiates an orderly shutdown in which http requests previously submitted are run to
* completion, but no new requests will be accepted. Running a request to completion may involve
* running several operations in the background, including waiting for responses to be delivered.
* This method waits until all operations have completed execution and the client has terminated.
*
* @see {@linkplain java.net.http.HttpClient#close}
*/
@Override
void close();

/**
* Builds the HttpClient.
*
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package io.avaje.http.generator.client;

import io.avaje.http.generator.core.APContext;
import io.avaje.http.generator.core.BaseControllerWriter;
import io.avaje.http.generator.core.ControllerReader;
import io.avaje.http.generator.core.MethodReader;
Expand Down Expand Up @@ -62,12 +63,17 @@ private void writeMethods() {
for (final ClientMethodWriter methodWriter : methodList) {
methodWriter.write();
}
writer.append(" @Override").eol();
writer.append(" public void close() {").eol();
writer.append(" this.client.close();").eol();
writer.append(" }").eol();
}

private void writeClassStart() {
writer.append(AT_GENERATED).eol();
AnnotationUtil.writeAnnotations(writer, reader.beanType());
writer.append("public class %s%s implements %s {", shortName, suffix, shortName).eol().eol();

writer.append("public class %s%s implements %s, AutoCloseable {", shortName, suffix, shortName).eol().eol();

writer.append(" private final HttpClient client;").eol().eol();

Expand Down