This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2017-02-07
Channels
- # aleph (3)
- # aws (7)
- # beginners (117)
- # boot (119)
- # cider (2)
- # cljs-dev (3)
- # clojure (193)
- # clojure-austin (1)
- # clojure-dusseldorf (4)
- # clojure-finland (5)
- # clojure-france (5)
- # clojure-italy (7)
- # clojure-portugal (1)
- # clojure-russia (204)
- # clojure-serbia (5)
- # clojure-spec (31)
- # clojure-uk (64)
- # clojurescript (288)
- # community-development (9)
- # core-async (54)
- # cursive (8)
- # datascript (18)
- # datomic (26)
- # dirac (8)
- # emacs (26)
- # figwheel (1)
- # hoplon (16)
- # jobs (2)
- # jobs-discuss (4)
- # juxt (1)
- # lein-figwheel (4)
- # leiningen (14)
- # london-clojurians (2)
- # lumo (17)
- # off-topic (44)
- # om (63)
- # om-next (2)
- # onyx (26)
- # perun (14)
- # planck (5)
- # portland-or (34)
- # proton (2)
- # protorepl (8)
- # quil (1)
- # re-frame (6)
- # reagent (16)
- # remote-jobs (4)
- # ring (7)
- # ring-swagger (10)
- # rum (1)
- # untangled (2)
I think i managed to make a clean version of throttle-chan that doesn't need more than one go-loop and doesn't use atoms
(defn- throttle-chan [in-chan out-chan valve-chan]
(async/go-loop [allowance 0]
(if (< 0 allowance)
(do (async/>! out-chan (async/<! in-chan))
(recur (reduce (fn [val f] (f val)) allowance (take-while (complement nil?) (repeatedly (fn [] (async/poll! valve-chan)))))))
(let [f (async/<! valve-chan)]
(recur (reduce (fn [val f] (f val)) (f allowance) (take-while (complement nil?) (repeatedly (fn [] (async/poll! valve-chan))))))))))
@bcbradley Doesn't this have the issue of out-chan
potentially blocking valve-chan
?
Even in-chan
can block valve-chan
in this version AFAICT
You need to include all your takes and puts in an alt!
or alts!
(or perhaps you can even work something out with poll!
) to prevent that
@dergutemoritz i've thought about it for a while and came to the conclusion that because you must "hold" the value you get from the in-chan before sending it off to the out-chan
and because during that "hold" you could potentially receive a value from valve-chan unless you block it,
what would you do with the value you held if you are told from the change in allowance that you aren't allowed to push it to the out-chan?
basically, if you want to preserve the order, and you don't want to allow the valve chan to invalidate the thing you "hold"
if there were a way to forcibly push something backwards into a channel, it might be possible to rewind without reordering
OK if that's fine then I'm sure your function can be simplified 🙂
Well yeah, if an allowance change comes in while you wait for out-chan
to become available but you've already taken a value from in-chan
, the semantics are a bit hazy indeed 🙂
Not sure if blocking in this case is ideal either, though
What order exactly would be mucked with, though?
You would check the allowance after taking but before waiting for the put
If an allowance change comes in then, you would potentially put something at a point in time where the already taken value would not be allowed anymore but you would still put it into out-chan before any next value that comes through in-chan
Oh I thought you threw away values which were taken while allowance is 0
the allowance is basically how many values you will allow to go from in-chan to out-chan
So you don't want to lose values, yeah?
the issue is that someone could set the allowance to zero through the valve-chan, and expect it to freeze the flow of stuff from in-chan to out-chan
if you are in a state where you already took from in-chan and are ready to put to out-chan, thats where the rabbit hole starts
after working with different solutions for a couple days I finally came to the conclusion that the issue is my assumptions
it seems making that assumption isn't logically consistent with the semantics of a throttle
Yeah makes sense
These things can be really tricky, indeed
BTW, both your and mine don't currently properly handle closing of any of the channels involved
In fact, my version is a bit different: it will also allow changing the allowance when it's > 0 while nothing is coming in through in-chan