Skip to content

Commit acd5a7a

Browse files
[uss_qualifier] Reduce number of UTMClientSessions generated by DSSInstanceResource and add timeout_seconds config option (#1411)
1 parent a8acd6b commit acd5a7a

File tree

4 files changed

+39
-9
lines changed

4 files changed

+39
-9
lines changed

monitoring/monitorlib/infrastructure.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,10 @@ class UTMClientSession(requests.Session):
9191
If the URL starts with '/', then automatically prefix the URL with the
9292
`prefix_url` specified on construction (this is usually the base URL of the
9393
DSS).
94+
95+
When possible, a UTMClientSession should be reused rather than creating a
96+
new one because an excessive number of UTMClientSessions can exhaust the
97+
number of connections allowed by the system (see #1407).
9498
"""
9599

96100
_next_session_id: int = 1

monitoring/uss_qualifier/resources/astm/f3411/dss.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
from implicitdict import ImplicitDict
66

77
from monitoring.monitorlib import infrastructure
8+
from monitoring.monitorlib.infrastructure import UTMClientSession
89
from monitoring.monitorlib.rid import RIDVersion
910
from monitoring.uss_qualifier.reports.report import ParticipantID
1011
from monitoring.uss_qualifier.resources.communications import AuthAdapterResource
@@ -40,12 +41,12 @@ def __init__(
4041
participant_id: ParticipantID,
4142
base_url: str,
4243
rid_version: RIDVersion,
43-
auth_adapter: infrastructure.AuthAdapter,
44+
client: UTMClientSession,
4445
):
4546
self.participant_id = participant_id
4647
self.base_url = base_url
4748
self.rid_version = rid_version
48-
self.client = infrastructure.UTMClientSession(base_url, auth_adapter)
49+
self.client = client
4950

5051
def is_same_as(self, other: DSSInstance) -> bool:
5152
return (
@@ -81,7 +82,9 @@ def __init__(
8182
specification.participant_id,
8283
specification.base_url,
8384
specification.rid_version,
84-
auth_adapter.adapter,
85+
infrastructure.UTMClientSession(
86+
specification.base_url, auth_adapter.adapter
87+
),
8588
)
8689

8790
@classmethod

monitoring/uss_qualifier/resources/astm/f3548/v21/dss.py

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,10 @@
4141
)
4242
from uas_standards.astm.f3548.v21.constants import Scope
4343

44-
from monitoring.monitorlib import infrastructure
4544
from monitoring.monitorlib.fetch import Query, QueryError, QueryType, query_and_describe
4645
from monitoring.monitorlib.fetch import scd as fetch
4746
from monitoring.monitorlib.fetch.scd import FetchedSubscription, FetchedSubscriptions
47+
from monitoring.monitorlib.infrastructure import UTMClientSession
4848
from monitoring.monitorlib.inspection import calling_function_name, fullname
4949
from monitoring.monitorlib.mutate import scd as mutate
5050
from monitoring.monitorlib.mutate.scd import MutatedSubscription
@@ -65,6 +65,9 @@ class DSSInstanceSpecification(ImplicitDict):
6565
supports_ovn_request: Optional[bool]
6666
"""Whether this DSS instance supports the optional extension not part of the original F3548 standard API allowing a USS to request a specific OVN when creating or updating an operational intent."""
6767

68+
timeout_seconds: Optional[float]
69+
"""If specified, number of seconds to allow before timing out requests to this DSS instance."""
70+
6871
def __init__(self, *args, **kwargs):
6972
super().__init__(**kwargs)
7073
try:
@@ -77,21 +80,21 @@ class DSSInstance:
7780
participant_id: str
7881
user_participant_ids: list[str]
7982
base_url: str
80-
client: infrastructure.UTMClientSession
83+
client: UTMClientSession
8184
_scopes_authorized: set[str]
8285

8386
def __init__(
8487
self,
8588
participant_id: str,
8689
user_participant_ids: list[str],
8790
base_url: str,
88-
auth_adapter: infrastructure.AuthAdapter,
91+
client: UTMClientSession,
8992
scopes_authorized: list[str],
9093
):
9194
self.participant_id = participant_id
9295
self.user_participant_ids = user_participant_ids
9396
self.base_url = base_url
94-
self.client = infrastructure.UTMClientSession(base_url, auth_adapter)
97+
self.client = client
9598
self._scopes_authorized = set(
9699
s.value if isinstance(s, Enum) else s for s in scopes_authorized
97100
)
@@ -126,7 +129,11 @@ def with_different_auth(
126129
participant_id=self.participant_id,
127130
user_participant_ids=self.user_participant_ids,
128131
base_url=self.base_url,
129-
auth_adapter=auth_adapter.adapter,
132+
client=UTMClientSession(
133+
self.base_url,
134+
auth_adapter=auth_adapter.adapter,
135+
timeout_seconds=self.client.timeout_seconds,
136+
),
130137
scopes_authorized=list(scopes_required),
131138
)
132139

@@ -694,6 +701,7 @@ def delete_subscription(self, sub_id: str, sub_version: str) -> MutatedSubscript
694701
class DSSInstanceResource(Resource[DSSInstanceSpecification]):
695702
_specification: DSSInstanceSpecification
696703
_auth_adapter: AuthAdapterResource
704+
_client: UTMClientSession
697705

698706
def __init__(
699707
self,
@@ -704,6 +712,14 @@ def __init__(
704712
super().__init__(specification, resource_origin)
705713
self._specification = specification
706714
self._auth_adapter = auth_adapter
715+
timeout_seconds = (
716+
specification.timeout_seconds
717+
if "timeout_seconds" in specification
718+
else None
719+
)
720+
self._client = UTMClientSession(
721+
self._specification.base_url, auth_adapter.adapter, timeout_seconds
722+
)
707723

708724
def can_use_scope(self, scope: str) -> bool:
709725
return scope in self._auth_adapter.scopes
@@ -777,7 +793,7 @@ def get_instance(self, scopes_required: dict[str, str]) -> DSSInstance:
777793
else []
778794
),
779795
self._specification.base_url,
780-
self._auth_adapter.adapter,
796+
self._client,
781797
list(scopes_required),
782798
)
783799

schemas/monitoring/uss_qualifier/resources/astm/f3548/v21/dss/DSSInstanceSpecification.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,13 @@
2222
"null"
2323
]
2424
},
25+
"timeout_seconds": {
26+
"description": "If specified, number of seconds to allow before timing out requests to this DSS instance.",
27+
"type": [
28+
"number",
29+
"null"
30+
]
31+
},
2532
"user_participant_ids": {
2633
"description": "IDs of any participants using this DSS instance, apart from the USS responsible for this DSS instance.",
2734
"items": {

0 commit comments

Comments
 (0)