Tuesday, 14 October 2014

Dynamic typing - Sampler filter

This post is superficially related to the basic concept of the previous, but shows a very similar pattern to Circuit Breaker. I call it Sampler filter, but I'm pretty sure there is an established name in the field of electrical engineering.
By implementing a Swing UI of my Mars Rover project (source code is here) I recently faced the problem that if I run hundreds of rovers, then refreshing the panel after every rover position change is impossible. Each position change puts an event - containing the positions for all rovers - on the channel the displayer logic listens to, and the messages just kept piling up. It's a typical IO bottleneck. So I decided it's enough to display every 100th or 1000th of them. They would change faster than my eyes can process them anyway. So I took the original function performing the display - repaint! - and wrapped it in a sampler. Here is the code


(defn repaint!
"Refreshes the Swing JPanel"
[positions-msg]
...)
(defn sampler-filter
"Wraps around a function returning a new function with the same signature, which delegates to the original function, but only
with the given frequency
f - the original function
freq - frequency
E.g. 'f' is 'println' and 'freq' is 3, then calling the result function only will print at every 3rd time"
[f freq]
(let [counter (atom 0)]
(fn [& args]
(swap! counter inc)
(when (= freq @counter)
(reset! counter 0)
(apply f args)))))
;; this function will only perform repaint at avery 1000th invocation
(def sampling-repaint! (sampler-filter repaint! 1000))

No comments :

Post a Comment