-
Notifications
You must be signed in to change notification settings - Fork 221
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
alexeygorobets
wants to merge
1
commit into
finagle:master
Choose a base branch
from
alexeygorobets:streaming-with-monix-iterant
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
16
monix/src/test/scala/io/finch/monix/MonixStreamingSpec.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
|
||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.