Last active
June 6, 2018 10:47
-
-
Save adamw/b25755161035a5a8b45fe1db4023f7e4 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
| class AkkaRateLimiter(rateLimiterActor: ActorRef) { | |
| def runLimited[T](f: => Future[T])(implicit ec: ExecutionContext): Future[T] = { | |
| val p = Promise[T] | |
| val msg = LazyFuture(() => f.andThen { case r => p.complete(r) }.map(_ => ())) | |
| rateLimiterActor ! msg | |
| p.future | |
| } | |
| } | |
| object AkkaRateLimiter { | |
| def create(maxRuns: Int, per: FiniteDuration)( | |
| implicit actorSystem: ActorSystem): AkkaRateLimiter = { | |
| val rateLimiterActor = actorSystem.actorOf( | |
| Props(new RateLimiterActor(maxRuns, per))) | |
| new AkkaRateLimiter(rateLimiterActor) | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment