Skip to content
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
4 changes: 4 additions & 0 deletions core/src/main/scala/execution.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Comment on lines +65 to +67
*/
def withConfiguration(withBuilder: Builder => Builder): Http = {
val newBuilder = new Builder(defaultClientBuilder.build)
Expand Down
90 changes: 80 additions & 10 deletions docs/05/00.markdown
Original file line number Diff line number Diff line change
@@ -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.
67 changes: 67 additions & 0 deletions docs/05/a.markdown
Original file line number Diff line number Diff line change
@@ -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())
Comment on lines +25 to +29
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.
13 changes: 13 additions & 0 deletions docs/06/00.markdown
Original file line number Diff line number Diff line change
@@ -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)
Loading