Skip to content

Commit a69a7d1

Browse files
committed
[monitorlib] Make UTMClientSession a singleton
1 parent 10ab076 commit a69a7d1

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed

monitoring/monitorlib/infrastructure.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,9 @@ def get_sub(self) -> str | None:
7575
return None
7676

7777

78+
_sessions: dict[tuple, "UTMClientSession"] = {}
79+
80+
7881
class UTMClientSession(requests.Session):
7982
"""Requests session that enables easy access to ASTM-specified UTM endpoints.
8083
@@ -86,12 +89,26 @@ class UTMClientSession(requests.Session):
8689
DSS).
8790
"""
8891

92+
def __new__(cls, prefix_url, auth_adapter=None, timeout_seconds=None):
93+
"""Make the session a singleton based on parameter combinaison"""
94+
95+
key = (prefix_url, auth_adapter, timeout_seconds)
96+
if key not in _sessions:
97+
_sessions[key] = super().__new__(cls)
98+
return _sessions[key]
99+
89100
def __init__(
90101
self,
91102
prefix_url: str,
92103
auth_adapter: AuthAdapter | None = None,
93104
timeout_seconds: float | None = None,
94105
):
106+
107+
if hasattr(
108+
self, "_prefix_url"
109+
): # If set, we have been reused from the singleton pattern, no need to do anything
110+
return
111+
95112
super().__init__()
96113

97114
self._prefix_url = prefix_url[0:-1] if prefix_url[-1] == "/" else prefix_url

0 commit comments

Comments
 (0)