Skip to content

Commit b27904d

Browse files
Merge pull request #149 from fbascheper/feature/upgrade-AHC-2.0.32
Upgrade to AsyncHttpClient 2.0.32
2 parents 46262f1 + 9df52ba commit b27904d

File tree

30 files changed

+161
-215
lines changed

30 files changed

+161
-215
lines changed

README.markdown

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@ as its underlying transport. For more info, see the
1111
[docs]: http://dispatch.databinder.net/Dispatch.html
1212
[async]: https://github.com/AsyncHttpClient/async-http-client
1313

14+
### Dependencies
15+
* [Async HTTP client](https://github.com/AsyncHttpClient/async-http-client)
16+
* JDK 8, as required by the Async HTTP client library
17+
1418
### Mailing List
1519

1620
There's a [mailing list for Dispatch][mail]. Please mail the list **before opening

build.sbt

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,7 @@ lazy val tagsoup = module("tagsoup")
4848
lazy val xmlDependency = libraryDependencies ++= Seq("org.scala-lang.modules" %% "scala-xml" % "1.0.6")
4949

5050
/** Util module for using unfiltered with scalacheck */
51-
lazy val ufcheck = Project(
52-
"ufcheck", file("ufcheck")
53-
).settings(
51+
lazy val ufcheck = project.in(file("ufcheck")).settings(
5452
scalaVersion := Common.defaultScalaVersion
5553
)
5654

core/build.sbt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
name := "dispatch-core"
22

33
description :=
4-
"Core Dispatch module wrapping sonatype/async-http-client"
4+
"Core Dispatch module wrapping async-http-client"
55

66
libraryDependencies +=
7-
"com.ning" % "async-http-client" % "1.9.11"
7+
"org.asynchttpclient" % "async-http-client" % "2.0.32"
88

99
Seq(lsSettings :_*)
1010

core/src/main/scala/as/core.scala

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,36 +2,36 @@ package dispatch.as
22

33
import dispatch._
44

5-
import com.ning.http.client
65
import java.nio.charset.Charset
6+
import org.asynchttpclient
77

88
object Response {
9-
def apply[T](f: client.Response => T) = f
9+
def apply[T](f: asynchttpclient.Response => T) = f
1010
}
1111

12-
object String extends (client.Response => String) {
12+
object String extends (asynchttpclient.Response => String) {
1313
/** @return response body as a string decoded as either the charset provided by
1414
* Content-Type header of the response or ISO-8859-1 */
15-
def apply(r: client.Response) = r.getResponseBody
15+
def apply(r: asynchttpclient.Response) = r.getResponseBody
1616

1717
/** @return a function that will return response body decoded in the provided charset */
18-
case class charset(set: Charset) extends (client.Response => String) {
19-
def apply(r: client.Response) = r.getResponseBody(set.name)
18+
case class charset(set: Charset) extends (asynchttpclient.Response => String) {
19+
def apply(r: asynchttpclient.Response) = r.getResponseBody(set)
2020
}
2121

2222
/** @return a function that will return response body as a utf8 decoded string */
2323
object utf8 extends charset(Charset.forName("utf8"))
2424
}
2525

26-
object Bytes extends (client.Response => Array[Byte]) {
27-
def apply(r: client.Response) = r.getResponseBodyAsBytes
26+
object Bytes extends (asynchttpclient.Response => Array[Byte]) {
27+
def apply(r: asynchttpclient.Response) = r.getResponseBodyAsBytes
2828
}
2929

3030
object File extends {
3131
def apply(file: java.io.File) =
32-
(new client.resumable.ResumableAsyncHandler with OkHandler[Nothing])
32+
(new asynchttpclient.handler.resumable.ResumableAsyncHandler with OkHandler[asynchttpclient.Response])
3333
.setResumableListener(
34-
new client.extra.ResumableRandomAccessFileListener(
34+
new asynchttpclient.handler.resumable.ResumableRandomAccessFileListener(
3535
new java.io.RandomAccessFile(file, "rw")
3636
)
3737
)

core/src/main/scala/as/oauth/token.scala

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
package dispatch.as.oauth
22

3-
import dispatch.oauth._
4-
import com.ning.http.client.Response
5-
import com.ning.http.client.oauth._
3+
import org.asynchttpclient.Response
4+
import org.asynchttpclient.oauth.RequestToken
65

76
object Token extends (Response => Either[String, RequestToken]) {
87
def apply(res: Response) = tokenDecode(dispatch.as.String(res))

core/src/main/scala/as/stream/lines.scala

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@ package dispatch.as.stream
22

33
import dispatch._
44

5-
import com.ning.http.client._
6-
75
object Lines {
86
def apply[U](f: String => U) =
97
new stream.StringsByLine[Unit] {

core/src/main/scala/defaults.scala

Lines changed: 12 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -1,108 +1,35 @@
11
package dispatch
22

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._
116

127
object Defaults {
138
implicit def executor = scala.concurrent.ExecutionContext.Implicits.global
9+
1410
implicit lazy val timer: Timer = InternalDefaults.timer
1511
}
1612

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
3015

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
3318

3419
private trait Defaults {
35-
def builder: AsyncHttpClientConfig.Builder
20+
def builder: DefaultAsyncHttpClientConfig.Builder
21+
3622
def timer: Timer
3723
}
3824

3925
/** Sets a user agent, no timeout for requests */
4026
private object BasicDefaults extends Defaults {
4127
lazy val timer = new HashedWheelTimer()
42-
def builder = new AsyncHttpClientConfig.Builder()
28+
29+
def builder: Builder = new DefaultAsyncHttpClientConfig.Builder()
4330
.setUserAgent("Dispatch/%s" format BuildInfo.version)
4431
.setRequestTimeout(-1) // don't timeout streaming connections
4532
.setUseProxyProperties(true)
4633
}
4734

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)
10835
}

core/src/main/scala/enrich.scala

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,7 @@
11
package dispatch
22

3-
import com.ning.http.client.ListenableFuture
4-
import java.util.{concurrent => juc}
5-
import juc.TimeUnit
63
import scala.concurrent.{ExecutionContext,Await,ExecutionException}
74
import scala.concurrent.duration.Duration
8-
import scala.util.control.Exception.{allCatch,catching}
95

106
class EnrichedFuture[A](underlying: Future[A]) {
117

core/src/main/scala/execution.scala

Lines changed: 26 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,39 @@
11
package dispatch
22

3-
import com.ning.http.client.{
4-
AsyncHttpClient, Request, Response, AsyncHandler,
5-
AsyncHttpClientConfig
6-
}
7-
83
import java.util.{concurrent => juc}
9-
import scala.concurrent.{ExecutionContext}
4+
5+
import org.asynchttpclient._
6+
7+
import scala.concurrent.ExecutionContext
8+
import scala.util.Try
109

1110
/** Http executor with defaults */
1211
case class Http(
13-
client: AsyncHttpClient = InternalDefaults.client
14-
) extends HttpExecutor {
15-
import AsyncHttpClientConfig.Builder
12+
clientBuilder: DefaultAsyncHttpClientConfig.Builder = InternalDefaults.clientBuilder
13+
) extends HttpExecutor {
1614

17-
/** Replaces `client` with a new instance configured using the withBuilder
18-
function. The current client config is the builder's prototype. */
19-
def configure(withBuilder: Builder => Builder) =
20-
copy(client =
21-
new AsyncHttpClient(withBuilder(
22-
new AsyncHttpClientConfig.Builder(client.getConfig)
23-
).build)
24-
)
15+
import DefaultAsyncHttpClientConfig.Builder
16+
17+
lazy val client = new DefaultAsyncHttpClient(clientBuilder.build)
18+
19+
/*§*
20+
* Replaces `clientBuilder` with a new config built using the withBuilder function.
21+
* The current client config is the builder's prototype.
22+
*/
23+
def configure(withBuilder: Builder => Builder): Http = {
24+
val newBuilder = new Builder(this.clientBuilder.build)
25+
copy(clientBuilder = withBuilder(newBuilder))
26+
}
2527
}
2628

2729
/** Singleton default Http executor, can be used directly or altered
28-
* with its case-class `copy` */
30+
* with its case-class `copy` */
2931
object Http extends Http(
30-
InternalDefaults.client
32+
InternalDefaults.clientBuilder
3133
)
3234

33-
trait HttpExecutor { self =>
35+
trait HttpExecutor {
36+
self =>
3437
def client: AsyncHttpClient
3538

3639
def apply(req: Req)
@@ -42,12 +45,12 @@ trait HttpExecutor { self =>
4245
apply(pair._1, pair._2)
4346

4447
def apply[T]
45-
(request: Request, handler: AsyncHandler[T])
46-
(implicit executor: ExecutionContext): Future[T] = {
48+
(request: Request, handler: AsyncHandler[T])
49+
(implicit executor: ExecutionContext): Future[T] = {
4750
val lfut = client.executeRequest(request, handler)
4851
val promise = scala.concurrent.Promise[T]()
4952
lfut.addListener(
50-
() => promise.complete(util.Try(lfut.get())),
53+
() => promise.complete(Try(lfut.get())),
5154
new juc.Executor {
5255
def execute(runnable: Runnable) = {
5356
executor.execute(runnable)

core/src/main/scala/handlers.scala

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package dispatch
22

3-
import com.ning.http.client
4-
import client.{
3+
import org.asynchttpclient.{
54
Response, AsyncCompletionHandler, AsyncHandler,
65
HttpResponseStatus
76
}

0 commit comments

Comments
 (0)