I'm using a Direct VPC Connector to connect a Cloud Run service and a PgBouncer on a Compute Engine instance, which acts as a connection pooler for a SQL database instance. The problem arises when my Cloud Run service restarts. After the restart, it loses its ability to connect to the PgBouncer instance.
Here are some more specific observations:
- Restart: The service restart is triggered by a script that makes calls until it saturates the internal memory of the Cloud Run instance.
- Ephemeral Connections: The connection between Cloud Run and PgBouncer are established and closed for each individual request.
- No Active Connections on PgBouncer: After the Cloud Run service restarts, there are no active connections visible on the PgBouncer side.
- Workarounds: The problem can be resolved by deploying a new revision of the Cloud Run service.
- Serverless VPC Connector as a Solution: Interestingly, the issue doesn't occur if I use a Serverless VPC Connector to establish the connection between Cloud Run and PgBouncer instead of a Direct VPC Connector.