Skip to content

Instantly share code, notes, and snippets.

@nambrot
Created December 2, 2017 23:34
Show Gist options
  • Save nambrot/cee1e4cf115a80ebadb59638c9bdb4b4 to your computer and use it in GitHub Desktop.
Save nambrot/cee1e4cf115a80ebadb59638c9bdb4b4 to your computer and use it in GitHub Desktop.
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