Created
December 7, 2018 17:37
-
-
Save kiambogo/f9f68e2e8497eee58d2147ad18b746c8 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
import $ivy.`org.systemfw::upperbound:0.2.0-M1` | |
import fs2._, concurrent._ | |
import upperbound._ | |
import cats.effect.{IO, ContextShift, Timer} | |
import upperbound.syntax.rate._ | |
import scala.concurrent.duration._ | |
import scala.concurrent.ExecutionContext | |
implicit val ec: ExecutionContext = ExecutionContext.global | |
implicit val ioContextShift: ContextShift[IO] = IO.contextShift(ec) | |
implicit val timer: Timer[IO] = IO.timer(ec) | |
val stream = for { | |
limiter <- Limiter.stream[IO](3 every 1.second) | |
queue <- Stream.eval(Queue.unbounded[IO, Int]) | |
worker = limiter.worker | |
_ <- queue.dequeue.evalMap(i => IO(println(i))) concurrently Stream.iterate(0)(_ + 1).take(30).evalMap(i => worker.submit(queue.enqueue1(i))) | |
} yield () | |
stream.compile.drain.unsafeRunSync |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Shows that the limiter distributes work over time (1 elem per period)