Created
December 2, 2017 23:34
-
-
Save nambrot/cee1e4cf115a80ebadb59638c9bdb4b4 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
val rateLimiter = | |
Flow[(Integer, String)] | |
.statefulMapConcat { () => | |
val hashMap = new mutable.HashMap[Integer, (Long, String)]() | |
(el) => el match { | |
case (userId, alert) => { | |
val current = System.currentTimeMillis() | |
hashMap.get(userId) match { | |
case Some((oldTime, oldAlert)) => { | |
if (oldTime + 15.seconds.toMillis < current) { | |
hashMap.put(userId, (current, alert)) | |
List[(Integer, String)]((userId, alert)) | |
} else | |
List[(Integer, String)]() | |
} | |
case None => { | |
hashMap.put(userId, (current, alert)) | |
List[(Integer, String)]((userId, alert)) | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment