Skip to content

Commit 6dc0015

Browse files
committed
SQLite cache: Use requests_cache.CachedSession for better concurrency
Users on Apple M2 Max reported DB lock errors: [grafana_wtf.core] INFO: Fetching dashboards in parallel with 5 concurrent requests [requests_cache.backends.sqlite] WARNING: Database is locked in thread 6272069632; retrying (1/3) Also segmentation faults, and leaks: UserWarning: resource_tracker: There appear to be 1 leaked semaphore objects to clean up at shutdown
1 parent 76e1abd commit 6dc0015

File tree

5 files changed

+30
-5
lines changed

5 files changed

+30
-5
lines changed

CHANGES.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ in progress
1212
- Add subcommand ``explore permissions``. Thanks, @meyerder.
1313
- Added support for Python 3.12
1414
- Removed support for Python 3.7
15+
- SQLite cache: Use ``requests_cache.CachedSession`` for better concurrency
16+
behaviour. Thanks, @JensRichnow and @JWCook.
1517

1618
2024-03-07 0.18.0
1719
=================

grafana_wtf/__init__.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,6 @@
22

33
__appname__ = "grafana-wtf"
44
__version__ = "0.18.0"
5+
6+
# Amalgamate `requests` to `niquests`.
7+
import grafana_wtf.compat

grafana_wtf/compat.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
from sys import modules
2+
3+
import niquests
4+
import urllib3
5+
6+
# Amalgamate the module namespace to make all modules aiming
7+
# to use `requests`, in fact use `niquests` instead.
8+
modules["requests"] = niquests
9+
modules["requests.adapters"] = niquests.adapters
10+
modules["requests.sessions"] = niquests.sessions
11+
modules["requests.exceptions"] = niquests.exceptions
12+
modules["requests.packages.urllib3"] = urllib3

grafana_wtf/core.py

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
from grafana_client.api import GrafanaApi
1717
from grafana_client.client import GrafanaClientError, GrafanaUnauthorizedError
1818
from munch import Munch, munchify
19+
from requests_cache import CachedSession
1920
from tqdm import tqdm
2021
from tqdm.contrib.logging import tqdm_logging_redirect
2122
from urllib3.exceptions import InsecureRequestWarning
@@ -34,6 +35,8 @@
3435

3536

3637
class GrafanaEngine:
38+
session = niquests.Session()
39+
3740
def __init__(self, grafana_url, grafana_token):
3841
self.grafana_url = grafana_url
3942
self.grafana_token = grafana_token
@@ -56,9 +59,11 @@ def enable_cache(self, expire_after=60, drop_cache=False):
5659
log.info(f"Response cache will expire immediately (expire_after=0)")
5760
else:
5861
log.info(f"Response cache will expire after {expire_after} seconds")
59-
requests_cache.install_cache(cache_name=__appname__, expire_after=expire_after, use_cache_dir=True)
60-
cache_database_file = requests_cache.get_cache().db_path
61-
log.info(f"Response cache database location is {cache_database_file}")
62+
63+
self.session = CachedSession(cache_name=__appname__, expire_after=expire_after, use_cache_dir=True)
64+
65+
cache = self.session.cache
66+
log.info(f"Response cache database location is: {cache.db_path}")
6267
if drop_cache:
6368
log.info("Dropping response cache")
6469
self.clear_cache()
@@ -72,8 +77,8 @@ def clear_cache(self):
7277
def enable_concurrency(self, concurrency):
7378
self.concurrency = concurrency
7479

75-
@staticmethod
76-
def grafana_client_factory(grafana_url, grafana_token=None):
80+
@classmethod
81+
def grafana_client_factory(cls, grafana_url, grafana_token=None):
7782
url = urlparse(grafana_url)
7883

7984
# Grafana API Key auth
@@ -97,6 +102,9 @@ def grafana_client_factory(grafana_url, grafana_token=None):
97102
url_path_prefix=url.path.lstrip("/"),
98103
verify=verify,
99104
)
105+
if cls.session:
106+
cls.session.headers["User-Agent"] = grafana.client.user_agent
107+
grafana.client.s = cls.session
100108

101109
return grafana
102110

tests/__init__.py

Whitespace-only changes.

0 commit comments

Comments
 (0)