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

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.

主站蜘蛛池模板: 神池县| 武义县| 白城市| 阿拉尔市| 沾化县| 和政县| 萨迦县| 德庆县| 许昌县| 寿阳县| 库尔勒市| 岐山县| 云浮市| 襄城县| 张掖市| 龙陵县| 沿河| 南召县| 扬中市| 锦屏县| 和龙市| 嘉黎县| 桐庐县| 宁远县| 临漳县| 襄城县| 个旧市| 巩留县| 高邑县| 普陀区| 肥东县| 班戈县| 措勤县| 康乐县| 布拖县| 苍溪县| 茶陵县| 星子县| 吉安市| 长葛市| 海伦市|