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

Use Cats Effect EC as HttpClient executor #641

Merged
merged 5 commits into from
May 22, 2022

Conversation

armanbilge
Copy link
Member

@armanbilge armanbilge commented May 22, 2022

I'm not 100% sure on this change yet, but I'm pretty sure this executor is not used for blocking operations. In which case, we should definitely prefer to use the Cats Effect compute pool here. It means one less threadpool in the application and thus less contention and less page faults, which is a big win.

        /**
         * Sets the executor to be used for asynchronous and dependent tasks.
         *
         * <p> If this method is not invoked prior to {@linkplain #build()
         * building}, a default executor is created for each newly built {@code
         * HttpClient}.

https://github.com/openjdk/jdk11/blob/37115c8ea4aff13a8148ee2b8832b20888a5d880/src/java.net.http/share/classes/java/net/http/HttpClient.java#L253

@mergify mergify bot added the core label May 22, 2022
Comment on lines 266 to 269
ec match {
case exec: util.concurrent.Executor => builder.executor(exec)
case _ => builder.executor(ec.execute)
}
Copy link
Member Author

@armanbilge armanbilge May 22, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hopefully we will upstream this into Cats Effect.

@armanbilge armanbilge closed this May 22, 2022
@armanbilge armanbilge reopened this May 22, 2022
@armanbilge
Copy link
Member Author

So I've been poking around in the JDK sources and again, I'm pretty sure this makes sense. I think it would be strange to be running block ops on this, but I'm no expert 😅

https://github.com/openjdk/jdk11/blob/37115c8ea4aff13a8148ee2b8832b20888a5d880/src/java.net.http/share/classes/jdk/internal/net/http/HttpClientImpl.java

build.sbt Outdated Show resolved Hide resolved
Copy link
Member

@amesgen amesgen left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree that it seems unplausible that the Executor is asked to running blocking tasks. Another point in favor of this is that by default, Executors.newCachedThreadPool is used:

These pools will typically improve the performance of programs that execute many short-lived asynchronous tasks.


Do you think it would be a good idea to update the documentation at https://jdk-http-client.http4s.org/0.7/#custom-clients to also set the executor like this? Or do we want to wait for typelevel/cats-effect#3001?

@armanbilge
Copy link
Member Author

Oh, good eye! Yup, this is almost definitely the right thing to do then.

Good point, I can update the docs. We can just demonstrate it with wrapping for now, since the default work-stealing threadpool in CE isn't even an Executor until that PR 😅

@mergify mergify bot added the docs label May 22, 2022
Copy link
Member

@amesgen amesgen left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Approved, this can be followed up on when a release with typelevel/cats-effect#3001 lands 👍

@amesgen amesgen merged commit 497745e into http4s:series/0.7 May 22, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants