Skip to content

Commit 920cf75

Browse files
refactor: remove loop argument from Instance (#1009)
1 parent aad7ff5 commit 920cf75

File tree

4 files changed

+6
-25
lines changed

4 files changed

+6
-25
lines changed

google/cloud/sql/connector/connector.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,6 @@ async def connect_async(
234234
instance_connection_string,
235235
self._client,
236236
self._keys,
237-
self._loop,
238237
enable_iam_auth,
239238
)
240239
self._instances[instance_connection_string] = instance

google/cloud/sql/connector/instance.py

Lines changed: 6 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -143,19 +143,8 @@ class Instance:
143143
Enables automatic IAM database authentication for Postgres or MySQL
144144
instances.
145145
:type enable_iam_auth: bool
146-
147-
:param loop:
148-
A new event loop for the refresh function to run in.
149-
:type loop: asyncio.AbstractEventLoop
150146
"""
151147

152-
# asyncio.AbstractEventLoop is used because the default loop,
153-
# SelectorEventLoop, is usable on both Unix and Windows but has limited
154-
# functionality on Windows. It is recommended to use ProactorEventLoop
155-
# while developing on Windows.
156-
# Link to Github issue:
157-
# https://github.com/GoogleCloudPlatform/cloud-sql-python-connector/issues/22
158-
_loop: asyncio.AbstractEventLoop
159148
_enable_iam_auth: bool
160149
_keys: asyncio.Future
161150
_instance_connection_string: str
@@ -173,7 +162,6 @@ def __init__(
173162
instance_connection_string: str,
174163
client: CloudSQLClient,
175164
keys: asyncio.Future,
176-
loop: asyncio.AbstractEventLoop,
177165
enable_iam_auth: bool = False,
178166
) -> None:
179167
# validate and parse instance connection name
@@ -183,11 +171,11 @@ def __init__(
183171
self._instance_connection_string = instance_connection_string
184172

185173
self._enable_iam_auth = enable_iam_auth
186-
self._loop = loop
187174
self._keys = keys
188175
self._client = client
189176
self._refresh_rate_limiter = AsyncRateLimiter(
190-
max_capacity=2, rate=1 / 30, loop=self._loop
177+
max_capacity=2,
178+
rate=1 / 30,
191179
)
192180
self._refresh_in_progress = asyncio.locks.Event()
193181
self._current = self._schedule_refresh(0)
@@ -225,15 +213,15 @@ async def _perform_refresh(self) -> ConnectionInfo:
225213

226214
logger.debug(f"['{self._instance_connection_string}']: Creating context")
227215

228-
metadata_task = self._loop.create_task(
216+
metadata_task = asyncio.create_task(
229217
self._client._get_metadata(
230218
self._project,
231219
self._region,
232220
self._instance,
233221
)
234222
)
235223

236-
ephemeral_task = self._loop.create_task(
224+
ephemeral_task = asyncio.create_task(
237225
self._client._get_ephemeral(
238226
self._project,
239227
self._instance,
@@ -306,7 +294,7 @@ async def _refresh_task(self: Instance, delay: int) -> ConnectionInfo:
306294
logger.debug(f"['{self._instance_connection_string}']: Entering sleep")
307295
if delay > 0:
308296
await asyncio.sleep(delay)
309-
refresh_task = self._loop.create_task(self._perform_refresh())
297+
refresh_task = asyncio.create_task(self._perform_refresh())
310298
refresh_data = await refresh_task
311299
except asyncio.CancelledError:
312300
logger.debug(
@@ -337,7 +325,7 @@ async def _refresh_task(self: Instance, delay: int) -> ConnectionInfo:
337325
return refresh_data
338326

339327
# schedule refresh task and return it
340-
scheduled_task = self._loop.create_task(_refresh_task(self, delay))
328+
scheduled_task = asyncio.create_task(_refresh_task(self, delay))
341329
return scheduled_task
342330

343331
async def connect_info(

tests/conftest.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -138,13 +138,11 @@ async def fake_client(
138138

139139
@pytest.fixture
140140
async def instance(fake_client: CloudSQLClient) -> AsyncGenerator[Instance, None]:
141-
loop = asyncio.get_running_loop()
142141
keys = asyncio.create_task(generate_keys())
143142
instance = Instance(
144143
"test-project:test-region:test-instance",
145144
client=fake_client,
146145
keys=keys,
147-
loop=loop,
148146
)
149147
yield instance
150148
await instance.close()

tests/unit/test_instance.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -305,7 +305,6 @@ async def test_ClientResponseError(
305305
"""
306306
Test that detailed error message is applied to ClientResponseError.
307307
"""
308-
loop = asyncio.get_running_loop()
309308
# mock Cloud SQL Admin API calls with exceptions
310309
keys = asyncio.create_task(generate_keys())
311310
client = CloudSQLClient(
@@ -336,7 +335,6 @@ async def test_ClientResponseError(
336335
"my-project:my-region:my-instance",
337336
client,
338337
keys,
339-
loop,
340338
)
341339
try:
342340
await instance._current
@@ -359,14 +357,12 @@ async def test_AutoIAMAuthNotSupportedError(fake_client: CloudSQLClient) -> None
359357
Test that AutoIAMAuthNotSupported exception is raised
360358
for SQL Server instances.
361359
"""
362-
loop = asyncio.get_running_loop()
363360
# generate client key pair
364361
keys = asyncio.create_task(generate_keys())
365362
instance = Instance(
366363
"test-project:test-region:sqlserver-instance",
367364
client=fake_client,
368365
keys=keys,
369-
loop=loop,
370366
enable_iam_auth=True,
371367
)
372368
with pytest.raises(AutoIAMAuthNotSupported):

0 commit comments

Comments
 (0)