aboutsummaryrefslogtreecommitdiff
path: root/async-http-client-handler
diff options
context:
space:
mode:
authorAdam Warski <adam@warski.org>2017-09-07 09:13:04 +0200
committerGitHub <noreply@github.com>2017-09-07 09:13:04 +0200
commit3c85a4a5acf7197c1822d4b6339168e36cf1b853 (patch)
tree6147dd38662760d14bc6161b692486e1f29f9b30 /async-http-client-handler
parent7188ebe102803c7d27c75b4640ded86a2ba7c6f6 (diff)
parent6874e55a316e4fe8a650efd3a849814a91bba8cb (diff)
downloadsttp-3c85a4a5acf7197c1822d4b6339168e36cf1b853.tar.gz
sttp-3c85a4a5acf7197c1822d4b6339168e36cf1b853.tar.bz2
sttp-3c85a4a5acf7197c1822d4b6339168e36cf1b853.zip
Merge pull request #28 from bhop/feature/request-timeout
Make request timeout configurable
Diffstat (limited to 'async-http-client-handler')
-rw-r--r--async-http-client-handler/cats/src/main/scala/com/softwaremill/sttp/asynchttpclient/cats/AsyncHttpClientCatsHandler.scala8
-rw-r--r--async-http-client-handler/fs2/src/main/scala/com/softwaremill/sttp/asynchttpclient/fs2/AsyncHttpClientFs2Handler.scala8
-rw-r--r--async-http-client-handler/future/src/main/scala/com/softwaremill/sttp/asynchttpclient/future/AsyncHttpClientFutureHandler.scala9
-rw-r--r--async-http-client-handler/monix/src/main/scala/com/softwaremill/sttp/asynchttpclient/monix/AsyncHttpClientMonixHandler.scala9
-rw-r--r--async-http-client-handler/scalaz/src/main/scala/com/softwaremill/sttp/asynchttpclient/scalaz/AsyncHttpClientScalazHandler.scala11
-rw-r--r--async-http-client-handler/src/main/scala/com/softwaremill/sttp/asynchttpclient/AsyncHttpClientHandler.scala18
6 files changed, 48 insertions, 15 deletions
diff --git a/async-http-client-handler/cats/src/main/scala/com/softwaremill/sttp/asynchttpclient/cats/AsyncHttpClientCatsHandler.scala b/async-http-client-handler/cats/src/main/scala/com/softwaremill/sttp/asynchttpclient/cats/AsyncHttpClientCatsHandler.scala
index 18949c0..9948d42 100644
--- a/async-http-client-handler/cats/src/main/scala/com/softwaremill/sttp/asynchttpclient/cats/AsyncHttpClientCatsHandler.scala
+++ b/async-http-client-handler/cats/src/main/scala/com/softwaremill/sttp/asynchttpclient/cats/AsyncHttpClientCatsHandler.scala
@@ -16,6 +16,7 @@ import org.asynchttpclient.{
}
import org.reactivestreams.Publisher
+import scala.concurrent.duration.FiniteDuration
import scala.language.higherKinds
class AsyncHttpClientCatsHandler[F[_]: Async] private (
@@ -46,8 +47,11 @@ object AsyncHttpClientCatsHandler {
new FollowRedirectsHandler[F, Nothing](
new AsyncHttpClientCatsHandler(asyncHttpClient, closeClient))
- def apply[F[_]: Async](): SttpHandler[F, Nothing] =
- AsyncHttpClientCatsHandler(new DefaultAsyncHttpClient(), closeClient = true)
+ def apply[F[_]: Async](connectionTimeout: FiniteDuration = SttpHandler.DefaultConnectionTimeout)
+ : SttpHandler[F, Nothing] =
+ AsyncHttpClientCatsHandler(
+ AsyncHttpClientHandler.defaultClient(connectionTimeout.toMillis.toInt),
+ closeClient = true)
def usingConfig[F[_]: Async](
cfg: AsyncHttpClientConfig): SttpHandler[F, Nothing] =
diff --git a/async-http-client-handler/fs2/src/main/scala/com/softwaremill/sttp/asynchttpclient/fs2/AsyncHttpClientFs2Handler.scala b/async-http-client-handler/fs2/src/main/scala/com/softwaremill/sttp/asynchttpclient/fs2/AsyncHttpClientFs2Handler.scala
index 56e7b8c..a33932b 100644
--- a/async-http-client-handler/fs2/src/main/scala/com/softwaremill/sttp/asynchttpclient/fs2/AsyncHttpClientFs2Handler.scala
+++ b/async-http-client-handler/fs2/src/main/scala/com/softwaremill/sttp/asynchttpclient/fs2/AsyncHttpClientFs2Handler.scala
@@ -21,6 +21,7 @@ import org.asynchttpclient.{
import org.reactivestreams.Publisher
import scala.concurrent.ExecutionContext
+import scala.concurrent.duration.FiniteDuration
import scala.language.higherKinds
class AsyncHttpClientFs2Handler[F[_]: Effect] private (
@@ -63,11 +64,12 @@ object AsyncHttpClientFs2Handler {
* e.g. mapping responses. Defaults to the global execution
* context.
*/
- def apply[F[_]: Effect]()(
+ def apply[F[_]: Effect](connectionTimeout: FiniteDuration = SttpHandler.DefaultConnectionTimeout)(
implicit ec: ExecutionContext = ExecutionContext.Implicits.global)
: SttpHandler[F, Stream[F, ByteBuffer]] =
- AsyncHttpClientFs2Handler[F](new DefaultAsyncHttpClient(),
- closeClient = true)
+ AsyncHttpClientFs2Handler[F](
+ AsyncHttpClientHandler.defaultClient(connectionTimeout.toMillis.toInt),
+ closeClient = true)
/**
* @param ec The execution context for running non-network related operations,
diff --git a/async-http-client-handler/future/src/main/scala/com/softwaremill/sttp/asynchttpclient/future/AsyncHttpClientFutureHandler.scala b/async-http-client-handler/future/src/main/scala/com/softwaremill/sttp/asynchttpclient/future/AsyncHttpClientFutureHandler.scala
index b80a91e..6e0a22c 100644
--- a/async-http-client-handler/future/src/main/scala/com/softwaremill/sttp/asynchttpclient/future/AsyncHttpClientFutureHandler.scala
+++ b/async-http-client-handler/future/src/main/scala/com/softwaremill/sttp/asynchttpclient/future/AsyncHttpClientFutureHandler.scala
@@ -11,6 +11,7 @@ import org.asynchttpclient.{
}
import org.reactivestreams.Publisher
+import scala.concurrent.duration.FiniteDuration
import scala.concurrent.{ExecutionContext, Future}
class AsyncHttpClientFutureHandler private (
@@ -44,10 +45,12 @@ object AsyncHttpClientFutureHandler {
* e.g. mapping responses. Defaults to the global execution
* context.
*/
- def apply()(implicit ec: ExecutionContext = ExecutionContext.Implicits.global)
+ def apply(connectionTimeout: FiniteDuration = SttpHandler.DefaultConnectionTimeout)(
+ implicit ec: ExecutionContext = ExecutionContext.Implicits.global)
: SttpHandler[Future, Nothing] =
- AsyncHttpClientFutureHandler(new DefaultAsyncHttpClient(),
- closeClient = true)
+ AsyncHttpClientFutureHandler(
+ AsyncHttpClientHandler.defaultClient(connectionTimeout.toMillis.toInt),
+ closeClient = true)
/**
* @param ec The execution context for running non-network related operations,
diff --git a/async-http-client-handler/monix/src/main/scala/com/softwaremill/sttp/asynchttpclient/monix/AsyncHttpClientMonixHandler.scala b/async-http-client-handler/monix/src/main/scala/com/softwaremill/sttp/asynchttpclient/monix/AsyncHttpClientMonixHandler.scala
index 311d3ea..7cfcb43 100644
--- a/async-http-client-handler/monix/src/main/scala/com/softwaremill/sttp/asynchttpclient/monix/AsyncHttpClientMonixHandler.scala
+++ b/async-http-client-handler/monix/src/main/scala/com/softwaremill/sttp/asynchttpclient/monix/AsyncHttpClientMonixHandler.scala
@@ -20,6 +20,7 @@ import org.asynchttpclient.{
}
import org.reactivestreams.Publisher
+import scala.concurrent.duration.FiniteDuration
import scala.util.{Failure, Success}
class AsyncHttpClientMonixHandler private (
@@ -61,10 +62,12 @@ object AsyncHttpClientMonixHandler {
* @param s The scheduler used for streaming request bodies. Defaults to the
* global scheduler.
*/
- def apply()(implicit s: Scheduler = Scheduler.Implicits.global)
+ def apply(connectionTimeout: FiniteDuration = SttpHandler.DefaultConnectionTimeout)(
+ implicit s: Scheduler = Scheduler.Implicits.global)
: SttpHandler[Task, Observable[ByteBuffer]] =
- AsyncHttpClientMonixHandler(new DefaultAsyncHttpClient(),
- closeClient = true)
+ AsyncHttpClientMonixHandler(
+ AsyncHttpClientHandler.defaultClient(connectionTimeout.toMillis.toInt),
+ closeClient = true)
/**
* @param s The scheduler used for streaming request bodies. Defaults to the
diff --git a/async-http-client-handler/scalaz/src/main/scala/com/softwaremill/sttp/asynchttpclient/scalaz/AsyncHttpClientScalazHandler.scala b/async-http-client-handler/scalaz/src/main/scala/com/softwaremill/sttp/asynchttpclient/scalaz/AsyncHttpClientScalazHandler.scala
index b470606..0e80a29 100644
--- a/async-http-client-handler/scalaz/src/main/scala/com/softwaremill/sttp/asynchttpclient/scalaz/AsyncHttpClientScalazHandler.scala
+++ b/async-http-client-handler/scalaz/src/main/scala/com/softwaremill/sttp/asynchttpclient/scalaz/AsyncHttpClientScalazHandler.scala
@@ -15,6 +15,7 @@ import org.asynchttpclient.{
}
import org.reactivestreams.Publisher
+import scala.concurrent.duration.FiniteDuration
import scalaz.{-\/, \/-}
import scalaz.concurrent.Task
@@ -42,12 +43,16 @@ object AsyncHttpClientScalazHandler {
new FollowRedirectsHandler[Task, Nothing](
new AsyncHttpClientScalazHandler(asyncHttpClient, closeClient))
- def apply(): SttpHandler[Task, Nothing] =
- AsyncHttpClientScalazHandler(new DefaultAsyncHttpClient(),
- closeClient = true)
+ def apply(connectionTimeout: FiniteDuration = SttpHandler.DefaultConnectionTimeout)
+ : SttpHandler[Task, Nothing] =
+ AsyncHttpClientScalazHandler(
+ AsyncHttpClientHandler.defaultClient(connectionTimeout.toMillis.toInt),
+ closeClient = true)
+
def usingConfig(cfg: AsyncHttpClientConfig): SttpHandler[Task, Nothing] =
AsyncHttpClientScalazHandler(new DefaultAsyncHttpClient(cfg),
closeClient = true)
+
def usingClient(client: AsyncHttpClient): SttpHandler[Task, Nothing] =
AsyncHttpClientScalazHandler(client, closeClient = false)
}
diff --git a/async-http-client-handler/src/main/scala/com/softwaremill/sttp/asynchttpclient/AsyncHttpClientHandler.scala b/async-http-client-handler/src/main/scala/com/softwaremill/sttp/asynchttpclient/AsyncHttpClientHandler.scala
index b6c9249..984ecf6 100644
--- a/async-http-client-handler/src/main/scala/com/softwaremill/sttp/asynchttpclient/AsyncHttpClientHandler.scala
+++ b/async-http-client-handler/src/main/scala/com/softwaremill/sttp/asynchttpclient/AsyncHttpClientHandler.scala
@@ -17,6 +17,8 @@ import org.asynchttpclient.{
AsyncCompletionHandler,
AsyncHandler,
AsyncHttpClient,
+ DefaultAsyncHttpClientConfig,
+ DefaultAsyncHttpClient,
HttpResponseBodyPart,
HttpResponseHeaders,
HttpResponseStatus,
@@ -155,7 +157,11 @@ abstract class AsyncHttpClientHandler[R[_], S](asyncHttpClient: AsyncHttpClient,
}
private def requestToAsync(r: Request[_, S]): AsyncRequest = {
- val rb = new RequestBuilder(r.method.m).setUrl(r.uri.toString)
+ val readTimeout = r.options.readTimeout
+ val rb = new RequestBuilder(r.method.m)
+ .setUrl(r.uri.toString)
+ .setRequestTimeout(
+ if (readTimeout.isFinite()) readTimeout.toMillis.toInt else -1)
r.headers.foreach { case (k, v) => rb.setHeader(k, v) }
setBody(r, r.body, rb)
rb.build()
@@ -289,6 +295,16 @@ abstract class AsyncHttpClientHandler[R[_], S](asyncHttpClient: AsyncHttpClient,
}
}
+object AsyncHttpClientHandler {
+
+ private[asynchttpclient] def defaultClient(connectionTimeout: Int): AsyncHttpClient =
+ new DefaultAsyncHttpClient(
+ new DefaultAsyncHttpClientConfig.Builder()
+ .setConnectTimeout(connectionTimeout)
+ .build()
+ )
+}
+
object EmptyPublisher extends Publisher[ByteBuffer] {
override def subscribe(s: Subscriber[_ >: ByteBuffer]): Unit = {
s.onComplete()