This proposal aims to replace the current functions to start and stop reading data from a stream in a push-fashion with a uv_read function which would work in a pull-fashion. Function prototype:
typedef void (*uv_read_cb)(uv_read_t* req, int status);
UV_EXTERN int uv_read(uv_read_t* req,
uv_stream_t* handle,
const uv_buf_t bufs[],
unsigned int nbufs,
uv_read_cb cb);
Basically this would work as an async readv
. Once the read operation is completed the
callback would be fired and the status parameter would contain the number of bytes read,
or an error. Support for cancellation of this type of requests is also planned.
- Consistency with write operations
- A push style API can be emulated on top of this pull style API by having a request active all the time and rearming it after a read is performed, but not the other way around. It's doable, but more cumbersome.
- It could poptentially simplify how memory is managed for reading, as it's attached to each request and the user is responsible for allocating any buffer size, which needs to be valid until the request callback is called.
- Wisdom of hindsight by @bnoordhuis: joyent/libuv#1246 (comment)
Funny enough, Windows already works like this internally.
It is the "iocp model". However in case of tcp we do some trickery to avoid it. Otherwise you need to pre-allocate x kb (typically 64 kb) per socket; so when there are many open-but-mostly-idle connections such as websockets, this uses up a lot of memory for no gain.