|
1 | 1 | package dispatch |
2 | 2 |
|
3 | | -import org.jboss.netty.util.{Timer, HashedWheelTimer} |
4 | | -import org.jboss.netty.channel.socket.nio.{ |
5 | | - NioClientSocketChannelFactory, NioWorkerPool} |
6 | | -import java.util.{concurrent => juc} |
7 | | -import com.ning.http.client.{ |
8 | | - AsyncHttpClient, AsyncHttpClientConfig |
9 | | -} |
10 | | -import com.ning.http.client.providers.netty.NettyAsyncHttpProviderConfig |
| 3 | +import io.netty.util.{HashedWheelTimer, Timer} |
| 4 | +import org.asynchttpclient.DefaultAsyncHttpClientConfig.Builder |
| 5 | +import org.asynchttpclient._ |
11 | 6 |
|
12 | 7 | object Defaults { |
13 | 8 | implicit def executor = scala.concurrent.ExecutionContext.Implicits.global |
| 9 | + |
14 | 10 | implicit lazy val timer: Timer = InternalDefaults.timer |
15 | 11 | } |
16 | 12 |
|
17 | | -private [dispatch] object InternalDefaults { |
18 | | - /** true if we think we're runing un-forked in an sbt-interactive session */ |
19 | | - val inSbt = ( |
20 | | - for (group <- Option(Thread.currentThread.getThreadGroup)) |
21 | | - yield ( |
22 | | - group.getName == "trap.exit" // sbt version <= 0.13.0 |
23 | | - || group.getName.startsWith("run-main-group") // sbt 0.13.1+ |
24 | | - ) |
25 | | - ).getOrElse(false) |
26 | | - |
27 | | - private lazy val underlying = |
28 | | - if (inSbt) SbtProcessDefaults |
29 | | - else BasicDefaults |
| 13 | +private[dispatch] object InternalDefaults { |
| 14 | + private lazy val underlying = BasicDefaults |
30 | 15 |
|
31 | | - def client = new AsyncHttpClient(underlying.builder.build()) |
32 | | - lazy val timer = underlying.timer |
| 16 | + lazy val clientBuilder: Builder = underlying.builder |
| 17 | + lazy val timer: Timer = underlying.timer |
33 | 18 |
|
34 | 19 | private trait Defaults { |
35 | | - def builder: AsyncHttpClientConfig.Builder |
| 20 | + def builder: DefaultAsyncHttpClientConfig.Builder |
| 21 | + |
36 | 22 | def timer: Timer |
37 | 23 | } |
38 | 24 |
|
39 | 25 | /** Sets a user agent, no timeout for requests */ |
40 | 26 | private object BasicDefaults extends Defaults { |
41 | 27 | lazy val timer = new HashedWheelTimer() |
42 | | - def builder = new AsyncHttpClientConfig.Builder() |
| 28 | + |
| 29 | + def builder: Builder = new DefaultAsyncHttpClientConfig.Builder() |
43 | 30 | .setUserAgent("Dispatch/%s" format BuildInfo.version) |
44 | 31 | .setRequestTimeout(-1) // don't timeout streaming connections |
45 | 32 | .setUseProxyProperties(true) |
46 | 33 | } |
47 | 34 |
|
48 | | - /** Uses daemon threads and tries to exit cleanly when running in sbt */ |
49 | | - private object SbtProcessDefaults extends Defaults { |
50 | | - def builder = { |
51 | | - val shuttingDown = new juc.atomic.AtomicBoolean(false) |
52 | | - |
53 | | - def shutdown(): Unit = { |
54 | | - if (shuttingDown.compareAndSet(false, true)) { |
55 | | - nioClientSocketChannelFactory.releaseExternalResources() |
56 | | - timer.stop() |
57 | | - } |
58 | | - () |
59 | | - } |
60 | | - /** daemon threads that also shut down everything when interrupted! */ |
61 | | - lazy val interruptThreadFactory = new juc.ThreadFactory { |
62 | | - def newThread(runnable: Runnable) = { |
63 | | - new Thread(runnable) { |
64 | | - setDaemon(true) |
65 | | - /** only reliably called on any thread if all spawned threads are daemon */ |
66 | | - override def interrupt() = { |
67 | | - shutdown() |
68 | | - super.interrupt() |
69 | | - } |
70 | | - } |
71 | | - } |
72 | | - } |
73 | | - lazy val nioClientSocketChannelFactory = { |
74 | | - val workerCount = 2 * Runtime.getRuntime().availableProcessors() |
75 | | - new NioClientSocketChannelFactory( |
76 | | - juc.Executors.newCachedThreadPool(interruptThreadFactory), |
77 | | - 1, |
78 | | - new NioWorkerPool( |
79 | | - juc.Executors.newCachedThreadPool(interruptThreadFactory), |
80 | | - workerCount |
81 | | - ), |
82 | | - timer |
83 | | - ) |
84 | | - } |
85 | | - |
86 | | - val config = new NettyAsyncHttpProviderConfig().addProperty( |
87 | | - "socketChannelFactory", |
88 | | - nioClientSocketChannelFactory |
89 | | - ) |
90 | | - config.setNettyTimer(timer) |
91 | | - BasicDefaults.builder.setAsyncHttpClientProviderConfig(config) |
92 | | - } |
93 | | - lazy val timer = new HashedWheelTimer(DaemonThreads.factory) |
94 | | - } |
95 | | -} |
96 | | - |
97 | | -object DaemonThreads { |
98 | | - /** produces daemon threads that won't block JVM shutdown */ |
99 | | - val factory = new juc.ThreadFactory { |
100 | | - def newThread(runnable: Runnable): Thread ={ |
101 | | - val thread = new Thread(runnable) |
102 | | - thread.setDaemon(true) |
103 | | - thread |
104 | | - } |
105 | | - } |
106 | | - def apply(threadPoolSize: Int) = |
107 | | - juc.Executors.newFixedThreadPool(threadPoolSize, factory) |
108 | 35 | } |
0 commit comments