-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Do not throw if `sbomnix` is executed concurrently. Signed-off-by: Henri Rosten <[email protected]>
- Loading branch information
1 parent
102f197
commit 56dcd1a
Showing
6 changed files
with
93 additions
and
50 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
# SPDX-FileCopyrightText: 2022-2024 Technology Innovation Institute (TII) | ||
# | ||
# SPDX-License-Identifier: Apache-2.0 | ||
|
||
# pylint: disable=too-few-public-methods | ||
|
||
"""Thread-safe DataFrameDiskCache""" | ||
|
||
import pathlib | ||
import tempfile | ||
from getpass import getuser | ||
|
||
from filelock import FileLock | ||
from dfdiskcache import DataFrameDiskCache | ||
|
||
############################################################################### | ||
|
||
# DataFrameDiskCache cache local path and lock file | ||
DFCACHE_PATH = pathlib.Path(tempfile.gettempdir()) / f"{getuser()}_sbomnix_df_cache" | ||
DFCACHE_LOCK = DFCACHE_PATH / "dfcache.lock" | ||
|
||
################################################################################ | ||
|
||
|
||
class LockedDfCache: | ||
"""Thread-safe (and process-safe) wrapper for DataFrameDiskCache""" | ||
|
||
def __init__(self): | ||
self.dflock = FileLock(DFCACHE_LOCK) | ||
|
||
def __getattr__(self, name): | ||
|
||
def wrap(*a, **k): | ||
with self.dflock: | ||
# We intentionally do not store the dfcache as object variable | ||
# but re-instantiate it every time any LockedDfCache method | ||
# is called. DataFrameDiskCache internally makes use of sqlite | ||
# which does not allow concurrent connections to the database. | ||
# Having the dfcache initiated once in __init__() and then | ||
# re-used here would mean the connection would remain reserved | ||
# for the first thread making other threads throw with | ||
# 'database locked' etc. even if we otherwise protect | ||
# concurrent writes. | ||
dfcache = DataFrameDiskCache(cache_dir_path=DFCACHE_PATH) | ||
return getattr(dfcache, name)(*a, **k) | ||
|
||
return wrap | ||
|
||
|
||
############################################################################### |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters