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

Use MonadCancel in the Http1Writer.write #5600

Merged
merged 2 commits into from Nov 21, 2021
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 @@ -18,6 +18,8 @@ package org.http4s
package blazecore
package util

import cats.effect.kernel.Outcome
import cats.effect.syntax.monadCancel._
import cats.syntax.all._
import org.http4s.util.StringWriter
import org.log4s.getLogger
Expand All @@ -28,16 +30,16 @@ import scala.concurrent._

private[http4s] trait Http1Writer[F[_]] extends EntityBodyWriter[F] {
final def write(headerWriter: StringWriter, body: EntityBody[F]): F[Boolean] =
fromFutureNoShift(F.delay(writeHeaders(headerWriter))).attempt.flatMap {
case Right(()) =>
writeEntityBody(body)
case Left(t) =>
body.drain.compile.drain.handleError { t2 =>
// Don't lose this error when sending the other
// TODO implement with cats.effect.Bracket when we have it
Http1Writer.logger.error(t2)("Error draining body")
} *> F.raiseError(t)
}
fromFutureNoShift(F.delay(writeHeaders(headerWriter)))
.guaranteeCase {
case Outcome.Succeeded(_) =>
F.unit

case Outcome.Errored(_) | Outcome.Canceled() =>
body.drain.compile.drain.handleError { t2 =>
Http1Writer.logger.error(t2)("Error draining body")
}
} >> writeEntityBody(body)

/* Writes the header. It is up to the writer whether to flush immediately or to
* buffer the header with a subsequent chunk. */
Expand Down