@@ -144,32 +144,13 @@ def get_preferred_ip(self, ip_type: IPTypes) -> str:
144144 )
145145
146146
147- class Instance :
148- """A class to manage the details of the connection to a Cloud SQL
149- instance, including refreshing the credentials.
150-
151- :param instance_connection_string:
152- The Google Cloud SQL Instance's connection
153- string.
154- :type instance_connection_string: str
155-
156- :param enable_iam_auth
157- Enables automatic IAM database authentication for Postgres or MySQL
158- instances.
159- :type enable_iam_auth: bool
160- """
161-
162- _enable_iam_auth : bool
163- _keys : asyncio .Future
164- _instance_connection_string : str
165- _instance : str
166- _project : str
167- _region : str
147+ class RefreshAheadCache :
148+ """Cache that refreshes connection info in the background prior to expiration.
168149
169- _refresh_rate_limiter : AsyncRateLimiter
170- _refresh_in_progress : asyncio . locks . Event
171- _current : asyncio . Task # task wraps coroutine that returns ConnectionInfo
172- _next : asyncio . Task # task wraps coroutine that returns another task
150+ Background tasks are used to schedule refresh attempts to get a new
151+ ephemeral certificate and Cloud SQL metadata (IP addresses, etc.) ahead of
152+ expiration.
153+ """
173154
174155 def __init__ (
175156 self ,
@@ -178,6 +159,18 @@ def __init__(
178159 keys : asyncio .Future ,
179160 enable_iam_auth : bool = False ,
180161 ) -> None :
162+ """Initializes a RefreshAheadCache instance.
163+
164+ Args:
165+ instance_connection_string (str): The Cloud SQL Instance's
166+ connection string (also known as an instance connection name).
167+ client (CloudSQLClient): The Cloud SQL Client instance.
168+ keys (asyncio.Future): A future to the client's public-private key
169+ pair.
170+ enable_iam_auth (bool): Enables automatic IAM database authentication
171+ (Postgres and MySQL) as the default authentication method for all
172+ connections.
173+ """
181174 # validate and parse instance connection name
182175 self ._project , self ._region , self ._instance = _parse_instance_connection_name (
183176 instance_connection_string
@@ -192,8 +185,8 @@ def __init__(
192185 rate = 1 / 30 ,
193186 )
194187 self ._refresh_in_progress = asyncio .locks .Event ()
195- self ._current = self ._schedule_refresh (0 )
196- self ._next = self ._current
188+ self ._current : asyncio . Task = self ._schedule_refresh (0 )
189+ self ._next : asyncio . Task = self ._current
197190
198191 async def force_refresh (self ) -> None :
199192 """
@@ -211,10 +204,11 @@ async def _perform_refresh(self) -> ConnectionInfo:
211204 """Retrieves instance metadata and ephemeral certificate from the
212205 Cloud SQL Instance.
213206
214- :rtype: ConnectionInfo
215- :returns: A dataclass containing a string representing the ephemeral certificate, a dict
216- containing the instances IP adresses, a string representing a PEM-encoded private key
217- and a string representing a PEM-encoded certificate authority.
207+ Returns:
208+ A ConnectionInfo instance containing a string representing the
209+ ephemeral certificate, a dict containing the instances IP adresses,
210+ a string representing a PEM-encoded private key and a string
211+ representing a PEM-encoded certificate authority.
218212 """
219213 self ._refresh_in_progress .set ()
220214 logger .debug (
@@ -290,15 +284,14 @@ def _schedule_refresh(self, delay: int) -> asyncio.Task:
290284 """
291285 Schedule task to sleep and then perform refresh to get ConnectionInfo.
292286
293- :type delay: int
294- :param delay
295- Time in seconds to sleep before running _perform_refresh.
287+ Args:
288+ delay (int): Time in seconds to sleep before performing a refresh.
296289
297- :rtype: asyncio.Task
298- :returns: A Task representing the scheduled _perform_refresh .
290+ Returns:
291+ An asyncio. Task representing the scheduled refresh .
299292 """
300293
301- async def _refresh_task (self : Instance , delay : int ) -> ConnectionInfo :
294+ async def _refresh_task (self : RefreshAheadCache , delay : int ) -> ConnectionInfo :
302295 """
303296 A coroutine that sleeps for the specified amount of time before
304297 running _perform_refresh.
@@ -349,16 +342,14 @@ async def connect_info(
349342 """Retrieve instance metadata and ip address required
350343 for making connection to Cloud SQL instance.
351344
352- :type ip_type: IPTypes
353- :param ip_type: Enum specifying whether to look for public
354- or private IP address.
355-
356- :rtype instance_data: ConnectionInfo
357- :returns: Instance metadata for Cloud SQL instance.
345+ Args:
346+ ip_type (IPTypes): Enum specifying type of IP address to lookup and
347+ use for connection.
358348
359- :rtype ip_address: str
360- :returns: A string representing the IP address of
361- the given Cloud SQL instance.
349+ Returns:
350+ A tuple with the first item being the ConnectionInfo instance for
351+ establishing the connection, and the second item being the IP
352+ address of the Cloud SQL instance matching the specified IP type.
362353 """
363354 logger .debug (
364355 f"['{ self ._instance_connection_string } ']: Entered connect_info method"
0 commit comments