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

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.

主站蜘蛛池模板: 化隆| 厦门市| 桃园市| 临潭县| 延庆县| 永仁县| 馆陶县| 湘潭县| 大化| 南川市| 会宁县| 竹北市| 房山区| 壶关县| 新疆| 晋江市| 荥阳市| 淄博市| 砀山县| 闵行区| 翁牛特旗| 密云县| 红原县| 潍坊市| 务川| 安陆市| 普陀区| 龙州县| 盐山县| 德格县| 灵石县| 织金县| 阿拉善左旗| 阳曲县| 冀州市| 通许县| 福泉市| 泰兴市| 石渠县| 乐东| 辛集市|