Skip to content
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

Java 21+: Ensure simple client is closed #1011

Open
ybasket opened this issue Jan 31, 2024 · 4 comments · May be fixed by #1015
Open

Java 21+: Ensure simple client is closed #1011

ybasket opened this issue Jan 31, 2024 · 4 comments · May be fixed by #1015

Comments

@ybasket
Copy link

ybasket commented Jan 31, 2024

Java 21 adds methods to close a HttpClient and also makes it implement AutoCloseable: https://bugs.openjdk.org/browse/JDK-8267140

Ideally, http4s-jdk-http-client should leverage this and expose the simple client wrapped in a Resource to ensure the client is properly closed and cleaned up after use. Not sure how this can be achieved without requiring Java 21+ though, needs research.

@armanbilge
Copy link
Member

We can just use reflection. I don't think it would be a hot-path.

The bigger issue is that our signatures currently return F[_] instead of Resource. This would have to be a breaking change 😕

def simple[F[_]](implicit F: Async[F]): F[Client[F]] =
defaultHttpClient[F].map(apply(_))

@rossabaker
Copy link
Member

Does it need reflection, or can it just pattern match on AutoCloseable?

Why does it need to be a breaking change instead of another constructor? The bigger problem I see is that the resource release would be a no-op on older JDK.

@armanbilge
Copy link
Member

Does it need reflection, or can it just pattern match on AutoCloseable?

👍 smart

Why does it need to be a breaking change instead of another constructor?

You're right, it doesn't. See a prior discussion on a similar issue where we decided to break: #770 (comment).

@rossabaker
Copy link
Member

That pattern match might require a nowarn when compiled on JDK21 for an unreachable fallback case.

ybasket added a commit to ybasket/http4s-jdk-http-client that referenced this issue Feb 3, 2024
As Java 21+ allows explicitly closing clients, this adds new constructors that return the clients wrapped in a Resource. On older Java versions, the release is a no-op, on newer it enforces the cleanup of resources.

Also ensures the websocket client closes the input channel on release so the connection gets properly closed.

Closes http4s#1011
@ybasket ybasket linked a pull request Feb 3, 2024 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants