Skip to content

Instantly share code, notes, and snippets.

@inanna-malick
Created December 3, 2014 15:06
Show Gist options
  • Save inanna-malick/7836aa4f1ea7d04eb033 to your computer and use it in GitHub Desktop.
Save inanna-malick/7836aa4f1ea7d04eb033 to your computer and use it in GitHub Desktop.
Throttle a flow using Akka Stream's new FlowGraph DSL
val rate = 200 millis
def throttled[T]: Flow[T, T] = {
val tickSource = TickSource(rate, rate, () => () )
val zip = Zip[T, Unit]
val in = UndefinedSource[T]
val out = UndefinedSink[T]
PartialFlowGraph{ implicit builder =>
import FlowGraphImplicits._
in ~> zip.left ~> Flow[(T,Unit)].map{ case (t, _) => t } ~> out
tickSource ~> zip.right
}.toFlow(in, out)
}
@akauppi
Copy link

akauppi commented Jun 2, 2016

Thanks. I think it's awkward that a .throttle method is not built-in in Akka Streams.

btw. note that the above is from the experimental akka streams era. See here for discussion on 2.4.6

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment