From bf52fd5052cbc66c503d2c6f8f82ed7c221a84f4 Mon Sep 17 00:00:00 2001 From: Matt Farmer Date: Wed, 11 Nov 2026 22:22:56 -0500 Subject: [PATCH] Document Http executor lifecycle and thread sharing - Add Scaladoc to Http.withConfiguration noting that each executor owns its own AHC client, pool, and threads, so it should be created once, reused, and shut down. - Replace docs/05/00 with a guide to Http executors: the default executor, custom configuration, thread cost, shutdown, and closeAndConfigure. - Add docs/05/a on capping I/O threads, sharing an event loop group and timer across executors, and naming threads. - Move the Scaladoc index to docs/06/00 so it follows the new pages. --- core/src/main/scala/execution.scala | 4 ++ docs/05/00.markdown | 90 +++++++++++++++++++++++++---- docs/05/a.markdown | 67 +++++++++++++++++++++ docs/06/00.markdown | 13 +++++ 4 files changed, 164 insertions(+), 10 deletions(-) create mode 100644 docs/05/a.markdown create mode 100644 docs/06/00.markdown diff --git a/core/src/main/scala/execution.scala b/core/src/main/scala/execution.scala index 77f46ee0..a3931854 100644 --- a/core/src/main/scala/execution.scala +++ b/core/src/main/scala/execution.scala @@ -61,6 +61,10 @@ object Http { /** * Create an Http executor with some custom configuration defined by the `withBuilder` function * that will mutate the underlying `Builder` from AHC. + * + * Each executor this returns gets its own AHC client, connection pool, and threads once it + * makes its first request. Create it once and reuse it, and call `shutdown()` when you're done + * with it. */ def withConfiguration(withBuilder: Builder => Builder): Http = { val newBuilder = new Builder(defaultClientBuilder.build) diff --git a/docs/05/00.markdown b/docs/05/00.markdown index efba504e..e1808ec2 100644 --- a/docs/05/00.markdown +++ b/docs/05/00.markdown @@ -1,13 +1,83 @@ -## Scaladoc Index +Http executors +-------------- -The following is an index of the available Dispatch Scaladocs by -Dispatch release series and module. +Every example so far has sent its requests through `Http.default`. +That's an *executor*: an instance of `Http` that wraps one +async-http-client (AHC) client, along with the connection pool and +threads that client owns. -### Dispatch 0.13.x +### The default executor -* [dispatch-core](/scaladocs/0.13/core) -* [dispatch-json4sjackson](/scaladocs/0.13/json4sjackson) -* [dispatch-json4snative](/scaladocs/0.13/json4snative) -* [dispatch-jsoup](/scaladocs/0.13/jsoup) -* [dispatch-liftjson](/scaladocs/0.13/liftjson) -* [dispatch-tagsoup](/scaladocs/0.13/tagsoup) +`Http.default` is one executor shared by your whole application. It's +safe to use from many threads at once, and most applications never +need another. + +### Configuring your own executor + +When the defaults don't suit you, `Http.withConfiguration` creates a +new executor. It hands you AHC's [config builder][builder] to change: + +[builder]: https://javadoc.io/doc/org.asynchttpclient/async-http-client/latest/org/asynchttpclient/DefaultAsyncHttpClientConfig.Builder.html + +```scala +val http = Http.withConfiguration(_.setFollowRedirect(true)) +val page = http(svc OK as.String) +``` + +Each executor you create has its own client, connection pool, and +threads, separate from `Http.default` and from every other executor. +Create it once, keep it, and reuse it. Creating one per request throws +away its connections and leaves its threads running: + +```scala +// Don't do this: each call starts a new client and new threads +def fetch(req: Req) = + Http.withConfiguration(_.setFollowRedirect(true))(req OK as.String) +``` + +If Netty logs `You are creating too many HashedWheelTimer instances`, +your application is probably doing this. Each executor in use has its +own timer, and Netty logs this once more than 64 timers exist at the +same time. + +### What an executor costs + +An executor doesn't start its client until its first request. After +that, it keeps: + +* one timer thread, for timeouts and cleaning up idle connections +* I/O threads, started as it opens connections, up to one per + available processor + +A single request starts two threads. A busy executor on an 8-core +machine can hold nine. These threads keep running until you shut the +executor down. + +### Shutting down + +AHC's threads aren't daemon threads, so an executor that's still +running keeps the JVM from exiting. When you're done with an executor, +shut it down to close its connections and stop its threads: + +```scala +http.shutdown() +``` + +An executor can't be restarted after it's shut down. Any request sent +through it afterward fails with an `ExecutionException` caused by +`IllegalStateException: Closed`. + +That applies to `Http.default` too. Everything in the JVM shares it, +so shut it down only when your application exits, and never from +library code. + +### Changing an executor's configuration + +`closeAndConfigure` shuts down an executor and returns a new one with +the same configuration plus your changes: + +```scala +val newHttp = http.closeAndConfigure(_.setUserAgent("my-app/1.0")) +``` + +Use the new executor from then on. The old one is shut down. diff --git a/docs/05/a.markdown b/docs/05/a.markdown new file mode 100644 index 00000000..dca88c9a --- /dev/null +++ b/docs/05/a.markdown @@ -0,0 +1,67 @@ +Limiting and sharing threads +---------------------------- + +AHC's config builder also controls the threads an executor uses. Like +any other setting, you change them with `Http.withConfiguration`. + +### Capping I/O threads + +`setIoThreadsCount` sets the most I/O threads an executor will start: + +```scala +val http = Http.withConfiguration(_.setIoThreadsCount(2)) +``` + +Each I/O thread serves many connections at once, so a capped executor +can still run many requests in parallel. + +### Sharing threads between executors + +If you need several executors with different settings, they can share +one set of I/O threads and one timer. Create a Netty event loop group +and timer yourself, then pass both to each executor: + +```scala +import io.netty.channel.MultiThreadIoEventLoopGroup +import io.netty.channel.nio.NioIoHandler +import io.netty.util.HashedWheelTimer + +val ioThreads = new MultiThreadIoEventLoopGroup(4, NioIoHandler.newFactory()) +val timer = new HashedWheelTimer() + +val apiHttp = Http.withConfiguration( + _.setEventLoopGroup(ioThreads).setNettyTimer(timer).setFollowRedirect(true) +) +val cdnHttp = Http.withConfiguration( + _.setEventLoopGroup(ioThreads).setNettyTimer(timer).setMaxConnections(200) +) +``` + +Together, these two executors use at most four I/O threads and one +timer thread. Each still has its own connection pool. + +Because you created the event loop group and timer, shutting down an +executor doesn't stop them. Shut down every executor that uses them, +then stop them yourself: + +```scala +apiHttp.shutdown() +cdnHttp.shutdown() +ioThreads.shutdownGracefully() +timer.stop() +``` + +### Naming threads + +`setThreadPoolName` sets the prefix of an executor's thread names, so +you can tell executors apart in a thread dump: + +```scala +val http = Http.withConfiguration(_.setThreadPoolName("billing-api")) +``` + +Its threads are then named like `billing-api-12-1` and +`billing-api-timer-11-1`. Without this setting, every executor's +threads start with `AsyncHttpClient`. The name only applies to threads +the executor creates itself, not to a shared event loop group or +timer. diff --git a/docs/06/00.markdown b/docs/06/00.markdown new file mode 100644 index 00000000..efba504e --- /dev/null +++ b/docs/06/00.markdown @@ -0,0 +1,13 @@ +## Scaladoc Index + +The following is an index of the available Dispatch Scaladocs by +Dispatch release series and module. + +### Dispatch 0.13.x + +* [dispatch-core](/scaladocs/0.13/core) +* [dispatch-json4sjackson](/scaladocs/0.13/json4sjackson) +* [dispatch-json4snative](/scaladocs/0.13/json4snative) +* [dispatch-jsoup](/scaladocs/0.13/jsoup) +* [dispatch-liftjson](/scaladocs/0.13/liftjson) +* [dispatch-tagsoup](/scaladocs/0.13/tagsoup)