Last active
June 6, 2018 10:53
-
-
Save adamw/59d21aa5cf9dbe9baa2949b2b80ca511 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
| 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