rogergl
rogergl

Reputation: 3781

How to limit write frequency to core.async channel?

Is there an easy way to limit the write frequency to a core.async channel ? So far I could find two examples. One uses a sliding-buffer and some SetTimeout magic to handle this the other uses an external atom as a counter. I would have expected that core.async provides such functionality out of the box. Since one of the examples is rather old (10 month) I would like to know if theres is maybe an easier solution ?

I'm looking for a solution that works with Clojure and ClojureScript.

Upvotes: 7

Views: 339

Answers (1)

noisesmith
noisesmith

Reputation: 20194

The throttler lib provides functions to impose rate controls on Clojure code (including throttles on channels or functions).

From the readme:

(def in (chan 1))
(def slow-chan (throttle-chan in 1 :millisecond)) ; 1 msg/ms

(>!! in :token) ; => true
(<!! slow-chan) ; :token

Upvotes: 2

Related Questions