Skip to content

Instantly share code, notes, and snippets.

@adamw
Last active June 6, 2018 10:53
Show Gist options
  • Select an option

  • Save adamw/59d21aa5cf9dbe9baa2949b2b80ca511 to your computer and use it in GitHub Desktop.

Select an option

Save adamw/59d21aa5cf9dbe9baa2949b2b80ca511 to your computer and use it in GitHub Desktop.
def rateLimit(timer: TimerScheduler[RateLimiterMsg],
data: RateLimiterQueue[LazyFuture]): Behavior[RateLimiterMsg] =
Behaviors.receiveMessage {
case lf: LazyFuture => rateLimit(timer, runQueue(timer, data.enqueue(lf)))
case ScheduledRunQueue => rateLimit(timer, runQueue(timer, data.notScheduled))
}
def runQueue(timer: TimerScheduler[RateLimiterMsg],
data: RateLimiterQueue[LazyFuture]): RateLimiterQueue[LazyFuture] = {
val now = System.currentTimeMillis()
val (tasks, data2) = data.run(now)
tasks.foreach {
case Run(LazyFuture(f)) => f()
case RunAfter(millis) => timer.startSingleTimer((), ScheduledRunQueue,
millis.millis)
}
data2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment