-
Notifications
You must be signed in to change notification settings - Fork 30
[monitorlib] Obtain UTMClientSessions via factory rather than direct construction #1413
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,12 +1,12 @@ | ||
| from monitoring.mock_uss.app import require_config_value, webapp | ||
| from monitoring.mock_uss.config import KEY_AUTH_SPEC, KEY_DSS_URL | ||
| from monitoring.monitorlib import auth | ||
| from monitoring.monitorlib.infrastructure import UTMClientSession | ||
| from monitoring.monitorlib.infrastructure import utm_client_session_factory | ||
|
|
||
| require_config_value(KEY_DSS_URL) | ||
| require_config_value(KEY_AUTH_SPEC) | ||
|
|
||
| utm_client = UTMClientSession( | ||
| utm_client = utm_client_session_factory.get_session( | ||
| webapp.config[KEY_DSS_URL], | ||
| auth.make_auth_adapter(webapp.config[KEY_AUTH_SPEC]), | ||
| ) |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -111,6 +111,8 @@ def __init__( | |
| auth_adapter: AuthAdapter | None = None, | ||
| timeout_seconds: float | None = None, | ||
| ): | ||
| """Instances should usually be constructed using a factory to avoid unnecessary duplication.""" | ||
|
|
||
| super().__init__() | ||
|
|
||
| with UTMClientSession._session_id_lock: | ||
|
|
@@ -223,6 +225,31 @@ def delete(self, *args, **kwargs): | |
| return super().delete(*args, **kwargs) | ||
|
|
||
|
|
||
| class UTMClientSessionFactory: | ||
| _sessions: dict[tuple, UTMClientSession] | ||
|
|
||
| def __init__(self): | ||
| self._sessions = {} | ||
|
|
||
| def get_session( | ||
| self, | ||
| prefix_url: str, | ||
| auth_adapter: AuthAdapter | None = None, | ||
| timeout_seconds: float | None = None, | ||
| ) -> UTMClientSession: | ||
| key = (prefix_url, auth_adapter, timeout_seconds) | ||
| if key not in self._sessions: | ||
| self._sessions[key] = UTMClientSession( | ||
| prefix_url=prefix_url, | ||
| auth_adapter=auth_adapter, | ||
| timeout_seconds=timeout_seconds, | ||
| ) | ||
| return self._sessions[key] | ||
|
|
||
|
|
||
| utm_client_session_factory = UTMClientSessionFactory() | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: I would have made it a class method and just use
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This assumes that it could only ever make sense to have a single |
||
|
|
||
|
|
||
| class AsyncUTMTestSession: | ||
| """ | ||
| Requests Asyncio client session that provides additional functionality for running DSS concurrency tests: | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.