【问题标题】:Downsides for not implementing _read in stream.Readable未在 stream.Readable 中实现 _read 的缺点
【发布时间】:2014-08-21 12:15:53
【问题描述】:

我有异步数据源我想给stream.Readable 接口。然而,这个数据源没有“读取”的概念——当我有数据时,我只收到一个事件,不维护缓冲区。

那么,使用下面的实现有什么缺点吗?

stream = require 'stream'

class Wrapper extends stream.Readable
    constructor: (@resource) ->
        # call stream.Readable constructor
        super

        # when message from resource arrives
        @resource.on 'message', (message) =>
            # just push it into read queue
            @push message

        # when resource is closed
        @resource.on 'close', =>
            # indicate there will be nothing more to read
            @push null

    _read: (size) ->
        # no-op

例如如果_read 方法为空,并且数据最终会按消息推送到读取队列中,我会破坏什么吗?

【问题讨论】:

    标签: javascript node.js stream coffeescript


    【解决方案1】:

    _read() 方法用于通知您流可以处理更多数据(例如,内部缓冲区尚未达到 highWaterMark)。你当然可以忽略它。

    但是,您至少应该适当地处理背压:如果 push() 返回 false,您应该停止推送,直到调用 _read() 或发出“drain”。

    【讨论】:

    • 如果我不能停止推送怎么办——如果我停止监听message 事件一段时间,在那个时间间隔内到达的消息将会丢失,如果我自己缓冲它们然后是push,这与使用流核心缓冲它有什么不同?
    猜你喜欢
    • 2018-08-25
    • 2014-01-09
    • 2014-10-30
    • 1970-01-01
    • 2016-01-15
    • 2013-10-12
    • 1970-01-01
    • 1970-01-01
    • 2016-11-07
    相关资源
    最近更新 更多