@@ -54,8 +54,11 @@ async def groups_sync(iam_groups, sql_instances, credentials, private_ip=False):
5454 # set ip_type to proper type for connector
5555 ip_type = IPTypes .PRIVATE if private_ip else IPTypes .PUBLIC
5656
57+ # create aiohttp client session for async API calls
58+ client_session = ClientSession (headers = {"Content-Type" : "application/json" })
59+
5760 # create UserService object for API calls
58- user_service = UserService (credentials )
61+ user_service = UserService (client_session , credentials )
5962
6063 # keep track of IAM group and database instance tasks
6164 group_tasks = {}
@@ -178,20 +181,23 @@ async def groups_sync(iam_groups, sql_instances, credentials, private_ip=False):
178181 "[%s][%s] Users granted role: %s." % (instance , group , granted_users )
179182 )
180183
184+ # close aiohttp client session for graceful exit
185+ if not client_session .closed :
186+ await client_session .close ()
187+
181188
182189class UserService :
183190 """Helper class for building googleapis service calls."""
184191
185- def __init__ (self , creds ):
192+ def __init__ (self , client_session , creds ):
186193 """Initialize UserService instance.
187194
188195 Args:
196+ client_session: aiohttp client session object for API calls.
189197 creds: OAuth2 credentials to call admin APIs.
190198 """
199+ self .client_session = client_session
191200 self .creds = creds
192- self .client_session = ClientSession (
193- headers = {"Content-Type" : "application/json" }
194- )
195201
196202 async def get_group_members (self , group ):
197203 """Get all members of an IAM group.
@@ -316,17 +322,6 @@ async def get_database_version(self, instance_connection_name):
316322 f"Error: Failed to get the database version for `{ instance_connection_name } `. Verify instance connection name and instance details."
317323 ) from e
318324
319- def __del__ (self ):
320- """Deconstructor for UserService to close ClientSession and have
321- graceful exit.
322- """
323-
324- async def deconstruct ():
325- if not self .client_session .closed :
326- await self .client_session .close ()
327-
328- asyncio .run_coroutine_threadsafe (deconstruct (), loop = asyncio .get_event_loop ())
329-
330325
331326class RequestType (Enum ):
332327 """Helper class for supported aiohttp request types."""
0 commit comments