Simplify database locking

Use a single lock for everything to ensure we don’t go inconsistent.
One exception is the firewall nodes table which is only accessed when
pushing updated config.
This commit is contained in:
Timotej Lazar 2023-05-19 09:30:28 +02:00
parent 93458c4782
commit 22cec64bef
5 changed files with 19 additions and 18 deletions

View file

@ -13,12 +13,12 @@ def lock(name, timeout=5):
time.sleep(1)
raise TimeoutError(f'could not lock {name}')
def unlock(name):
def unlock(name='db'):
lockfile = pathlib.Path.home() / f'{name}.lock'
lockfile.unlink(missing_ok=True)
@contextlib.contextmanager
def locked(name):
def locked(name='db'):
lock(name)
try:
yield name
@ -36,9 +36,9 @@ def write(name, data):
f.close()
def load(name):
with locked(name):
with locked():
return read(name)
def save(name, data):
with locked(name):
with locked():
write(name, data)