Skip to content

Commit

Permalink
fix: re-use existing connections on force refresh (#98)
Browse files Browse the repository at this point in the history
  • Loading branch information
jackwotherspoon authored Aug 18, 2023
1 parent 630869c commit 888abd4
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 5 deletions.
2 changes: 1 addition & 1 deletion google/cloud/alloydb/connector/connector.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ async def connect_async(self, instance_uri: str, driver: str, **kwargs: Any) ->
return await self._loop.run_in_executor(None, connect_partial)
except Exception:
# we attempt a force refresh, then throw the error
instance.force_refresh()
await instance.force_refresh()
raise

def __enter__(self) -> "Connector":
Expand Down
7 changes: 4 additions & 3 deletions google/cloud/alloydb/connector/instance.py
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ async def _refresh_operation(self, delay: int) -> RefreshResult:

return refresh_result

def force_refresh(self) -> None:
async def force_refresh(self) -> None:
"""
Schedules a new refresh operation immediately to be used
for future connection attempts.
Expand All @@ -196,8 +196,9 @@ def force_refresh(self) -> None:
if not self._refresh_in_progress.is_set():
self._next.cancel()
self._next = self._schedule_refresh(0)
# block all sequential connection attempts on the next refresh result
self._current = self._next
# block all sequential connection attempts on the next refresh result if current is invalid
if not await _is_valid(self._current):
self._current = self._next

async def connection_info(self) -> Tuple[str, ssl.SSLContext]:
"""
Expand Down
2 changes: 1 addition & 1 deletion tests/unit/test_instance.py
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ async def test_force_refresh_cancels_pending_refresh() -> None:
# shouldn't be set
pending_refresh = instance._next
assert instance._refresh_in_progress.is_set() is False
instance.force_refresh()
await instance.force_refresh()
# pending_refresh has to be awaited for it to raised as cancelled
with pytest.raises(asyncio.CancelledError):
assert await pending_refresh
Expand Down

0 comments on commit 888abd4

Please sign in to comment.