feat: add an API to time out query executions

Currently, if a predicate is hard to match on the Rust side, a sizable
query against a very large file can take forever, and ends up hanging.
This commit adds an API function `ts_query_cursor_set_timeout_micros` to
limit how long query execution is allowed to take, thereby negating the
chance of a hang to occur.
This commit is contained in:
Amaan Qureshi 2024-08-29 17:21:52 -04:00
parent a748488596
commit 3f424c0121
11 changed files with 132 additions and 8 deletions

View file

@ -792,7 +792,8 @@ void ts_query_matches_wasm(
uint32_t start_index,
uint32_t end_index,
uint32_t match_limit,
uint32_t max_start_depth
uint32_t max_start_depth,
uint32_t timeout_micros
) {
if (!scratch_query_cursor) {
scratch_query_cursor = ts_query_cursor_new();
@ -810,6 +811,7 @@ void ts_query_matches_wasm(
ts_query_cursor_set_byte_range(scratch_query_cursor, start_index, end_index);
ts_query_cursor_set_match_limit(scratch_query_cursor, match_limit);
ts_query_cursor_set_max_start_depth(scratch_query_cursor, max_start_depth);
ts_query_cursor_set_timeout_micros(scratch_query_cursor, timeout_micros);
ts_query_cursor_exec(scratch_query_cursor, self, node);
uint32_t index = 0;
@ -847,7 +849,8 @@ void ts_query_captures_wasm(
uint32_t start_index,
uint32_t end_index,
uint32_t match_limit,
uint32_t max_start_depth
uint32_t max_start_depth,
uint32_t timeout_micros
) {
if (!scratch_query_cursor) {
scratch_query_cursor = ts_query_cursor_new();
@ -862,6 +865,7 @@ void ts_query_captures_wasm(
ts_query_cursor_set_byte_range(scratch_query_cursor, start_index, end_index);
ts_query_cursor_set_match_limit(scratch_query_cursor, match_limit);
ts_query_cursor_set_max_start_depth(scratch_query_cursor, max_start_depth);
ts_query_cursor_set_timeout_micros(scratch_query_cursor, timeout_micros);
ts_query_cursor_exec(scratch_query_cursor, self, node);
unsigned index = 0;