Skip to content

Commit f776858

Browse files
committed
Session for each async request
1 parent 0bdb2e9 commit f776858

File tree

2 files changed

+20
-22
lines changed

2 files changed

+20
-22
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ __pycache__/
88
coverage.xml
99
requirements.txt
1010
requirements-last-build.txt
11+
.last_refresh_unit_status.json
1112

1213
# PyCharm project folder.
1314
.idea/

src/cluster.py

Lines changed: 19 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -121,17 +121,6 @@ class ClusterMember(TypedDict):
121121
lag: int
122122

123123

124-
async def _aiohttp_get_request(session, ssl_ctx, url):
125-
try:
126-
async with session.get(url, ssl=ssl_ctx) as response:
127-
if response.status > 299:
128-
logger.debug("Call failed with status code {response.status}: {response.text()}")
129-
return
130-
return await response.json()
131-
except (ClientError, ValueError):
132-
return None
133-
134-
135124
class Patroni:
136125
"""This class handles the bootstrap of a PostgreSQL database through Patroni."""
137126

@@ -311,7 +300,7 @@ def get_member_status(self, member_name: str) -> str:
311300
return member["state"]
312301
return ""
313302

314-
async def _async_get_request(self, uri, endpoints):
303+
async def _aiohttp_get_request(self, url):
315304
ssl_ctx = ssl.create_default_context()
316305
try:
317306
ssl_ctx.load_verify_locations(cafile=f"{PATRONI_CONF_PATH}/{TLS_CA_FILE}")
@@ -321,16 +310,24 @@ async def _async_get_request(self, uri, endpoints):
321310
auth=self._patroni_async_auth,
322311
timeout=ClientTimeout(total=API_REQUEST_TIMEOUT),
323312
) as session:
324-
tasks = [
325-
_aiohttp_get_request(session, ssl_ctx, f"http://{ip}:8008{uri}")
326-
for ip in endpoints
327-
] + [
328-
_aiohttp_get_request(session, ssl_ctx, f"https://{ip}:8008{uri}")
329-
for ip in endpoints
330-
]
331-
for routine in as_completed(tasks):
332-
if result := await routine:
333-
return result
313+
try:
314+
async with session.get(url, ssl=ssl_ctx) as response:
315+
if response.status > 299:
316+
logger.debug(
317+
"Call failed with status code {response.status}: {response.text()}"
318+
)
319+
return
320+
return await response.json()
321+
except (ClientError, ValueError):
322+
return None
323+
324+
async def _async_get_request(self, uri, endpoints):
325+
tasks = [self._aiohttp_get_request(f"http://{ip}:8008{uri}") for ip in endpoints] + [
326+
self._aiohttp_get_request(f"https://{ip}:8008{uri}") for ip in endpoints
327+
]
328+
for routine in as_completed(tasks):
329+
if result := await routine:
330+
return result
334331

335332
def parallel_patroni_get_request(self, uri: str, endpoints: list[str] | None = None) -> dict:
336333
"""Call all possible patroni endpoints in parallel."""

0 commit comments

Comments
 (0)