Skip to content

Instantly share code, notes, and snippets.

@kiambogo
Created December 7, 2018 17:37
Show Gist options
  • Save kiambogo/f9f68e2e8497eee58d2147ad18b746c8 to your computer and use it in GitHub Desktop.
Save kiambogo/f9f68e2e8497eee58d2147ad18b746c8 to your computer and use it in GitHub Desktop.
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
@kiambogo
Copy link
Author

kiambogo commented Dec 7, 2018

Shows that the limiter distributes work over time (1 elem per period)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment