Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Raise throwables into body if streaming has begun #3521

Merged
merged 1 commit into from Jun 24, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -22,18 +22,14 @@ import org.asynchttpclient.handler.StreamedAsyncHandler
import org.asynchttpclient.request.body.generator.{BodyGenerator, ReactiveStreamsBodyGenerator}
import org.asynchttpclient.{Request => AsyncRequest, Response => _, _}
import org.http4s.internal.CollectionCompat.CollectionConverters._
import org.http4s.internal.invokeCallback
import org.http4s.internal.bug
import org.http4s.internal.threads._
import org.log4s.getLogger
import org.reactivestreams.Publisher
import _root_.io.netty.handler.codec.http.cookie.Cookie
import org.asynchttpclient.uri.Uri
import org.asynchttpclient.cookie.CookieStore

object AsyncHttpClient {
private[this] val logger = getLogger

val defaultConfig = new DefaultAsyncHttpClientConfig.Builder()
.setMaxConnectionsPerHost(200)
.setMaxConnections(400)
Expand Down Expand Up @@ -89,6 +85,7 @@ object AsyncHttpClient {
var response: Response[F] = Response()
val dispose = F.delay { state = State.ABORT }
val onStreamCalled = Ref.unsafe[F, Boolean](false)
val deferredThrowable = Deferred.unsafe[F, Throwable]

override def onStream(publisher: Publisher[HttpResponseBodyPart]): State = {
val eff = for {
Expand All @@ -106,6 +103,7 @@ object AsyncHttpClient {
subscriber
.stream(bodyDisposal.set(F.unit) >> subscribeF)
.flatMap(part => chunk(Chunk.bytes(part.getBodyPartBytes)))
.mergeHaltBoth(Stream.eval(deferredThrowable.get.flatMap(F.raiseError[Byte])))

responseWithBody = response.copy(body = body)

Expand All @@ -132,7 +130,12 @@ object AsyncHttpClient {
}

override def onThrowable(throwable: Throwable): Unit =
invokeCallback(logger)(cb(Left(throwable)))
onStreamCalled.get
.ifM(
ifTrue = deferredThrowable.complete(throwable),
ifFalse = invokeCallbackF(cb(Left(throwable))))
.runAsync(_ => IO.unit)
.unsafeRunSync()

override def onCompleted(): Unit =
onStreamCalled.get
Expand Down