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

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.

主站蜘蛛池模板: 郯城县| 安泽县| 昌乐县| 金乡县| 南部县| 静宁县| 明星| 东台市| 上栗县| 永登县| 肇东市| 凌源市| 延长县| 蒲城县| 东乡| 虞城县| 新邵县| 巍山| 四川省| 抚松县| 靖远县| 兰考县| 金堂县| 大埔区| 高密市| 饶河县| 大同市| 镇安县| 克什克腾旗| 来凤县| 土默特右旗| 恭城| 陇南市| 兴安县| 保定市| 阜康市| 海阳市| 长沙县| 平安县| 新乡市| 泾阳县|