Skip to content

Commit 8f7bae7

Browse files
authored
fix: run credential refresh in thread (#508)
The async connector must sometimes refresh its credentials, but prior to this commit the refresh operation would block the main thread. Now that this library supports at minimum Python 3.10, we may use the asyncio.to_thread method to run the refresh on a separate thread, awaiting its result before continuing with the connection info refresh operation. Fixes #245
1 parent c5ea645 commit 8f7bae7

1 file changed

Lines changed: 3 additions & 2 deletions

File tree

google/cloud/alloydbconnector/client.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -239,9 +239,10 @@ async def get_connection_info(
239239
"""
240240
priv_key, pub_key = await keys
241241

242-
# before making AlloyDB API calls, refresh creds if required
242+
# Before making AlloyDB API calls, refresh creds if required
243+
# Run refresh in a separate thread to avoid blocking the main thread.
243244
if not self._credentials.token_state == TokenState.FRESH:
244-
self._credentials.refresh(requests.Request())
245+
await asyncio.to_thread(self._credentials.refresh, requests.Request())
245246

246247
# fetch metadata
247248
metadata_task = asyncio.create_task(

0 commit comments

Comments
 (0)