Skip to content
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

refactor: move message handling internals to use async/await #1656

Open
wants to merge 22 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 16 commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
ff57dfb
Upgrade to `@types/[email protected]`
arthurschreiber Aug 18, 2024
1d56dbf
Simplify usage of `AbortSignal` by using built-in APIs.
arthurschreiber Aug 18, 2024
2ecdc11
refactor: rewrite parts of internal connection initialization to `asy…
arthurschreiber Aug 18, 2024
e761fa1
Add `MessageIO.readMessage` and `MessageIO.writeMessage`.
arthurschreiber Aug 27, 2024
b3b1239
Merge branch 'master' into arthur/async-await
arthurschreiber Aug 27, 2024
cbc800d
Handle `PRELOGIN` messages via new message reading/writing functions.
arthurschreiber Aug 27, 2024
c3d3d52
Move more things around.
arthurschreiber Aug 28, 2024
a19caa9
Fix error handling
arthurschreiber Aug 28, 2024
584dc96
Ensure we transition to `FINAL` state.
arthurschreiber Aug 28, 2024
64acb61
See if we can figure out what keeps mocha from exiting.
arthurschreiber Aug 29, 2024
e6ca6eb
Skip this test.
arthurschreiber Aug 29, 2024
7cf364b
See if this fixes the stuck connection issue.
arthurschreiber Aug 29, 2024
79a0b33
Recreate the abort controller.
arthurschreiber Aug 29, 2024
251886d
Add more debug output.
arthurschreiber Aug 31, 2024
ed2bb64
More logging.
arthurschreiber Aug 31, 2024
c88a5c4
More logs.
arthurschreiber Aug 31, 2024
153bc3f
log connection duration.
arthurschreiber Aug 31, 2024
2302104
more log output.
arthurschreiber Aug 31, 2024
9aa29b6
add dns log output.
arthurschreiber Aug 31, 2024
41d829d
more dns logging
arthurschreiber Aug 31, 2024
0fe8301
Actually log the error here.
arthurschreiber Aug 31, 2024
39cbe28
Slightly increase the timeout.
arthurschreiber Sep 1, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions benchmarks/message-io/incoming-message-stream.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
const { createBenchmark } = require('../common');
const { Readable } = require('stream');

const Debug = require('tedious/lib/debug');
const IncomingMessageStream = require('tedious/lib/incoming-message-stream');
const { Packet } = require('tedious/lib/packet');

const bench = createBenchmark(main, {
n: [100, 1000, 10000, 100000]
});

function main({ n }) {
const debug = new Debug();

const stream = Readable.from((async function*() {
for (let i = 0; i < n; i++) {
const packet = new Packet(2);
packet.last(true);
packet.addData(Buffer.from([1, 2, 3, 4, 5, 6, 7, 8, 9]));

yield packet.buffer;
}
})());

const incoming = new IncomingMessageStream(debug);
stream.pipe(incoming);

bench.start();
console.profile('incoming-message-stream');

(async function() {
let total = 0;

for await (m of incoming) {
for await (const buf of m) {
total += buf.length;
}
}

console.profileEnd('incoming-message-stream');
bench.end(n);
})();
}
72 changes: 72 additions & 0 deletions benchmarks/message-io/outgoing-message-stream.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
const { createBenchmark } = require('../common');
const { Duplex } = require('stream');

const Debug = require('../../lib/debug');
const OutgoingMessageStream = require('../../lib/outgoing-message-stream');
const Message = require('../../lib/message');

const bench = createBenchmark(main, {
n: [100, 1000, 10000, 100000]
});

function main({ n }) {
const debug = new Debug();

const stream = new Duplex({
read() {},
write(chunk, encoding, callback) {
// Just consume the data
callback();
}
});

const payload = [
Buffer.alloc(1024),
Buffer.alloc(1024),
Buffer.alloc(1024),
Buffer.alloc(256),
Buffer.alloc(256),
Buffer.alloc(256),
Buffer.alloc(256),
];

const out = new OutgoingMessageStream(debug, {
packetSize: 8 + 1024
});
out.pipe(stream);

bench.start();
console.profile('write-message');

function writeNextMessage(i) {
if (i == n) {
out.end();
out.once('finish', () => {
console.profileEnd('write-message');
bench.end(n);
});
return;
}

const m = new Message({ type: 2, resetConnection: false });
out.write(m);

for (const buf of payload) {
m.write(buf);
}

m.end();

if (out.needsDrain) {
out.once('drain', () => {
writeNextMessage(i + 1);
});
} else {
process.nextTick(() => {
writeNextMessage(i + 1);
});
}
}

writeNextMessage(0);
}
39 changes: 39 additions & 0 deletions benchmarks/message-io/read-message.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
const { createBenchmark } = require('../common');
const { Readable } = require('stream');

const Debug = require('tedious/lib/debug');
const MessageIO = require('tedious/lib/message-io');
const { Packet } = require('tedious/lib/packet');

const bench = createBenchmark(main, {
n: [100, 1000, 10000, 100000]
});

function main({ n }) {
const debug = new Debug();

const stream = Readable.from((async function*() {
for (let i = 0; i < n; i++) {
const packet = new Packet(2);
packet.last(true);
packet.addData(Buffer.from([1, 2, 3, 4, 5, 6, 7, 8, 9]));

yield packet.buffer;
}
})());

(async function() {
bench.start();
console.profile('read-message');

let total = 0;
for (let i = 0; i < n; i++) {
for await (const chunk of MessageIO.readMessage(stream, debug)) {
total += chunk.length;
}
}

console.profileEnd('read-message');
bench.end(n);
})();
}
43 changes: 43 additions & 0 deletions benchmarks/message-io/write-message.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
const { createBenchmark, createConnection } = require('../common');
const { Duplex } = require('stream');

const Debug = require('tedious/lib/debug');
const MessageIO = require('tedious/lib/message-io');

const bench = createBenchmark(main, {
n: [100, 1000, 10000, 100000]
});

function main({ n }) {
const debug = new Debug();

const stream = new Duplex({
read() {},
write(chunk, encoding, callback) {
// Just consume the data
callback();
}
});

const payload = [
Buffer.alloc(1024),
Buffer.alloc(1024),
Buffer.alloc(1024),
Buffer.alloc(256),
Buffer.alloc(256),
Buffer.alloc(256),
Buffer.alloc(256),
];

(async function() {
bench.start();
console.profile('write-message');

for (let i = 0; i <= n; i++) {
await MessageIO.writeMessage(stream, debug, 8 + 1024, 2, payload);
}

console.profileEnd('write-message');
bench.end(n);
})();
}
12 changes: 11 additions & 1 deletion package-lock.json

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

4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,8 @@
"semantic-release": "^22.0.12",
"sinon": "^15.2.0",
"typedoc": "^0.26.6",
"typescript": "^5.5.4"
"typescript": "^5.5.4",
"wtfnode": "^0.9.3"
},
"scripts": {
"docs": "typedoc",
Expand Down Expand Up @@ -124,6 +125,7 @@
]
},
"mocha": {
"nodeOption": "require=wtfnode",
"require": "test/setup.js",
"timeout": 5000,
"extension": [
Expand Down
Loading
Loading