官术网_书友最值得收藏!

Sliding buffer

A drawback of dropping buffers is that we might not be processing the latest items at a given time. For the times where processing the latest information is a must, we can use a sliding buffer:

    (def result (chan (async/sliding-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: " 3
    ;; "Got value: " 4  

As before, we only get two values, but they are the latest ones that have been produced by the go loop.

When the limit of the sliding buffer is overrun, core.async drops the oldest items to make room for the newest ones. I end up using this buffering strategy most of the time.

主站蜘蛛池模板: 北碚区| 原平市| 惠水县| 莲花县| 天柱县| 玛曲县| 五台县| 怀远县| 商水县| 长汀县| 沙洋县| 丰都县| 夏津县| 宁都县| 砀山县| 苏尼特右旗| 清水县| 定陶县| 辉南县| 陇西县| 五原县| 宁化县| 得荣县| 泗水县| 蒲江县| 扎囊县| 凉城县| 香格里拉县| 莲花县| 松原市| 青川县| 惠水县| 扬中市| 和林格尔县| 无棣县| 涟水县| 鄂伦春自治旗| 江城| 新平| 肥东县| 达孜县|