Repository navigation
Description
Context
Since the creation of tedious, all different request types (e.g. RPC Requests, SQL Batches, Transaction Manager Requests) were send to the remote server by writing all parts of the message to a WritableTrackingBuffer instance, and then writing the underlying Buffer to the network socket.
This was pretty simple from an implementation point of view - there was no async processing involved, and the WritableTrackingBuffer could just be passed around to different method that would write their data into it.
But this simplicity comes with a few trade offs:
-
Unbounded memory usage:
WritableTrackingBuffers start with a pre-defined size, and once more data written to theWritableTrackingBufferhits that size limit, it will be dynamically (and exponentially) grown to fit more data. This makes it easy to use, but leads to essentially unbounded memory growth when a lot of data is written to it. This can easily happen if TVPs with many columns and/rows are written, or if parameters contain large values (e.g.Buffervalues tovarbinary(max)parameters,stringvalues tonvarchar(max)values, ...). -
Request latency: The whole request needs to be build, before the first byte can hit the network. Again, this is especially noticable with requests that have a TVP parameter with many rows and/or columns. All values of these rows and columns need to be completely processed, before the request can be sent, while in theory it would be possible to start sending request data while request data is already streamed to the remote server. This can reduce request times for such requests considerably.
-
Blocking of the Node.js event loop: Blocking the Node.js event loop is extremely problematic for many use-cases that
tediouswants to support. As the request payload generation was happening without any async scheduling, no other processing could happen in the meantime, causing delays in other parts of the application. A simple and common use case is usingtediousin a Node.js web application, but when sending a large request was sent viatediousin one web request, the web server would be blocked and other in-flight web requests would have to wait for the payload generation to be finished. This was partially tackled by fix: make writing large TVP non-blocking operation #845 adding callbacks to most functions that wrote to theWritableTrackingBufferand wrapping writing of parameter data to theWritableTrackingBufferintosetImmediatecalls to allow other asynchronous operations to be scheduled.
We want to fix these issues, and make tedious better suited for large requests with large parameter data.
Potential Approaches
There's two approaches that could be taken to improve this situation:
Fully asynchronous payload generation via callbacks
This would build on top of the approach already taken in #845. All methods that currently write to the WritableTrackingBuffer could be changed to instead accept a WritableStream, and could then build small buffers, write them to the WritableStream via write method. The request latency issue would be solved, as soon as parts of the payload are available they would be streamed out. Unbounded memory usage would also be fixed, as there would be no longer a growing WritableTrackingBuffer involved.
Unfortunately, this approach also has a few downsides: Code would become harder to understand due to the general code readability issues of heavy callback usage. We would also have to check on every call to .write whether we need to wait for a drain event before writing more data, and delay any following writes accordingly, making the code even more complex.
Payload generation via generators
Another approach would be to move payload generation to a generator based approach. Instead of writing to a WritableTrackingBuffer, each part of the payload generation is converted to a generator function that yields Buffers containing individual parts of the payload.
The output of such a generator function can easily be used as the data source of a ReadableStream, which in turn can be piped into the message stream, allowing the code to be free from any explicit handling of asynchronous processing or target stream draining concerns.
Generators have been available since Node.js 4.x.
Selected Approach
Based on the work done in #845 and trying out the alternative generator based approach, we'll be pursue the generator approach.
Project Outline
As a first step, we plan on rewriting parameter generation to be generator based.
Currently, parameter data is written to a WritableTrackingBuffer via a writeParameterData method that exist on every supported data type.
Here's the implementation from the BigInt datatype:
tedious/src/data-types/bigint.ts
Lines 18 to 27 in 62ff4a1
| writeParameterData: function(buffer, parameter, _options, cb) { | |
| if (parameter.value != null) { | |
| buffer.writeUInt8(8); | |
| buffer.writeInt64LE(Number(parameter.value)); | |
| } else { | |
| buffer.writeUInt8(0); | |
| } | |
| cb(); | |
| }, |
We'll start by adding new methods to all data types that generate the same data as the existing writeParameterData, but using one or multiple new WritableTrackingBuffer instance, and yielding the generated data, like this:
generateParameterData: function *(parameter, _options) {
if (parameter.value != null) {
const buffer = new WritableTrackingBuffer(8);
buffer.writeUInt8(8);
yield buffer.writeInt64LE(Number(parameter.value));
} else {
const buffer = new WritableTrackingBuffer(1);
yield buffer.writeUInt8(0);
}
},While the writeParameterData function will be update to build on top of the generator returned by generateParameterData:
writeParameterData: function(buffer, parameter, options, cb) {
buffer.writeBuffer(Buffer.concat(Array.from(this.generateParameterData(parameter, options))));
cb();
},
This allows us to move the parameter data generation logic to be based on generators, while still exposing the same interface writeParameterData to the rest of the code, allow us to keep our initial changes local to the data type code.
These changes won't fix the issues outlined above, but they'll put us in the position to easily fix those issues with additional work building on top of these changes.
Activity
IanChokS commentedon Jan 17, 2020
WIP #1037
refactor: generate request payloads using generator functions
IanChokS commentedon Mar 11, 2020
These improvements have been merged #1066