Skip to content

Commit 88fd6f3

Browse files
committed
chore: reformat config for lazy refresh setup
1 parent d11efaf commit 88fd6f3

File tree

5 files changed

+34
-15
lines changed

5 files changed

+34
-15
lines changed

cloud-sql/mysql/sqlalchemy/connect_connector.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
# [START cloud_sql_mysql_sqlalchemy_connect_connector]
1616
import os
1717

18-
from google.cloud.sql.connector import Connector, IPTypes, RefreshStrategy
18+
from google.cloud.sql.connector import Connector, IPTypes
1919
import pymysql
2020

2121
import 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(

cloud-sql/mysql/sqlalchemy/connect_connector_auto_iam_authn.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
# [START cloud_sql_mysql_sqlalchemy_auto_iam_authn]
1616
import os
1717

18-
from google.cloud.sql.connector import Connector, IPTypes, RefreshStrategy
18+
from google.cloud.sql.connector import Connector, IPTypes
1919
import pymysql
2020

2121
import 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(

cloud-sql/postgres/sqlalchemy/connect_connector.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
# [START cloud_sql_postgres_sqlalchemy_connect_connector]
1616
import os
1717

18-
from google.cloud.sql.connector import Connector, IPTypes, RefreshStrategy
18+
from google.cloud.sql.connector import Connector, IPTypes
1919
import pg8000
2020

2121
import 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(

cloud-sql/postgres/sqlalchemy/connect_connector_auto_iam_authn.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
# [START cloud_sql_postgres_sqlalchemy_auto_iam_authn]
1616
import os
1717

18-
from google.cloud.sql.connector import Connector, IPTypes, RefreshStrategy
18+
from google.cloud.sql.connector import Connector, IPTypes
1919
import pg8000
2020

2121
import 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(

cloud-sql/sql-server/sqlalchemy/connect_connector.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
# [START cloud_sql_sqlserver_sqlalchemy_connect_connector]
1616
import os
1717

18-
from google.cloud.sql.connector import Connector, IPTypes, RefreshStrategy
18+
from google.cloud.sql.connector import Connector, IPTypes
1919
import pytds
2020

2121
import 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

0 commit comments

Comments
 (0)