jakub.dyszkiewicz
04/09/2019, 12:06 PM.sample(Duration)
?
https://projectreactor.io/docs/core/release/api/reactor/core/publisher/Flux.html#sample-java.time.Duration- on channel. I’d like to throttle consumer of channel to process the latest data not often than every 1sjakub.dyszkiewicz
04/09/2019, 2:15 PMval limiter = TimeBasedRateLimiter(Duration.ofSeconds(5))
suspend fun updateSnapshot(combinedState: Pair<State, State>) = limiter {
println("Updating snapshot")
println(combinedState)
}
class TimeBasedRateLimiter(val limit: Duration) {
private var lastInvocation: Long = 0
suspend operator fun invoke(fn: suspend () -> Unit) {
if ((System.currentTimeMillis() - lastInvocation) > limit.toMillis()) {
fn()
lastInvocation = System.currentTimeMillis()
}
}
}