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

Bind timeout function for Node.js APIs #2311

Merged
merged 3 commits into from
Oct 31, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
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
1 change: 1 addition & 0 deletions tools/nodejs_api/src_cpp/include/node_connection.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ class NodeConnection : public Napi::ObjectWrap<NodeConnection> {
Napi::Value InitAsync(const Napi::CallbackInfo& info);
void InitCppConnection();
void SetMaxNumThreadForExec(const Napi::CallbackInfo& info);
void SetQueryTimeout(const Napi::CallbackInfo& info);
Napi::Value ExecuteAsync(const Napi::CallbackInfo& info);

private:
Expand Down
35 changes: 23 additions & 12 deletions tools/nodejs_api/src_cpp/node_connection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ Napi::Object NodeConnection::Init(Napi::Env env, Napi::Object exports) {
InstanceMethod("initAsync", &NodeConnection::InitAsync),
InstanceMethod("executeAsync", &NodeConnection::ExecuteAsync),
InstanceMethod("setMaxNumThreadForExec", &NodeConnection::SetMaxNumThreadForExec),
InstanceMethod("setQueryTimeout", &NodeConnection::SetQueryTimeout),
});

exports.Set("NodeConnection", t);
Expand Down Expand Up @@ -44,6 +45,28 @@ void NodeConnection::InitCppConnection() {
database.reset();
}

void NodeConnection::SetMaxNumThreadForExec(const Napi::CallbackInfo& info) {
Napi::Env env = info.Env();
Napi::HandleScope scope(env);
size_t numThreads = info[0].ToNumber().Int64Value();
try {
this->connection->setMaxNumThreadForExec(numThreads);
} catch (const std::exception& exc) {
Napi::Error::New(env, std::string(exc.what())).ThrowAsJavaScriptException();
}
}

void NodeConnection::SetQueryTimeout(const Napi::CallbackInfo& info) {
Napi::Env env = info.Env();
Napi::HandleScope scope(env);
size_t timeout = info[0].ToNumber().Int64Value();
try {
this->connection->setQueryTimeOut(timeout);
} catch (const std::exception& exc) {
Napi::Error::New(env, std::string(exc.what())).ThrowAsJavaScriptException();
}
}

Napi::Value NodeConnection::ExecuteAsync(const Napi::CallbackInfo& info) {
Napi::Env env = info.Env();
Napi::HandleScope scope(env);
Expand All @@ -62,15 +85,3 @@ Napi::Value NodeConnection::ExecuteAsync(const Napi::CallbackInfo& info) {
}
return info.Env().Undefined();
}

void NodeConnection::SetMaxNumThreadForExec(const Napi::CallbackInfo& info) {
Napi::Env env = info.Env();
Napi::HandleScope scope(env);
size_t numThreads = info[0].ToNumber().Int64Value();
try {
this->connection->setMaxNumThreadForExec(numThreads);
} catch (const std::exception& exc) {
Napi::Error::New(env, std::string(exc.what())).ThrowAsJavaScriptException();
}
}

28 changes: 26 additions & 2 deletions tools/nodejs_api/src_js/connection.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,9 @@ class Connection {
if (this._numThreads) {
this._connection.setMaxNumThreadForExec(this._numThreads);
}
if (this._queryTimeout) {
this._connection.setQueryTimeout(this._queryTimeout);
}
resolve();
}
});
Expand Down Expand Up @@ -194,10 +197,31 @@ class Connection {
if (typeof numThreads !== "number" || !numThreads || numThreads < 0) {
throw new Error("numThreads must be a positive number.");
}
if (!this.isInitialized) {
if (this._isInitialized) {
this._connection.setMaxNumThreadForExec(numThreads);
} else {
this._numThreads = numThreads;
}
}

/**
* Set the timeout for queries. Queries that take longer than the timeout
* will be aborted.
* @param {Number} timeoutInMs the timeout in milliseconds.
*/
setQueryTimeout(timeoutInMs) {
if (
typeof timeoutInMs !== "number" ||
isNaN(timeoutInMs) ||
timeoutInMs <= 0
) {
throw new Error("timeoutInMs must be a positive number.");
}
if (this._isInitialized) {
this._connection.setQueryTimeout(timeoutInMs);
} else {
this._queryTimeout = timeoutInMs;
}
this._numThreads = numThreads;
}
}

Expand Down
40 changes: 32 additions & 8 deletions tools/nodejs_api/test/test_connection.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,10 +89,7 @@ describe("Execute", function () {
await conn.execute(preparedStatement, { 1: 0 });
assert.fail("No error thrown when the prepared statement is invalid.");
} catch (e) {
assert.equal(
e.message,
"Binder exception: Table dog does not exist."
);
assert.equal(e.message, "Binder exception: Table dog does not exist.");
}
});

Expand Down Expand Up @@ -185,10 +182,7 @@ describe("Query", function () {
await conn.query("MATCH (a:dog) RETURN COUNT(*)");
assert.fail("No error thrown when the query is invalid.");
} catch (e) {
assert.equal(
e.message,
"Binder exception: Table dog does not exist."
);
assert.equal(e.message, "Binder exception: Table dog does not exist.");
}
});

Expand All @@ -201,3 +195,33 @@ describe("Query", function () {
}
});
});

describe("Timeout", function () {
it("should abort a query if the timeout is reached", async function () {
try {
const newConn = new kuzu.Connection(db);
await newConn.init();
newConn.setQueryTimeout(1);
await newConn.query(
"MATCH (a:person)-[:knows*1..28]->(b:person) RETURN COUNT(*);"
);
assert.fail("No error thrown when the query times out.");
} catch (err) {
assert.equal(err.message, "Interrupted.");
}
});

it("should allow setting a timeout before the connection is initialized", async function () {
try {
const newConn = new kuzu.Connection(db);
newConn.setQueryTimeout(1);
await newConn.init();
await newConn.query(
"MATCH (a:person)-[:knows*1..28]->(b:person) RETURN COUNT(*);"
);
assert.fail("No error thrown when the query times out.");
} catch (err) {
assert.equal(err.message, "Interrupted.");
}
});
});
Loading