Hands-On Reactive Programming with Clojure
上QQ阅读APP看书,第一时间看更新

Dropping buffer

A dropping buffer also has a fixed size. However, instead of blocking producers when it is full, it simply ignores any new items, as shown here:

    (def result (chan (async/dropping-buffer 2)))
    (go-loop []
      (<! (async/timeout 1000))
      (when-let [x (<! result)]
        (prn "Got value: " x)
        (recur)))
    
    (go  (doseq [n (range 5)]
           (>! result n))
         (prn "Done putting values!")
         (async/close! result))
    
    ;; "Done putting values!"
    ;; "Got value: " 0
    ;; "Got value: " 1  

As before, we still have a buffer of size two, but this time, the producer ends quickly without ever getting blocked. The dropping-buffer simply ignored all items over its limit.