Skip to content

Commit 5d9c473

Browse files
Address PR comments
1 parent 1ff96c7 commit 5d9c473

File tree

5 files changed

+33
-46
lines changed

5 files changed

+33
-46
lines changed

google/cloud/alloydb/connector/async_connector.py

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,9 @@
1515
from __future__ import annotations
1616

1717
import asyncio
18-
import io
1918
import logging
2019
from types import TracebackType
21-
from typing import Any, Optional, TYPE_CHECKING, Union
20+
from typing import Any, Optional, TYPE_CHECKING
2221

2322
import google.auth
2423
from google.auth.credentials import with_scopes_if_required
@@ -30,7 +29,7 @@
3029
from google.cloud.alloydb.connector.enums import RefreshStrategy
3130
from google.cloud.alloydb.connector.instance import RefreshAheadCache
3231
from google.cloud.alloydb.connector.lazy import LazyRefreshCache
33-
from google.cloud.alloydb.connector.static import StaticConnectionInfoCache
32+
from google.cloud.alloydb.connector.types import CacheTypes
3433
from google.cloud.alloydb.connector.utils import generate_keys
3534

3635
if TYPE_CHECKING:
@@ -61,9 +60,6 @@ class AsyncConnector:
6160
of the following: RefreshStrategy.LAZY ("LAZY") or
6261
RefreshStrategy.BACKGROUND ("BACKGROUND").
6362
Default: RefreshStrategy.BACKGROUND
64-
static_conn_info (io.TextIOBase): A file-like JSON object that contains
65-
static connection info for the StaticConnectionInfoCache.
66-
Defaults to None, which will not use the StaticConnectionInfoCache.
6763
"""
6864

6965
def __init__(
@@ -75,9 +71,8 @@ def __init__(
7571
ip_type: str | IPTypes = IPTypes.PRIVATE,
7672
user_agent: Optional[str] = None,
7773
refresh_strategy: str | RefreshStrategy = RefreshStrategy.BACKGROUND,
78-
static_conn_info: Optional[io.TextIOBase] = None,
7974
) -> None:
80-
self._cache: dict[str, Union[RefreshAheadCache, LazyRefreshCache]] = {}
75+
self._cache: dict[str, CacheTypes] = {}
8176
# initialize default params
8277
self._quota_project = quota_project
8378
self._alloydb_api_endpoint = alloydb_api_endpoint
@@ -106,7 +101,6 @@ def __init__(
106101
except RuntimeError:
107102
self._keys = None
108103
self._client: Optional[AlloyDBClient] = None
109-
self._static_conn_info = static_conn_info
110104

111105
async def connect(
112106
self,
@@ -147,11 +141,8 @@ async def connect(
147141
enable_iam_auth = kwargs.pop("enable_iam_auth", self._enable_iam_auth)
148142

149143
# use existing connection info if possible
150-
cache: Union[RefreshAheadCache, LazyRefreshCache, StaticConnectionInfoCache]
151144
if instance_uri in self._cache:
152145
cache = self._cache[instance_uri]
153-
elif self._static_conn_info:
154-
cache = StaticConnectionInfoCache(instance_uri, self._static_conn_info)
155146
else:
156147
if self._refresh_strategy == RefreshStrategy.LAZY:
157148
logger.debug(

google/cloud/alloydb/connector/connector.py

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
import struct
2323
from threading import Thread
2424
from types import TracebackType
25-
from typing import Any, Optional, TYPE_CHECKING, Union
25+
from typing import Any, Optional, TYPE_CHECKING
2626

2727
from google.auth import default
2828
from google.auth.credentials import TokenState
@@ -35,7 +35,7 @@
3535
from google.cloud.alloydb.connector.instance import RefreshAheadCache
3636
from google.cloud.alloydb.connector.lazy import LazyRefreshCache
3737
import google.cloud.alloydb.connector.pg8000 as pg8000
38-
from google.cloud.alloydb.connector.static import StaticConnectionInfoCache
38+
from google.cloud.alloydb.connector.types import CacheTypes
3939
from google.cloud.alloydb.connector.utils import generate_keys
4040
import google.cloud.alloydb_connectors_v1.proto.resources_pb2 as connectorspb
4141

@@ -93,7 +93,7 @@ def __init__(
9393
self._loop: asyncio.AbstractEventLoop = asyncio.new_event_loop()
9494
self._thread = Thread(target=self._loop.run_forever, daemon=True)
9595
self._thread.start()
96-
self._cache: dict[str, Union[RefreshAheadCache, LazyRefreshCache]] = {}
96+
self._cache: dict[str, CacheTypes] = {}
9797
# initialize default params
9898
self._quota_project = quota_project
9999
self._alloydb_api_endpoint = alloydb_api_endpoint
@@ -176,11 +176,8 @@ async def connect_async(self, instance_uri: str, driver: str, **kwargs: Any) ->
176176
)
177177
enable_iam_auth = kwargs.pop("enable_iam_auth", self._enable_iam_auth)
178178
# use existing connection info if possible
179-
cache: Union[RefreshAheadCache, LazyRefreshCache, StaticConnectionInfoCache]
180179
if instance_uri in self._cache:
181180
cache = self._cache[instance_uri]
182-
elif self._static_conn_info:
183-
cache = StaticConnectionInfoCache(instance_uri, self._static_conn_info)
184181
else:
185182
if self._refresh_strategy == RefreshStrategy.LAZY:
186183
logger.debug(
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# Copyright 2025 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
import typing
16+
17+
from google.cloud.alloydb.connector.instance import RefreshAheadCache
18+
from google.cloud.alloydb.connector.lazy import LazyRefreshCache
19+
from google.cloud.alloydb.connector.static import StaticConnectionInfoCache
20+
21+
CacheTypes = typing.Union[
22+
RefreshAheadCache, LazyRefreshCache, StaticConnectionInfoCache
23+
]

tests/unit/conftest.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import socket
1717
import ssl
1818
from threading import Thread
19+
from typing import Final
1920

2021
from aiofiles.tempfile import TemporaryDirectory
2122
from mocks import FakeAlloyDBClient
@@ -26,6 +27,8 @@
2627

2728
from google.cloud.alloydb.connector.utils import _write_to_file
2829

30+
DELAY: Final[float] = 1.0
31+
2932

3033
@pytest.fixture
3134
def credentials() -> FakeCredentials:
@@ -86,4 +89,4 @@ def proxy_server(fake_instance: FakeInstance) -> None:
8689
daemon=True,
8790
)
8891
thread.start()
89-
thread.join(0.1) # wait 100ms to allow the proxy server to start
92+
thread.join(DELAY) # add a delay to allow the proxy server to start

tests/unit/test_async_connector.py

Lines changed: 0 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
from mocks import FakeAlloyDBClient
2121
from mocks import FakeConnectionInfo
2222
from mocks import FakeCredentials
23-
from mocks import write_static_info
2423
import pytest
2524

2625
from google.cloud.alloydb.connector import AsyncConnector
@@ -334,29 +333,3 @@ async def test_Connector_remove_cached_no_ip_type(credentials: FakeCredentials)
334333
await connector.connect(instance_uri, "asyncpg", ip_type="private")
335334
# check that cache has been removed from dict
336335
assert instance_uri not in connector._cache
337-
338-
339-
async def test_Connector_static_connection_info(
340-
credentials: FakeCredentials, fake_client: FakeAlloyDBClient
341-
) -> None:
342-
"""
343-
Test that AsyncConnector.__init__() can specify a static connection info to
344-
connect to an instance.
345-
"""
346-
static_info = write_static_info(fake_client.instance)
347-
async with AsyncConnector(
348-
credentials=credentials, static_conn_info=static_info
349-
) as connector:
350-
connector._client = fake_client
351-
# patch db connection creation
352-
with patch("google.cloud.alloydb.connector.asyncpg.connect") as mock_connect:
353-
mock_connect.return_value = True
354-
connection = await connector.connect(
355-
fake_client.instance.uri(),
356-
"asyncpg",
357-
user="test-user",
358-
password="test-password",
359-
db="test-db",
360-
)
361-
# check connection is returned
362-
assert connection is True

0 commit comments

Comments
 (0)