Skip to content

Commit 1986be9

Browse files
committed
warning if non-flushed data is lost at close()
1 parent 6f4f4c9 commit 1986be9

4 files changed

Lines changed: 55 additions & 2 deletions

File tree

src/buffer/index.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -329,6 +329,14 @@ class SenderBuffer {
329329
this.startNewRow();
330330
}
331331

332+
/**
333+
* Returns the current position of the buffer.
334+
* New data will be written into the buffer starting from this position.
335+
*/
336+
currentPosition() {
337+
return this.position;
338+
}
339+
332340
private checkCapacity(data: string[], base = 0) {
333341
let length = base;
334342
for (const str of data) {

src/sender.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,10 +168,14 @@ class Sender {
168168
}
169169

170170
/**
171-
* Closes the TCP connection to the database. <br>
171+
* Closes the connection to the database. <br>
172172
* Data sitting in the Sender's buffer will be lost unless flush() is called before close().
173173
*/
174174
async close() {
175+
const pos = this.buffer.currentPosition();
176+
if (pos > 0) {
177+
this.log("warn", `Buffer contains data which has not been flushed, and it will be lost [position=${pos}]`);
178+
}
175179
return this.transport.close();
176180
}
177181

src/transport/tcp.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,6 @@ class TcpTransport implements SenderTransport {
176176

177177
/**
178178
* Closes the TCP connection to the database. <br>
179-
* Data sitting in the Sender's buffer will be lost unless flush() is called before close().
180179
*/
181180
async close(): Promise<void> {
182181
if (this.socket) {

test/sender.transport.test.ts

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -570,4 +570,46 @@ describe("Sender TCP suite", function () {
570570
await sender.close();
571571
await proxy.stop();
572572
});
573+
574+
it("warns if data is lost on close()", async function () {
575+
// we expect a warning about non-flushed data at close()
576+
const expectedMessages = [
577+
"Successfully connected to localhost:9088",
578+
"Buffer contains data which has not been flushed, and it will be lost [position=54]",
579+
/^Connection to .*1:9088 is closed$/,
580+
];
581+
const log = (
582+
level: "error" | "warn" | "info" | "debug",
583+
message: string,
584+
) => {
585+
if (level !== "debug") {
586+
expect(message).toMatch(expectedMessages.shift());
587+
}
588+
};
589+
const proxy = await createProxy();
590+
const sender = new Sender({
591+
protocol: "tcp",
592+
port: PROXY_PORT,
593+
host: PROXY_HOST,
594+
log: log,
595+
});
596+
await sender.connect();
597+
await sendData(sender);
598+
599+
// add another line to the buffer without calling flush()
600+
await sender
601+
.table("test")
602+
.symbol("location", "gb")
603+
.floatColumn("temperature", 16.4)
604+
.at(1658484775000000000n, "ns");
605+
606+
// assert that only the first line was sent
607+
await assertSentData(
608+
proxy,
609+
false,
610+
"test,location=us temperature=17.1 1658484765000000000\n",
611+
);
612+
await sender.close();
613+
await proxy.stop();
614+
});
573615
});

0 commit comments

Comments
 (0)