Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions monitoring/monitorlib/infrastructure.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,9 @@ def get_sub(self) -> str | None:
return None


_sessions: dict[tuple, "UTMClientSession"] = {}


class UTMClientSession(requests.Session):
"""Requests session that enables easy access to ASTM-specified UTM endpoints.

Expand All @@ -86,12 +89,25 @@ class UTMClientSession(requests.Session):
DSS).
"""

def __new__(cls, prefix_url, auth_adapter=None, timeout_seconds=None):
"""Make the session a singleton based on parameter combinaison"""

key = (prefix_url, auth_adapter, timeout_seconds)
if key not in _sessions:
_sessions[key] = super().__new__(cls)
return _sessions[key]

def __init__(
self,
prefix_url: str,
auth_adapter: AuthAdapter | None = None,
timeout_seconds: float | None = None,
):
if hasattr(
self, "_prefix_url"
): # If set, we have been reused from the singleton pattern, no need to do anything
return

super().__init__()

self._prefix_url = prefix_url[0:-1] if prefix_url[-1] == "/" else prefix_url
Expand Down
Loading