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

Stream - BroadcastThrough - refactor code #3254

Merged
merged 2 commits into from
Jul 27, 2023
Merged
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
54 changes: 25 additions & 29 deletions core/shared/src/main/scala/fs2/Stream.scala
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import cats.data.Ior
import cats.effect.Concurrent
import cats.effect.kernel._
import cats.effect.kernel.implicits._
import cats.effect.std.{Console, Queue, QueueSink, QueueSource, Semaphore}
import cats.effect.std.{Console, CountDownLatch, Queue, QueueSink, QueueSource, Semaphore}
import cats.effect.Resource.ExitCase
import cats.syntax.all._
import fs2.compat._
Expand Down Expand Up @@ -231,37 +231,33 @@ final class Stream[+F[_], +O] private[fs2] (private[fs2] val underlying: Pull[F,
* 2. chunks from each pipe come out of the resulting stream in the same
* order as they came out of the pipe, and without skipping any chunk.
*/
def broadcastThrough[F2[x] >: F[x]: Concurrent, O2](
pipes: Pipe[F2, O, O2]*
): Stream[F2, O2] = {
def broadcastThrough[F2[x] >: F[x]: Concurrent, O2](pipes: Pipe[F2, O, O2]*): Stream[F2, O2] = {
assert(pipes.nonEmpty, s"pipes should not be empty")
Stream
.eval {
(
cats.effect.std.CountDownLatch[F2](pipes.length),
fs2.concurrent.Topic[F2, Chunk[O]]
).tupled
}
.flatMap { case (latch, topic) =>
def produce = chunks.through(topic.publish)

def consume(pipe: Pipe[F2, O, O2]): Pipe[F2, Chunk[O], O2] =
_.unchunks.through(pipe)

Stream(pipes: _*)
.map { pipe =>
Stream
.resource(topic.subscribeAwait(1))
.flatMap { sub =>
// crucial that awaiting on the latch is not passed to
// the pipe, so that the pipe cannot interrupt it and alter
// the latch count
Stream.exec(latch.release >> latch.await) ++ sub.through(consume(pipe))
}
Stream.force {
for {
// topic: contains the chunk that the pipes are processing at one point.
// until and unless all pipes are finished with it, won't move to next one
topic <- Topic[F2, Chunk[O]]
// Coordination: neither the producer nor any consumer starts
// until and unless all consumers are subscribed to topic.
allReady <- CountDownLatch[F2](pipes.length)
} yield {
val checkIn = allReady.release >> allReady.await

def dump(pipe: Pipe[F2, O, O2]): Stream[F2, O2] =
Stream.resource(topic.subscribeAwait(1)).flatMap { sub =>
// Wait until all pipes are ready before consuming.
// Crucial: checkin is not passed to the pipe,
// so pipe cannot interrupt it and alter the latch count
Stream.exec(checkIn) ++ pipe(sub.unchunks)
}
.parJoinUnbounded
.concurrently(Stream.eval(latch.await) ++ produce)

val dumpAll: Stream[F2, O2] = Stream(pipes: _*).map(dump).parJoinUnbounded
// Wait until all pipes are checked in before pulling
val pump = Stream.exec(allReady.await) ++ topic.publish(chunks)
dumpAll.concurrently(pump)
}
}
}

/** Behaves like the identity function, but requests `n` elements at a time from the input.
Expand Down