brunoV / throttler

Control the throughput of function calls and core.async channels
286 stars 27 forks source link

Throttle based on concurrency #5

Open ludwik opened 10 years ago

ludwik commented 10 years ago

feature request: consider a plain concurrency limit (so eg: max burst of 1000 but at most 50 at a time).

Really like this library btw, good job!

brunoV commented 10 years ago

Hey @ludwik! I'm glad you're liking throttler.

Actually, I have your feature request already implemented in a local branch already. For life-related reasons I haven't buckled down and pushed it. This feature request may be what I needed to revisit that branch and publish it :)

So no promises on a date, but I'll do my best to release something soon.

ludwik commented 10 years ago

Awesome! Looking forward to it.

On this line: https://github.com/brunoV/throttler/blob/master/src/throttler/core.clj#L154 ...is it just a matter of controlling the 1 in the (chan 1). I'm still new to the async thing.

Regards.

Ludwik.

ludwik commented 9 years ago

Hi.

Just came back to this 6 months on... I'm doing something like the below, composes well with the throttler.

(defn fn-concurrency-throttler [n-way]
  (let [c (async/chan n-way)]
    (fn [f]
      (fn [& args]
        ;; The approach is simple: pipe a bogus message through a
        ;; concurrency-throttled channel. Evaluate the function, and when it completes, take the msg off.
        (async/>!! c :eval-request)
        (try
           (let [answer (apply f args)]
              (async/<!! c)
               answer)
          (catch Exception e (do (async/<!! c) (throw e))))))))  ; in case anything breaks, still take the msg off the channel and rethrow

(defn concurrency-throttler-fn [f n-way]
  ((fn-concurrency-throttler n-way) f))