Skip to content

Commit

Permalink
fix: use lazy refresh for AlloyDB and Cloud SQL Connector (#429)
Browse files Browse the repository at this point in the history
The Cloud SQL Python Connector just released a new version `v1.10.0`,
same with AlloyDB Python Connector `v1.2.0` that support setting the
`refresh_strategy` argument of the connector.

Setting the refresh strategy to lazy refresh is recommended for
serverless environments. As the default refresh strategy is to
have background refreshes occur to get the instance metadata and a fresh
certificate to use for the SSL/TLS connection.

However, these background refreshes can be throttled when serverless
environments scale to zero (Cloud Functions, Cloud Run etc.).

This PR will fix the ambiguous errors that are a result of the CPU being
throttled for connectors.
  • Loading branch information
jackwotherspoon committed Jun 26, 2024
1 parent 41f3bb7 commit c73484d
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 8 deletions.
6 changes: 4 additions & 2 deletions retrieval_service/datastore/providers/alloydb.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
from typing import Any, Literal, Optional

import asyncpg
from google.cloud.alloydb.connector import AsyncConnector
from google.cloud.alloydb.connector import AsyncConnector, RefreshStrategy
from pgvector.asyncpg import register_vector
from pydantic import BaseModel
from sqlalchemy import text
Expand Down Expand Up @@ -54,7 +54,9 @@ def __init__(self, pool: AsyncEngine):
@classmethod
async def create(cls, config: Config) -> "Client":
async def getconn() -> asyncpg.Connection:
async with AsyncConnector() as connector:
async with AsyncConnector(
refresh_strategy=RefreshStrategy.LAZY
) as connector:
conn: asyncpg.Connection = await connector.connect(
# Alloydb instance connection name
f"projects/{config.project}/locations/{config.region}/clusters/{config.cluster}/instances/{config.instance}",
Expand Down
4 changes: 2 additions & 2 deletions retrieval_service/datastore/providers/cloudsql_mysql.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
from typing import Any, Literal, Optional

import pymysql
from google.cloud.sql.connector import Connector
from google.cloud.sql.connector import Connector, RefreshStrategy
from pydantic import BaseModel
from sqlalchemy import Engine, create_engine, text
from sqlalchemy.engine.base import Engine
Expand Down Expand Up @@ -54,7 +54,7 @@ def __init__(self, pool: Engine, db_name: str):
@classmethod
def create_sync(cls, config: Config) -> "Client":
def getconn() -> pymysql.Connection:
with Connector() as connector:
with Connector(refresh_strategy=RefreshStrategy.LAZY) as connector:
conn: pymysql.Connection = connector.connect(
# Cloud SQL instance connection name
f"{config.project}:{config.region}:{config.instance}",
Expand Down
6 changes: 4 additions & 2 deletions retrieval_service/datastore/providers/cloudsql_postgres.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
from typing import Any, Literal, Optional

import asyncpg
from google.cloud.sql.connector import Connector
from google.cloud.sql.connector import Connector, RefreshStrategy
from pgvector.asyncpg import register_vector
from pydantic import BaseModel
from sqlalchemy import text
Expand Down Expand Up @@ -55,7 +55,9 @@ async def create(cls, config: Config) -> "Client":
loop = asyncio.get_running_loop()

async def getconn() -> asyncpg.Connection:
async with Connector(loop=loop) as connector:
async with Connector(
loop=loop, refresh_strategy=RefreshStrategy.LAZY
) as connector:
conn: asyncpg.Connection = await connector.connect_async(
# Cloud SQL instance connection name
f"{config.project}:{config.region}:{config.instance}",
Expand Down
4 changes: 2 additions & 2 deletions retrieval_service/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ langchain-core==0.2.8
pgvector==0.2.5
pydantic==2.6.1
uvicorn[standard]==0.27.0.post1
cloud-sql-python-connector==1.6.0
google-cloud-alloydb-connector[asyncpg]==1.1.1
cloud-sql-python-connector==1.10.0
google-cloud-alloydb-connector[asyncpg]==1.2.0
sqlalchemy==2.0.25
pandas==2.2.2
pandas-stubs==2.2.2.240603
Expand Down

0 comments on commit c73484d

Please sign in to comment.