tursodatabase/turso
Voir sur GitHubRobust support for setting a time limit for a query
Open
#1 913 ouverte le 1 juil. 2025
enhancementhelp wanted
Métriques du dépôt
- Stars
- (19 103 stars)
- Métriques de merge PR
- (Métriques PR en attente)
Description
In Datasette I implement a time limit for untrusted SQL queries on top of SQLite. Here's the code I use for that at the moment: https://github.com/simonw/datasette/blob/e2497fdb595055a63f2c9b7b522e76d501be224c/datasette/utils/__init__.py#L186-L206
@contextmanager
def sqlite_timelimit(conn, ms):
deadline = time.perf_counter() + (ms / 1000)
# n is the number of SQLite virtual machine instructions that will be
# executed between each check. It takes about 0.08ms to execute 1000.
# https://github.com/simonw/datasette/issues/1679
n = 1000
if ms <= 20:
# This mainly happens while executing our test suite
n = 1
def handler():
if time.perf_counter() >= deadline:
# Returning 1 terminates the query with an error
return 1
conn.set_progress_handler(handler, n)
try:
yield
finally:
conn.set_progress_handler(None, n)
Ideally I'd like to handle this at the database layer - it would be great if I could do something like this instead:
PRAGMA time_limit_ms = 500;
select * from really_big_table;