Skip to content

Instantly share code, notes, and snippets.

@adamw
Last active June 7, 2018 11:19
Show Gist options
  • Select an option

  • Save adamw/250dc18a5afdbe7536f5227412bf7ca4 to your computer and use it in GitHub Desktop.

Select an option

Save adamw/250dc18a5afdbe7536f5227412bf7ca4 to your computer and use it in GitHub Desktop.
class MonixRateLimiter(queue: MVar[RateLimiterMsg], queueFiber: Fiber[Task, Unit]) {
def runLimited[T](f: Task[T]): Task[T] = {
for {
mv <- MVar.empty[T]
_ <- queue.put(Schedule(f.flatMap(mv.put)))
r <- mv.take
} yield r
}
}
object MonixRateLimiter extends StrictLogging {
def create(maxRuns: Int, per: FiniteDuration): Task[MonixRateLimiter] =
for {
queue <- MVar.empty[RateLimiterMsg]
runQueueFiber <-
runQueue(RateLimiterQueue[Task[Unit]](maxRuns, per.toMillis), queue)
.fork
} yield new MonixRateLimiter(queue, runQueueFiber)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment