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

streaming with Monix's Iterant #1098

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ lazy val iterateeVersion = "0.18.0"
lazy val refinedVersion = "0.9.4"
lazy val catsEffectVersion = "1.2.0"
lazy val fs2Version = "1.0.4"
lazy val monixVersion = "3.0.0-RC2"
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a non-RC version of Monix we can depend on?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like RC2 is the latest. Otherwise we'd have to depend on Monix 2.0 which doesn't have Iterant.


lazy val compilerOptions = Seq(
"-deprecation",
Expand Down Expand Up @@ -236,7 +237,7 @@ lazy val finch = project.in(file("."))
"io.circe" %% "circe-generic" % circeVersion
))
.aggregate(
core, fs2, iteratee, generic, argonaut, circe, benchmarks, test, jsonTest, examples, refined
core, fs2, iteratee, monix, generic, argonaut, circe, benchmarks, test, jsonTest, examples, refined
)
.dependsOn(core, iteratee, generic, circe)

Expand Down Expand Up @@ -264,6 +265,16 @@ lazy val fs2 = project
)
.dependsOn(core % "compile->compile;test->test")

lazy val monix = project
.settings(moduleName := "finchx-monix")
.settings(allSettings)
.settings(
libraryDependencies ++= Seq(
"io.monix" %% "monix" % monixVersion
)
)
.dependsOn(core % "compile->compile;test->test")

lazy val generic = project
.settings(moduleName := "finchx-generic")
.settings(allSettings)
Expand Down
85 changes: 85 additions & 0 deletions monix/src/main/scala/io/finch/monix/package.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
package io.finch

import _root_.monix.tail.Iterant
import cats.effect.{Effect, IO}
import com.twitter.io.{Buf, Pipe, Reader}
import com.twitter.util.Future
import io.finch.internal._
import java.nio.charset.Charset

package object monix extends IterantInstances {

implicit def iterantLiftReader[F[_]](implicit
F: Effect[F],
TE: ToAsync[Future, F]
): LiftReader[Iterant, F] =
new LiftReader[Iterant, F] {
final def apply[A](reader: Reader[Buf], process: Buf => A): Iterant[F, A] = {
def loop(): Iterant[F, A] = {
Iterant
.liftF(F.suspend(TE(reader.read())))
.flatMap {
case None => Iterant.empty
case Some(buf) => Iterant.eval(process(buf)) ++ loop()
}
}

loop().guarantee(F.delay(reader.discard()))
}
}

implicit def encodeJsonIterant[F[_]: Effect, A](implicit
A: Encode.Json[A]
): EncodeStream.Json[F, Iterant, A] =
new EncodeNewLineDelimitedIterant[F, A, Application.Json]

implicit def encodeSseIterant[F[_]: Effect, A](implicit
A: Encode.Aux[A, Text.EventStream]
): EncodeStream.Aux[F, Iterant, A, Text.EventStream] =
new EncodeNewLineDelimitedIterant[F, A, Text.EventStream]

implicit def encodeTextIterant[F[_]: Effect, A](implicit
A: Encode.Text[A]
): EncodeStream.Text[F, Iterant, A] =
new EncodeIterant[F, A, Text.Plain] {
override protected def encodeChunk(chunk: A, cs: Charset): Buf = A(chunk, cs)
}
}

trait IterantInstances {

protected final class EncodeNewLineDelimitedIterant[F[_]: Effect, A, CT <: String](implicit
A: Encode.Aux[A, CT]
) extends EncodeIterant[F, A, CT] {
protected def encodeChunk(chunk: A, cs: Charset): Buf =
A(chunk, cs).concat(newLine(cs))
}

protected abstract class EncodeIterant[F[_], A, CT <: String](implicit
F: Effect[F],
TE: ToAsync[Future, F]
) extends EncodeStream[F, Iterant, A] with (Either[Throwable, Unit] => IO[Unit]) {

type ContentType = CT

protected def encodeChunk(chunk: A, cs: Charset): Buf

def apply(cb: Either[Throwable, Unit]): IO[Unit] = IO.unit

def apply(s: Iterant[F, A], cs: Charset): F[Reader[Buf]] = {
val p = new Pipe[Buf]
val run = s
.map(chunk => encodeChunk(chunk, cs))
.mapEval(chunk => TE(p.write(chunk)))
.guarantee(F.suspend(TE(p.close())))
.completedL

F.productR(F.runAsync(run)(this).to[F])(F.pure(p))
}
}

implicit def encodeBufIterant[F[_]: Effect, CT <: String]: EncodeStream.Aux[F, Iterant, Buf, CT] =
new EncodeIterant[F, Buf, CT] {
protected def encodeChunk(chunk: Buf, cs: Charset): Buf = chunk
}
}
16 changes: 16 additions & 0 deletions monix/src/test/scala/io/finch/monix/MonixStreamingSpec.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package io.finch.monix

import _root_.monix.tail.Iterant
import cats.effect.IO
import com.twitter.io.Buf
import io.finch._
import org.scalatest.prop.GeneratorDrivenPropertyChecks

class MonixStreamingSpec extends FinchSpec with GeneratorDrivenPropertyChecks {

checkAll("Iterant.streamBody", StreamingLaws[Iterant, IO](
list => Iterant.fromList(list),
_.map(array => Buf.ByteArray.Owned(array)).toListL.unsafeRunSync()
).all)

}