Better handling of busy errors when reading from live cache

This commit is contained in:
Kovid Goyal 2022-04-03 08:58:16 +05:30
parent 80863f5a97
commit c89511cda8
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C

View File

@ -135,7 +135,9 @@ def cache_path():
def db():
return apsw.Connection(cache_path())
ans = apsw.Connection(cache_path())
ans.cursor().execute('pragma busy_timeout=2000')
return ans
def table_definition():
@ -166,12 +168,13 @@ def write_to_cache(full_name, etag, data):
def read_from_cache(full_name):
rowid = etag = data = date = None
database = db()
c = db().cursor()
with suppress(StopIteration):
rowid, etag, data, date = next(database.cursor().execute(
rowid, etag, data, date = next(c.execute(
table_definition() + 'SELECT id, etag, data, date FROM modules WHERE full_name=? LIMIT 1', (full_name,)))
if rowid is not None:
database.cursor().execute('UPDATE modules SET atime=CURRENT_TIMESTAMP WHERE id=?', (rowid,))
with suppress(apsw.BusyError):
c.execute('UPDATE modules SET atime=CURRENT_TIMESTAMP WHERE id=?', (rowid,))
if date is not None:
date = parse_iso8601(date, assume_utc=True)
return etag, data, date