writable.cork()


writable.cork() 方法会强制将所有写入的数据缓存在内存中。缓冲的数据将在调用 stream.uncork()stream.end() 方法时被刷新。

【The writable.cork() method forces all written data to be buffered in memory. The buffered data will be flushed when either the stream.uncork() or stream.end() methods are called.】

writable.cork() 的主要目的是应对在流中快速连续写入多个小数据块的情况。writable.cork() 不会立即将这些数据块转发到底层目标,而是会将所有数据块缓冲起来,直到调用 writable.uncork() 为止,此时如果存在 writable._writev(),则会将所有数据块一次性传递给它。这样可以避免出现头部阻塞的情况,即在等待第一个小数据块处理时其他数据被缓冲。然而,如果在未实现 writable._writev() 的情况下使用 writable.cork() 可能会对吞吐量产生不利影响。

【The primary intent of writable.cork() is to accommodate a situation in which several small chunks are written to the stream in rapid succession. Instead of immediately forwarding them to the underlying destination, writable.cork() buffers all the chunks until writable.uncork() is called, which will pass them all to writable._writev(), if present. This prevents a head-of-line blocking situation where data is being buffered while waiting for the first small chunk to be processed. However, use of writable.cork() without implementing writable._writev() may have an adverse effect on throughput.】

另请参阅:writable.uncork()writable._writev()

【See also: writable.uncork(), writable._writev().】