Skip to content

Instantly share code, notes, and snippets.

@ottbot
Last active August 29, 2015 14:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ottbot/f2ffc9b8abbb64dac1dd to your computer and use it in GitHub Desktop.
Save ottbot/f2ffc9b8abbb64dac1dd to your computer and use it in GitHub Desktop.
throttling with manifold
(ns throttle-example
(:require [manifold.stream :as s]))
;; should limit to 1 per sec ?
(def a (s/throttle 1 (s/stream)))
(dotimes [n 100]
(s/put! a n))
(defn now []
(new java.util.Date))
(dotimes [n 10]
(println (str (now) " ~ " @(s/take! a))))
;; Wed Jun 17 22:34:52 PDT 2015 ~ 0
;; Wed Jun 17 22:34:52 PDT 2015 ~ 1
;; Wed Jun 17 22:34:52 PDT 2015 ~ 2
;; Wed Jun 17 22:34:52 PDT 2015 ~ 3
;; Wed Jun 17 22:34:52 PDT 2015 ~ 4
;; Wed Jun 17 22:34:52 PDT 2015 ~ 5
;; Wed Jun 17 22:34:52 PDT 2015 ~ 6
;; Wed Jun 17 22:34:52 PDT 2015 ~ 7
;; Wed Jun 17 22:34:52 PDT 2015 ~ 8
;; Wed Jun 17 22:34:52 PDT 2015 ~ 9
;; just prints out real fast like.
@ztellman
Copy link

You need to put! into the stream you've throttled, not the stream which was returned by throttle. In most of the operators I make it impossible to put! into a derivative stream like this, it was an oversight on my part not to do the same here. Sorry for the confusion.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment