Skip to content

Commit

Permalink
chore(nodejs): failproof flush (#8)
Browse files Browse the repository at this point in the history
* chore(nodejs): failproof flush

* chore(nodejs): failproof flush
  • Loading branch information
glasstiger authored Sep 21, 2022
1 parent 456e253 commit c71c8f9
Show file tree
Hide file tree
Showing 11 changed files with 174 additions and 63 deletions.
33 changes: 18 additions & 15 deletions docs/Sender.html
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,9 @@ <h5>Parameters:</h5>
<ul>
<li>bufferSize: <i>number</i> - Size of the buffer used by the sender to collect rows, provided in bytes. <br>
Optional, defaults to 8192 bytes </li>
<li>copyBuffer: <i>boolean</i> - If true a new buffer will be created for every flush() call and the data to be sent to the server will be copied into this new buffer. <br>
Setting the flag could result in performance degradation, use this flag only if calls to the client cannot be serialised. <br>
Optional, defaults to false </li>
<li>jwk: <i>{x: string, y: string, kid: string, kty: string, d: string, crv: string}</i> - JsonWebKey for authentication. <br>
If not provided, client is not authenticated and server might reject the connection depending on configuration.</li>
</ul>
Expand Down Expand Up @@ -322,7 +325,7 @@ <h5>Parameters:</h5>

<dt class="tag-source">Source:</dt>
<dd class="tag-source"><ul class="dummy"><li>
<a href="src_sender.js.html">src/sender.js</a>, <a href="src_sender.js.html#line328">line 328</a>
<a href="src_sender.js.html">src/sender.js</a>, <a href="src_sender.js.html#line351">line 351</a>
</li></ul></dd>


Expand Down Expand Up @@ -411,7 +414,7 @@ <h4 class="name" id="atNow"><span class="type-signature"></span>atNow<span class

<dt class="tag-source">Source:</dt>
<dd class="tag-source"><ul class="dummy"><li>
<a href="src_sender.js.html">src/sender.js</a>, <a href="src_sender.js.html#line348">line 348</a>
<a href="src_sender.js.html">src/sender.js</a>, <a href="src_sender.js.html#line370">line 370</a>
</li></ul></dd>


Expand Down Expand Up @@ -571,7 +574,7 @@ <h5>Parameters:</h5>

<dt class="tag-source">Source:</dt>
<dd class="tag-source"><ul class="dummy"><li>
<a href="src_sender.js.html">src/sender.js</a>, <a href="src_sender.js.html#line259">line 259</a>
<a href="src_sender.js.html">src/sender.js</a>, <a href="src_sender.js.html#line282">line 282</a>
</li></ul></dd>


Expand Down Expand Up @@ -682,7 +685,7 @@ <h4 class="name" id="close"><span class="type-signature">(async) </span>close<sp

<dt class="tag-source">Source:</dt>
<dd class="tag-source"><ul class="dummy"><li>
<a href="src_sender.js.html">src/sender.js</a>, <a href="src_sender.js.html#line147">line 147</a>
<a href="src_sender.js.html">src/sender.js</a>, <a href="src_sender.js.html#line159">line 159</a>
</li></ul></dd>


Expand Down Expand Up @@ -877,7 +880,7 @@ <h5>Parameters:</h5>

<dt class="tag-source">Source:</dt>
<dd class="tag-source"><ul class="dummy"><li>
<a href="src_sender.js.html">src/sender.js</a>, <a href="src_sender.js.html#line92">line 92</a>
<a href="src_sender.js.html">src/sender.js</a>, <a href="src_sender.js.html#line104">line 104</a>
</li></ul></dd>


Expand Down Expand Up @@ -1037,7 +1040,7 @@ <h5>Parameters:</h5>

<dt class="tag-source">Source:</dt>
<dd class="tag-source"><ul class="dummy"><li>
<a href="src_sender.js.html">src/sender.js</a>, <a href="src_sender.js.html#line274">line 274</a>
<a href="src_sender.js.html">src/sender.js</a>, <a href="src_sender.js.html#line297">line 297</a>
</li></ul></dd>


Expand Down Expand Up @@ -1148,7 +1151,7 @@ <h4 class="name" id="flush"><span class="type-signature">(async) </span>flush<sp

<dt class="tag-source">Source:</dt>
<dd class="tag-source"><ul class="dummy"><li>
<a href="src_sender.js.html">src/sender.js</a>, <a href="src_sender.js.html#line163">line 163</a>
<a href="src_sender.js.html">src/sender.js</a>, <a href="src_sender.js.html#line175">line 175</a>
</li></ul></dd>


Expand Down Expand Up @@ -1330,7 +1333,7 @@ <h5>Parameters:</h5>

<dt class="tag-source">Source:</dt>
<dd class="tag-source"><ul class="dummy"><li>
<a href="src_sender.js.html">src/sender.js</a>, <a href="src_sender.js.html#line290">line 290</a>
<a href="src_sender.js.html">src/sender.js</a>, <a href="src_sender.js.html#line313">line 313</a>
</li></ul></dd>


Expand Down Expand Up @@ -1441,7 +1444,7 @@ <h4 class="name" id="reset"><span class="type-signature"></span>reset<span class

<dt class="tag-source">Source:</dt>
<dd class="tag-source"><ul class="dummy"><li>
<a href="src_sender.js.html">src/sender.js</a>, <a href="src_sender.js.html#line80">line 80</a>
<a href="src_sender.js.html">src/sender.js</a>, <a href="src_sender.js.html#line92">line 92</a>
</li></ul></dd>


Expand Down Expand Up @@ -1602,7 +1605,7 @@ <h5>Parameters:</h5>

<dt class="tag-source">Source:</dt>
<dd class="tag-source"><ul class="dummy"><li>
<a href="src_sender.js.html">src/sender.js</a>, <a href="src_sender.js.html#line65">line 65</a>
<a href="src_sender.js.html">src/sender.js</a>, <a href="src_sender.js.html#line77">line 77</a>
</li></ul></dd>


Expand Down Expand Up @@ -1762,7 +1765,7 @@ <h5>Parameters:</h5>

<dt class="tag-source">Source:</dt>
<dd class="tag-source"><ul class="dummy"><li>
<a href="src_sender.js.html">src/sender.js</a>, <a href="src_sender.js.html#line242">line 242</a>
<a href="src_sender.js.html">src/sender.js</a>, <a href="src_sender.js.html#line265">line 265</a>
</li></ul></dd>


Expand Down Expand Up @@ -1944,7 +1947,7 @@ <h5>Parameters:</h5>

<dt class="tag-source">Source:</dt>
<dd class="tag-source"><ul class="dummy"><li>
<a href="src_sender.js.html">src/sender.js</a>, <a href="src_sender.js.html#line217">line 217</a>
<a href="src_sender.js.html">src/sender.js</a>, <a href="src_sender.js.html#line240">line 240</a>
</li></ul></dd>


Expand Down Expand Up @@ -2103,7 +2106,7 @@ <h5>Parameters:</h5>

<dt class="tag-source">Source:</dt>
<dd class="tag-source"><ul class="dummy"><li>
<a href="src_sender.js.html">src/sender.js</a>, <a href="src_sender.js.html#line196">line 196</a>
<a href="src_sender.js.html">src/sender.js</a>, <a href="src_sender.js.html#line219">line 219</a>
</li></ul></dd>


Expand Down Expand Up @@ -2285,7 +2288,7 @@ <h5>Parameters:</h5>

<dt class="tag-source">Source:</dt>
<dd class="tag-source"><ul class="dummy"><li>
<a href="src_sender.js.html">src/sender.js</a>, <a href="src_sender.js.html#line310">line 310</a>
<a href="src_sender.js.html">src/sender.js</a>, <a href="src_sender.js.html#line333">line 333</a>
</li></ul></dd>


Expand Down Expand Up @@ -2359,7 +2362,7 @@ <h2><a href="index.html">Home</a></h2><h3>Modules</h3><ul><li><a href="module-@q
<br class="clear">

<footer>
Documentation generated by <a href="https://github.com/jsdoc/jsdoc">JSDoc 3.6.11</a> on Tue Aug 16 2022 14:23:38 GMT+0100 (British Summer Time)
Documentation generated by <a href="https://github.com/jsdoc/jsdoc">JSDoc 3.6.11</a> on Wed Sep 21 2022 10:00:52 GMT+0100 (British Summer Time)
</footer>

<script> prettyPrint(); </script>
Expand Down
2 changes: 1 addition & 1 deletion docs/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ <h2><a href="index.html">Home</a></h2><h3>Modules</h3><ul><li><a href="module-@q
<br class="clear">

<footer>
Documentation generated by <a href="https://github.com/jsdoc/jsdoc">JSDoc 3.6.11</a> on Tue Aug 16 2022 14:23:38 GMT+0100 (British Summer Time)
Documentation generated by <a href="https://github.com/jsdoc/jsdoc">JSDoc 3.6.11</a> on Wed Sep 21 2022 10:00:52 GMT+0100 (British Summer Time)
</footer>

<script> prettyPrint(); </script>
Expand Down
2 changes: 1 addition & 1 deletion docs/index.js.html
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ <h2><a href="index.html">Home</a></h2><h3>Modules</h3><ul><li><a href="module-@q
<br class="clear">

<footer>
Documentation generated by <a href="https://github.com/jsdoc/jsdoc">JSDoc 3.6.11</a> on Tue Aug 16 2022 14:23:38 GMT+0100 (British Summer Time)
Documentation generated by <a href="https://github.com/jsdoc/jsdoc">JSDoc 3.6.11</a> on Wed Sep 21 2022 10:00:52 GMT+0100 (British Summer Time)
</footer>

<script> prettyPrint(); </script>
Expand Down
2 changes: 1 addition & 1 deletion docs/module-@questdb_nodejs-client.html
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ <h2><a href="index.html">Home</a></h2><h3>Modules</h3><ul><li><a href="module-@q
<br class="clear">

<footer>
Documentation generated by <a href="https://github.com/jsdoc/jsdoc">JSDoc 3.6.11</a> on Tue Aug 16 2022 14:23:38 GMT+0100 (British Summer Time)
Documentation generated by <a href="https://github.com/jsdoc/jsdoc">JSDoc 3.6.11</a> on Wed Sep 21 2022 10:00:52 GMT+0100 (British Summer Time)
</footer>

<script> prettyPrint(); </script>
Expand Down
44 changes: 33 additions & 11 deletions docs/src_sender.js.html
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ <h1 class="page-title">Source: src/sender.js</h1>
/** @private */ socket;
/** @private */ bufferSize;
/** @private */ buffer;
/** @private */ toBuffer;
/** @private */ doResolve;
/** @private */ position;
/** @private */ endOfLastRow;
/** @private */ hasTable;
Expand All @@ -70,6 +72,9 @@ <h1 class="page-title">Source: src/sender.js</h1>
* &lt;ul>
* &lt;li>bufferSize: &lt;i>number&lt;/i> - Size of the buffer used by the sender to collect rows, provided in bytes. &lt;br>
* Optional, defaults to 8192 bytes &lt;/li>
* &lt;li>copyBuffer: &lt;i>boolean&lt;/i> - If true a new buffer will be created for every flush() call and the data to be sent to the server will be copied into this new buffer. &lt;br>
* Setting the flag could result in performance degradation, use this flag only if calls to the client cannot be serialised. &lt;br>
* Optional, defaults to false &lt;/li>
* &lt;li>jwk: &lt;i>{x: string, y: string, kid: string, kty: string, d: string, crv: string}&lt;/i> - JsonWebKey for authentication. &lt;br>
* If not provided, client is not authenticated and server might reject the connection depending on configuration.&lt;/li>
* &lt;/ul>
Expand All @@ -79,6 +84,13 @@ <h1 class="page-title">Source: src/sender.js</h1>
if (options) {
this.jwk = options.jwk;
}
this.toBuffer = options &amp;&amp; options.copyBuffer ? this.toBufferNew : this.toBufferView;
this.doResolve = options &amp;&amp; options.copyBuffer
? resolve => resolve(true)
: resolve => {
compact(this);
resolve(true);
}
this.resize(options &amp;&amp; options.bufferSize ? options.bufferSize : DEFAULT_BUFFER_SIZE);
this.reset();
}
Expand Down Expand Up @@ -195,26 +207,37 @@ <h1 class="page-title">Source: src/sender.js</h1>
}
return new Promise((resolve, reject) => {
this.socket.write(data, err => {
if (err) {
reject(err);
} else {
compact(this);
resolve(true);
}
err ? reject(err) : this.doResolve(resolve);
});
});
}

/**
* @ignore
* @return {Buffer} Returns a cropped buffer ready to send to the server or null if there is nothing to send.
* The returned buffer is backed by the sender's buffer.
*/
toBuffer(pos = this.position) {
toBufferView(pos = this.position) {
return pos > 0
? this.buffer.subarray(0, pos)
: null;
}

/**
* @ignore
* @return {Buffer} Returns a cropped buffer ready to send to the server or null if there is nothing to send.
* The returned buffer is a copy of the sender's buffer.
*/
toBufferNew(pos = this.position) {
if (pos > 0) {
const data = Buffer.allocUnsafe(pos);
this.buffer.copy(data, 0, 0, pos);
compact(this);
return data;
}
return null;
}

/**
* Write the table name into the buffer of the sender.
*
Expand Down Expand Up @@ -361,10 +384,9 @@ <h1 class="page-title">Source: src/sender.js</h1>
throw new Error(`The designated timestamp must be of type string, received ${typeof timestamp}`);
}
validateDesignatedTimestamp(timestamp);
const timestampStr = timestamp.toString();
checkCapacity(this, [], 2 + timestampStr.length);
checkCapacity(this, [], 2 + timestamp.length);
write(this, ' ');
write(this, timestampStr);
write(this, timestamp);
write(this, '\n');
startNewRow(this);
}
Expand Down Expand Up @@ -513,7 +535,7 @@ <h2><a href="index.html">Home</a></h2><h3>Modules</h3><ul><li><a href="module-@q
<br class="clear">

<footer>
Documentation generated by <a href="https://github.com/jsdoc/jsdoc">JSDoc 3.6.11</a> on Tue Aug 16 2022 14:23:38 GMT+0100 (British Summer Time)
Documentation generated by <a href="https://github.com/jsdoc/jsdoc">JSDoc 3.6.11</a> on Wed Sep 21 2022 10:00:52 GMT+0100 (British Summer Time)
</footer>

<script> prettyPrint(); </script>
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@questdb/nodejs-client",
"version": "1.0.0",
"version": "1.0.1",
"description": "QuestDB Node.js Client",
"main": "index.js",
"types": "types/index.d.ts",
Expand Down
37 changes: 30 additions & 7 deletions src/sender.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ class Sender {
/** @private */ socket;
/** @private */ bufferSize;
/** @private */ buffer;
/** @private */ toBuffer;
/** @private */ doResolve;
/** @private */ position;
/** @private */ endOfLastRow;
/** @private */ hasTable;
Expand All @@ -42,6 +44,9 @@ class Sender {
* <ul>
* <li>bufferSize: <i>number</i> - Size of the buffer used by the sender to collect rows, provided in bytes. <br>
* Optional, defaults to 8192 bytes </li>
* <li>copyBuffer: <i>boolean</i> - If true a new buffer will be created for every flush() call and the data to be sent to the server will be copied into this new buffer. <br>
* Setting the flag could result in performance degradation, use this flag only if calls to the client cannot be serialised. <br>
* Optional, defaults to false </li>
* <li>jwk: <i>{x: string, y: string, kid: string, kty: string, d: string, crv: string}</i> - JsonWebKey for authentication. <br>
* If not provided, client is not authenticated and server might reject the connection depending on configuration.</li>
* </ul>
Expand All @@ -51,6 +56,13 @@ class Sender {
if (options) {
this.jwk = options.jwk;
}
this.toBuffer = options && options.copyBuffer ? this.toBufferNew : this.toBufferView;
this.doResolve = options && options.copyBuffer
? resolve => resolve(true)
: resolve => {
compact(this);
resolve(true);
}
this.resize(options && options.bufferSize ? options.bufferSize : DEFAULT_BUFFER_SIZE);
this.reset();
}
Expand Down Expand Up @@ -167,26 +179,37 @@ class Sender {
}
return new Promise((resolve, reject) => {
this.socket.write(data, err => {
if (err) {
reject(err);
} else {
compact(this);
resolve(true);
}
err ? reject(err) : this.doResolve(resolve);
});
});
}

/**
* @ignore
* @return {Buffer} Returns a cropped buffer ready to send to the server or null if there is nothing to send.
* The returned buffer is backed by the sender's buffer.
*/
toBuffer(pos = this.position) {
toBufferView(pos = this.position) {
return pos > 0
? this.buffer.subarray(0, pos)
: null;
}

/**
* @ignore
* @return {Buffer} Returns a cropped buffer ready to send to the server or null if there is nothing to send.
* The returned buffer is a copy of the sender's buffer.
*/
toBufferNew(pos = this.position) {
if (pos > 0) {
const data = Buffer.allocUnsafe(pos);
this.buffer.copy(data, 0, 0, pos);
compact(this);
return data;
}
return null;
}

/**
* Write the table name into the buffer of the sender.
*
Expand Down
Loading

0 comments on commit c71c8f9

Please sign in to comment.