-
Notifications
You must be signed in to change notification settings - Fork 161
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merge ReadableByteStream into ReadableStream #430
Conversation
Spec text update WIP. It shouldn't render by bikeshed yet. |
2bb48d2
to
0aa9a98
Compare
Yay, this is really exciting! I added some commits of my own to this branch. I think it'll be better for us to work together than to just do review-ping-pong every day. Here are some additional potential changes I'd like your opinion on:
|
@@ -3,21 +3,34 @@ const test = require('tape-catch'); | |||
// Many other pipeTo-with-options tests have been templated. | |||
|
|||
test('Piping with no options and a destination error', t => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why did these tests change? (We'll want to summarize any test changes in the commit message since they reflect potential observable behavior changes.)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think I see. It's all because of #424.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes. I've added a line explaining why this change happened in the OP of the PR
I added some examples to the appendix. Please review them for correctness. Especially the socket one, as I am not sure that |
|
Ah, it is supposed to be the argument to respond(). So it doesn't need to be assigned in the second branch |
In the second branch, you need to set the buffer's size to |
Regarding backpressure: Is it possible to not read when desiredSize <= 0? I'm not sure how precisely you need to manage the total buffer size, but when you stops reading from the socket the system will stop reading from the network and setTCPWindowSize(0) is not needed, IIUC. |
The problem is that the kernel queue (aka kernel buffer) could overflow. The point of the enqueue() system is to move things into a userspace queue to avoid that. |
So you want to minimize the kernel buffer usage when the consumer doesn't want the data, right? |
Yeah, that's the idea. |
I'm sorry I don't have enough knowledge about how Chrome uses a TCP socket. TCPSocketPosix::SetReceiveBufferSize doesn't look to be used for such purpose, but I may be wrong. You will find far better reviewers in net-dev@chromium.org. |
OK!
Fixed
Sounds good. Thanks
Ah, I see. OK. I'll make it an abstract operation.
Fixed |
In Linux (I'm reading 3.13), the size of window to advertise is determined from sk_rcvbuf (using tcp_adv_win_scale). By default, 3/4 of free space of sk_rcvbuf is advertised. The free space is calculated by deducting sk_rmem_alloc from sk_rcvbuf. sk_rmem_alloc represents the number of bytes received (+overhead) but not yet consumed by reading out to the user land. It increases when skb_set_owner_r() is called on delivery of data to TCP layer. skb_set_owner_r() also sets sock_rfree() to the sk_buff's destructor which decreases sk_rmem_alloc. tcp_read_sock() invokes this sock_rfree() by calling sk_eat_skb() on finished sk_buff. tcp_read_sock() is invoked on tcp_recvmsg(). When sending data or ack only packet, __tcp_select_window() is called to calculate the up-to-date window size to advertise in the packet. The calculation explained above happens in this function. TCP_WINDOW_CLAMP socket option bounds window. But it seems only when window scaling is not turned on. See tcp_sock's window_clamp. SO_RCVBUF socket option sets sk_rcvbuf. See SO_RCVBUF handling code in net/core/sock.c. These values cannot be less than SOCK_MIN_RCVBUF / 2. But it should be ok. It's small, and while read(2) doesn't happen the buffer gets filled to make sk_rmem_alloc hit sk_rcvbuf and let __tcp_select_window() decrease the advertised window. When tcp_moderate_rcvbuf sysctl option is enabled, tcp_rcv_space_adjust() may automatically increase the space every time when read(2) consumes data from the kernel buffer. This is done based on the amount copied to user space in the last RTT and some strategy (considering memory pressure, etc.). Setting SO_RCVBUF disables this automatic increase (see that SOCK_RCVBUF_LOCK is set to sk->userlocks) and reset the buffer size limit (sk_rcvbuf) to the given value. So, IIUC, TCPSocketPosix::SetReceiveBufferSize() would help us prevent / recover from unexpected increase of amount of data being buffered in kernel. |
Self-reply to #430 (comment)
SO_RCVBUF limits total memory consumption including overhead. So, this value should be set to a slightly larger value (e.g. like tcp_adv_win_scale is reflected to the advertised window size). |
Done reviewing |
Just noticed: IsReadableStreamBYOBRequest is not defined, and a brand check is missing in the view getter. |
Sorry for the spam. I will fix the things I commented on except the examples. (That is: adding IsReadableStreamBYOBRequest, adding the missing brand check, moving the BYOB request abstract ops, and the syntax in the .js files.) |
Another issue: there is a mixture of spec internal types and JS types in the ReadableStreamBYOBRequest constructor. It cannot take a Record as its second parameter. I'd suggest passing the view directly and letting the caller construct it. |
The automatic buffer allocation feature discussed at #430 (comment) has been added. See the newly added test. Before making the change for the feature, I made some refactoring changes. The changes are separated into small commits, so should be easy to review. |
Fixed all by 89e2d31 |
Ah, OK. That makes sense! |
Updated the example to use the automatic buffer allocation feature. |
@@ -1729,6 +1739,11 @@ table: | |||
1. Set *this*@[[totalQueuedBytes]] to *0*. | |||
1. Set *this*@[[started]], and *this*@[[closeRequested]] to *false*. | |||
1. Set *this*@[[strategyHWM]] to ValidateAndNormalizeHighWaterMark(_highWaterMark_). | |||
1. Let _autoAllocateChunkSize_ be GetV(_underlyingByteSource_, `"autoAllocateChunkSize"`). | |||
1. If _autoAllocateChunkSize_ is not *undefined*, | |||
1. If Number.isInteger(_autoAllocateChunkSize_) is *true* or _autoAllocateChunkSize_ < 0, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This doesn't look right. (And we shouldn't reference public overridable API functions like Number.isInteger.)
I think instead what we want to do is:
- Set autoAllocateChunkSize to ToInteger(autoAllocateChunkSize).
- ReturnIfAbrupt(autoAllocateChunkSize).
- If autoAllocateChunkSize ≤ 0, or if autoAllocateChunkSize is +∞ or -∞, throw a RangeError exception.
(NaN is automatically converted to 0 so will still throw.)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Got it. Thanks for fixing!
Sorry for the delay in getting back to you on this. I made some more tweaks to auto-allocate based on my review comments. I think we should work on merging this soon. Do you think it's ready? Would you mind drafting up a commit message? The main things I want to be sure are included are:
After we get this merged I would like to do an editorial pass on my own through the document. Largely it will consist of adding more cross-links (both within-spec and to other specs) and also updating to use the latest ES conventions (which allow us to use ? and ! prefixes and get rid of ReturnIfAbrupt). |
Thanks for fixing. The commit basically looks good. See the replies to comments. |
Here's a draft of the commit log: Merge ReadableByteStream into ReadableStream Background We originally designed the ReadableByteStream which includes extended features to handle bytes as a separate class from the ReadableStream which handles a stream of general objects. While designing the details of ReadableByteStream (see #361), we noticed that we could simplify the ReadableStream and ReadableByteStream by moving variables and logic for handling queuing into their controller class (see #379). This turned out to be also clarifying which part of the logic is representing semantic requirement of readable streams and which part of that is implementing helper logic for easing development of underlying sources. After the above refactoring, we also noticed that ReadableStream and ReadableByteStream share most of their code. So, we attempted to merge the ReadableByteStream into the ReadableStream. Change summary The new ReadableStream has two reader getter methods when BYOB reading is available. Availability of BYOB reading is determined by whether or not the underlying source passed to the ReadableStream had BYOB pulling functionality. This is indicated by the "byob" parameter of the ReadableStream constructor. The two readers are named the default reader and BYOB reader. The default reader's read() method takes no argument. The resulting promise of the read() method will be fulfilled by a generated chunk. The BYOB reader's read() method takes one argument. It must be an ArrayBufferView. The ReadableStream will fill the passed ArrayBufferView and fulfills the returned promise with it. The ArrayBufferView might be transferred several times. When the byob parameter is false, the underlying source is given a ReadableStreamDefaultController on start() method call. This class provides methods to enqueue a new chunk and know the status of the queue. Its queuing strategy is configured by the parameters passed to the ReadableStream's constructor. The underlying source can subscribe to events on which chunks are drained from the queue by implementing the "pull" method on it. Only the getReader() method will be available on the ReadableStream. When the byob parameter is false, the underlying source is given a ReadableStreamBYOBController on start() method call. In addition to the functionalities of the ReadableStreamDefaultController, this controller provides a getter named byobRequest() which exposes the oldest outstanding BYOB reading request into which the underlying source can put bytes directly (see #423). Both the getReader() and the getBYOBReader() method will be available on the ReadableStream. The ReadableStreamBYOBController can be configured to convert read requests from a default reader into BYOB read request by allocating a buffer and exposing it at the byobRequest getter automatically. This ease implementation of a reactive underlying source. Changes included in this commit In addition to the major changes as described above, this commit includes bunch of design / logic / aesthetic changes as follows:
|
Feel free to edit, Domenic. |
4f471ed
to
ee12d7a
Compare
## Background We originally designed ReadableByteStream, which included extended features to handle bytes, as a separate class from the ReadableStream, which handles a stream of general objects. While designing the details of ReadableByteStream (see #361), we noticed that we could simplify ReadableStream and ReadableByteStream by moving variables and logic for handling queuing into their controller class (see #379). This turned out to also clarify which part of the logic represents semantic requirements of readable streams, and which part of it is implementing helper logic for easier development of underlying sources. After the above refactoring, we also noticed that ReadableStream and ReadableByteStream share most of their code. So, we merged the ReadableByteStream class into ReadableStream. This has many benefits for developers who don't have to deal with two similar-but-different classes. Instead, the same class is used, with the behavior customized by the underlying source. ## Change summary The new ReadableStream class has two reader acquisition methods, getReader() and getBYOBReader(), with the latter working when BYOB reading is available. Availability of BYOB reading is determined by whether or not the underlying source passed to the ReadableStream had BYOB pulling functionality. This is indicated by a truthy `byob` property of the underlying source. The two readers are named the default reader and BYOB reader. The default reader's read() method takes no argument. The resulting promise of the read() method will be fulfilled with a newly-allocated chunk. The BYOB reader's read() method takes one argument; it must be an ArrayBuffer view. The ReadableStream will fill the passed ArrayBuffer view, and fulfill the returned promise with it. The ArrayBuffer view might be transferred several times, but the same backing memory is always written to. When the byob option is falsy, the underlying source is given a ReadableStreamDefaultController. This class provides methods to enqueue a new chunk and know the status of the queue. Its queuing strategy is configured by the parameters passed to the ReadableStream's constructor. The underlying source can subscribe to know when chunks are drained from the queue by implementing the "pull" method. Only the getReader() method will be functional on the ReadableStream. When the byob option is truthy, the underlying source is given a ReadableStreamBYOBController. In addition to the functionalities of the ReadableStreamDefaultController, this controller provides a getter named `byobRequest` which exposes the oldest outstanding BYOB reading request into which the underlying source can put bytes directly (see #423). Both the getReader() and the getBYOBReader() method will be functional on the ReadableStream. The ReadableStreamBYOBController can be configured to convert read requests from a default reader into BYOB read requests, by automatically allocating a buffer and exposing it via the byobRequest getter. This eases implementation of a reactive underlying source, as shown in one of the new examples. ## Changes included in this commit In addition to the major changes as described above, this commit includes bunch of design /logic/aesthetic changes as follows: ### Changes to existing observable features Although ReadableStream's internals were refactored immensely, its external behavior (when not providing a BYOB source) is almost identical to before, as verified by our extensive unit tests. However, we did make a few changes which are observable: - Changes to the semantics of the controller methods (see #424): - Make controller.close() and controller.enqueue() fail when the stream is not in the readable state - Make controller.enqueue() throw a predefined TypeError, not the stored error - (As a result of these changes, the tests test/pipe-to-options.js, test/readable-streams/general.js, and test/readable-stream-templated.js have been updated.) - Rename ReadableStreamController to ReadableStreamDefaultController - Rename ReadableStreamReader to ReadableStreamDefaultReader ### Changes to byte streams As explained above, byte streams were changed in fairly extensive ways to merge them into the base ReadableStream class. Here we call out a few notable changes from the previous specification text: - Remove auto release feature from the ReadableByteStream - Rename Byob to BYOB - Make the default highWaterMark of the byte source version to 0 - Port the functionality that the start method can delay pulling by returning a pending promise to the ReadableStreamBYOBController - Port the highWaterMark mechanism to ReadableByteStreamController - Rename ReadableByteStreamController to ReadableStreamBYOBController - Correctly update the [[disturbed]] slot in the byte handling logic - read(view) now checks view.byteLength before setting [[disturbed]]
OK! I have rebased and squashed and updated the commit message with my tweaks. I'll let you do the honors of any final review, and of merging! |
Closed by e601d69 Thanks!! |
autoAllocateSize was added in whatwg#430 as commit e601d69. The spec was out of sync with the reference implementation: it tried to round non-integers into integers instead of rejecting them. The reference implementation was out of sync with the spec: it was allowing an autoAllocateChunkSize of 0 through instead of throwing a RangeError. The toInteger helper was added in 200b54b and its only user was removed in commit 5b47faa.
autoAllocateChunkSize was added in whatwg#430 as commit e601d69. The spec was out of sync with the reference implementation: it tried to round non-integers into integers instead of rejecting them. The reference implementation was out of sync with the spec: it was allowing an autoAllocateChunkSize of 0 through instead of throwing a RangeError. The toInteger helper was added in 200b54b and its only user was removed in commit 5b47faa.
autoAllocateChunkSize was added in whatwg#430 as commit e601d69. The spec was out of sync with the reference implementation: it tried to round non-integers into integers instead of rejecting them. The reference implementation was out of sync with the spec: it was allowing an autoAllocateChunkSize of 0 through instead of throwing a RangeError.
autoAllocateChunkSize was added in #430 as commit e601d69. The spec was out of sync with the reference implementation: it tried to round non-integers into integers instead of rejecting them. The reference implementation was out of sync with the spec: it was allowing an autoAllocateChunkSize of 0 through instead of throwing a RangeError.
This commit includes the following design changes:
[[disturbed]]
feature to the byte source version