File tree Expand file tree Collapse file tree 5 files changed +34
-15
lines changed
Expand file tree Collapse file tree 5 files changed +34
-15
lines changed Original file line number Diff line number Diff line change 1515# [START cloud_sql_mysql_sqlalchemy_connect_connector]
1616import os
1717
18- from google .cloud .sql .connector import Connector , IPTypes , RefreshStrategy
18+ from google .cloud .sql .connector import Connector , IPTypes
1919import pymysql
2020
2121import sqlalchemy
@@ -40,9 +40,13 @@ def connect_with_connector() -> sqlalchemy.engine.base.Engine:
4040 db_name = os .environ ["DB_NAME" ] # e.g. 'my-database'
4141
4242 ip_type = IPTypes .PRIVATE if os .environ .get ("PRIVATE_IP" ) else IPTypes .PUBLIC
43- refresh_strategy = RefreshStrategy .LAZY
4443
45- connector = Connector (ip_type = ip_type , refresh_strategy = refresh_strategy )
44+ # setting the refresh strategy to LAZY
45+ # to refresh the tokens when they are needed, rather than on a regular interval
46+ # this is recommended for serverless environments to
47+ # avoid background refreshes from throttling CPU.
48+
49+ connector = Connector (ip_type = ip_type , refresh_strategy = "LAZY" )
4650
4751 def getconn () -> pymysql .connections .Connection :
4852 conn : pymysql .connections .Connection = connector .connect (
Original file line number Diff line number Diff line change 1515# [START cloud_sql_mysql_sqlalchemy_auto_iam_authn]
1616import os
1717
18- from google .cloud .sql .connector import Connector , IPTypes , RefreshStrategy
18+ from google .cloud .sql .connector import Connector , IPTypes
1919import pymysql
2020
2121import sqlalchemy
@@ -38,10 +38,14 @@ def connect_with_connector_auto_iam_authn() -> sqlalchemy.engine.base.Engine:
3838 db_name = os .environ ["DB_NAME" ] # e.g. 'my-database'
3939
4040 ip_type = IPTypes .PRIVATE if os .environ .get ("PRIVATE_IP" ) else IPTypes .PUBLIC
41- refresh_strategy = RefreshStrategy .LAZY
41+
42+ # setting the refresh strategy to LAZY
43+ # to refresh the tokens when they are needed, rather than on a regular interval
44+ # this is recommended for serverless environments to
45+ # avoid background refreshes from throttling CPU.
4246
4347 # initialize Cloud SQL Python Connector object
44- connector = Connector (refresh_strategy = refresh_strategy )
48+ connector = Connector (refresh_strategy = "LAZY" )
4549
4650 def getconn () -> pymysql .connections .Connection :
4751 conn : pymysql .connections .Connection = connector .connect (
Original file line number Diff line number Diff line change 1515# [START cloud_sql_postgres_sqlalchemy_connect_connector]
1616import os
1717
18- from google .cloud .sql .connector import Connector , IPTypes , RefreshStrategy
18+ from google .cloud .sql .connector import Connector , IPTypes
1919import pg8000
2020
2121import sqlalchemy
@@ -40,10 +40,14 @@ def connect_with_connector() -> sqlalchemy.engine.base.Engine:
4040 db_name = os .environ ["DB_NAME" ] # e.g. 'my-database'
4141
4242 ip_type = IPTypes .PRIVATE if os .environ .get ("PRIVATE_IP" ) else IPTypes .PUBLIC
43- refresh_strategy = RefreshStrategy .LAZY
43+
44+ # setting the refresh strategy to LAZY
45+ # to refresh the tokens when they are needed, rather than on a regular interval
46+ # this is recommended for serverless environments to
47+ # avoid background refreshes from throttling CPU.
4448
4549 # initialize Cloud SQL Python Connector object
46- connector = Connector (refresh_strategy = refresh_strategy )
50+ connector = Connector (refresh_strategy = "LAZY" )
4751
4852 def getconn () -> pg8000 .dbapi .Connection :
4953 conn : pg8000 .dbapi .Connection = connector .connect (
Original file line number Diff line number Diff line change 1515# [START cloud_sql_postgres_sqlalchemy_auto_iam_authn]
1616import os
1717
18- from google .cloud .sql .connector import Connector , IPTypes , RefreshStrategy
18+ from google .cloud .sql .connector import Connector , IPTypes
1919import pg8000
2020
2121import sqlalchemy
@@ -38,10 +38,13 @@ def connect_with_connector_auto_iam_authn() -> sqlalchemy.engine.base.Engine:
3838 db_name = os .environ ["DB_NAME" ] # e.g. 'my-database'
3939
4040 ip_type = IPTypes .PRIVATE if os .environ .get ("PRIVATE_IP" ) else IPTypes .PUBLIC
41- refresh_strategy = RefreshStrategy .LAZY
4241
42+ # setting the refresh strategy to LAZY
43+ # to refresh the tokens when they are needed, rather than on a regular interval
44+ # this is recommended for serverless environments to
45+ # avoid background refreshes from throttling CPU.
4346 # initialize Cloud SQL Python Connector object
44- connector = Connector (refresh_strategy = refresh_strategy )
47+ connector = Connector (refresh_strategy = "LAZY" )
4548
4649 def getconn () -> pg8000 .dbapi .Connection :
4750 conn : pg8000 .dbapi .Connection = connector .connect (
Original file line number Diff line number Diff line change 1515# [START cloud_sql_sqlserver_sqlalchemy_connect_connector]
1616import os
1717
18- from google .cloud .sql .connector import Connector , IPTypes , RefreshStrategy
18+ from google .cloud .sql .connector import Connector , IPTypes
1919import pytds
2020
2121import sqlalchemy
@@ -40,9 +40,13 @@ def connect_with_connector() -> sqlalchemy.engine.base.Engine:
4040 db_name = os .environ ["DB_NAME" ] # e.g. 'my-database'
4141
4242 ip_type = IPTypes .PRIVATE if os .environ .get ("PRIVATE_IP" ) else IPTypes .PUBLIC
43- refresh_strategy = RefreshStrategy .LAZY
43+
44+ # setting the refresh strategy to LAZY
45+ # to refresh the tokens when they are needed, rather than on a regular interval
46+ # this is recommended for serverless environments to
47+ # avoid background refreshes from throttling CPU.
4448
45- connector = Connector (ip_type = ip_type , refresh_strategy = refresh_strategy )
49+ connector = Connector (ip_type = ip_type , refresh_strategy = "LAZY" )
4650
4751 connect_args = {}
4852 # If your SQL Server instance requires SSL, you need to download the CA
You can’t perform that action at this time.
0 commit comments