Skip to content

Commit 68776f3

Browse files
chore: merge main
2 parents ebd7e7a + 35d417d commit 68776f3

30 files changed

+708
-63
lines changed

.github/workflows/codeql.yaml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,16 +55,16 @@ jobs:
5555

5656
# Initializes the CodeQL tools for scanning.
5757
- name: Initialize CodeQL
58-
uses: github/codeql-action/init@012739e5082ff0c22ca6d6ab32e07c36df03c4a4 # v3.22.12
58+
uses: github/codeql-action/init@e5f05b81d5b6ff8cfa111c80c22c5fd02a384118 # v3.23.0
5959
with:
6060
languages: ${{ matrix.language }}
6161

6262
# Autobuild attempts to build any compiled languages (C/C++, C#, Go, or Java).
6363
# If this step fails, then you should remove it and run the build manually
6464
- name: Autobuild
65-
uses: github/codeql-action/autobuild@012739e5082ff0c22ca6d6ab32e07c36df03c4a4 # v3.22.12
65+
uses: github/codeql-action/autobuild@e5f05b81d5b6ff8cfa111c80c22c5fd02a384118 # v3.23.0
6666

6767
- name: Perform CodeQL Analysis
68-
uses: github/codeql-action/analyze@012739e5082ff0c22ca6d6ab32e07c36df03c4a4 # v3.22.12
68+
uses: github/codeql-action/analyze@e5f05b81d5b6ff8cfa111c80c22c5fd02a384118 # v3.23.0
6969
with:
7070
category: "/language:${{matrix.language}}"

.github/workflows/scorecard.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,6 @@ jobs:
6565

6666
# Upload the results to GitHub's code scanning dashboard.
6767
- name: "Upload to code-scanning"
68-
uses: github/codeql-action/upload-sarif@012739e5082ff0c22ca6d6ab32e07c36df03c4a4 # v3.22.12
68+
uses: github/codeql-action/upload-sarif@e5f05b81d5b6ff8cfa111c80c22c5fd02a384118 # v3.23.0
6969
with:
7070
sarif_file: resultsFiltered.sarif

.github/workflows/tests.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ jobs:
7676

7777
- id: 'auth'
7878
name: Authenticate to Google Cloud
79-
uses: google-github-actions/auth@67e9c72af6e0492df856527b474995862b7b6591 # v2.0.0
79+
uses: google-github-actions/auth@f6de81663f7788d05bd15bcce18f0e57f23f0846 # v2.0.1
8080
with:
8181
workload_identity_provider: ${{ secrets.PROVIDER_NAME }}
8282
service_account: ${{ secrets.SERVICE_ACCOUNT }}
@@ -154,7 +154,7 @@ jobs:
154154

155155
- id: 'auth'
156156
name: 'Authenticate to Google Cloud'
157-
uses: google-github-actions/auth@67e9c72af6e0492df856527b474995862b7b6591 # v2.0.0
157+
uses: google-github-actions/auth@f6de81663f7788d05bd15bcce18f0e57f23f0846 # v2.0.1
158158
with:
159159
workload_identity_provider: ${{ secrets.PROVIDER_NAME }}
160160
service_account: ${{ secrets.SERVICE_ACCOUNT }}

.kokoro/requirements.txt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -281,9 +281,9 @@ jeepney==0.8.0 \
281281
# via
282282
# keyring
283283
# secretstorage
284-
jinja2==3.1.2 \
285-
--hash=sha256:31351a702a408a9e7595a8fc6150fc3f43bb6bf7e319770cbc0db9df9437e852 \
286-
--hash=sha256:6088930bfe239f0e6710546ab9c19c9ef35e29792895fed6e6e31a023a182a61
284+
jinja2==3.1.3 \
285+
--hash=sha256:7d6d50dd97d52cbc355597bd845fabfbac3f551e1f99619e39a35ce8c370b5fa \
286+
--hash=sha256:ac8bd6544d4bb2c9792bf3a159e80bba8fda7f07e81bc3aed565432d5925ba90
287287
# via gcp-releasetool
288288
keyring==23.11.0 \
289289
--hash=sha256:3dd30011d555f1345dec2c262f0153f2f0ca6bca041fb1dc4588349bb4c0ac1e \

README.md

Lines changed: 127 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,22 @@ Currently supported drivers are:
3434

3535
You can install this library with `pip install`:
3636

37+
### pg8000
38+
3739
```sh
3840
pip install "google-cloud-alloydb-connector[pg8000]"
3941
```
4042

43+
See [Synchronous Driver Usage](#synchronous-driver-usage) for details.
44+
45+
### asyncpg
46+
47+
```sh
48+
pip install "google-cloud-alloydb-connector[asyncpg]"
49+
```
50+
51+
See [Async Driver Usage](#async-driver-usage) for details.
52+
4153
### APIs and Services
4254

4355
This package requires the following to connect successfully:
@@ -70,7 +82,7 @@ This package provides several functions for authorizing and encrypting
7082
connections. These functions are used with your database driver to connect to
7183
your AlloyDB instance.
7284

73-
AlloyDB supports network connectivity through private, internal IP addresses only.
85+
AlloyDB supports network connectivity through private, internal IP addresses only.
7486
This package must be run in an environment that is connected to the
7587
[VPC Network][vpc] that hosts your AlloyDB private IP address.
7688

@@ -79,7 +91,7 @@ Please see [Configuring AlloyDB Connectivity][alloydb-connectivity] for more det
7991
[vpc]: https://cloud.google.com/vpc/docs/vpc
8092
[alloydb-connectivity]: https://cloud.google.com/alloydb/docs/configure-connectivity
8193

82-
### How to use this Connector
94+
### Synchronous Driver Usage
8395

8496
To connect to AlloyDB using the connector, inititalize a `Connector`
8597
object and call it's `connect` method with the proper input parameters.
@@ -151,7 +163,7 @@ To close the `Connector` object's background resources, call it's `close()` meth
151163
connector.close()
152164
```
153165

154-
### Using Connector as a Context Manager
166+
### Synchronous Context Manager
155167

156168
The `Connector` object can also be used as a context manager in order to
157169
automatically close and cleanup resources, removing the need for explicit
@@ -202,6 +214,118 @@ with pool.connect() as db_conn:
202214
print(row)
203215
```
204216

217+
### Async Driver Usage
218+
219+
The AlloyDB Connector is compatible with [asyncio][] to improve the speed and
220+
efficiency of database connections through concurrency. The `AsyncConnector`
221+
currently supports the following asyncio database drivers:
222+
223+
- [asyncpg](https://magicstack.github.io/asyncpg)
224+
225+
[asyncio]: https://docs.python.org/3/library/asyncio.html
226+
227+
```python
228+
import asyncpg
229+
230+
import sqlalchemy
231+
from sqlalchemy.ext.asyncio import AsyncEngine, create_async_engine
232+
233+
from google.cloud.alloydb.connector import AsyncConnector
234+
235+
async def init_connection_pool(connector: AsyncConnector) -> AsyncEngine:
236+
# initialize Connector object for connections to AlloyDB
237+
async def getconn() -> asyncpg.Connection:
238+
conn: asyncpg.Connection = await connector.connect(
239+
"projects/<YOUR_PROJECT>/locations/<YOUR_REGION>/clusters/<YOUR_CLUSTER>/instances/<YOUR_INSTANCE>",
240+
"asyncpg",
241+
user="my-user",
242+
password="my-password",
243+
db="my-db-name"
244+
# ... additional database driver args
245+
)
246+
return conn
247+
248+
# The AlloyDB Python Connector can be used along with SQLAlchemy using the
249+
# 'async_creator' argument to 'create_async_engine'
250+
pool = create_async_engine(
251+
"postgresql+asyncpg://",
252+
async_creator=getconn,
253+
)
254+
return pool
255+
256+
async def main():
257+
connector = AsyncConnector()
258+
259+
# initialize connection pool
260+
pool = await init_connection_pool(connector)
261+
262+
# example query
263+
async with pool.connect() as conn:
264+
await conn.execute(sqlalchemy.text("SELECT NOW()"))
265+
266+
# dispose of connection pool
267+
await pool.dispose()
268+
269+
# close Connector
270+
await connector.close()
271+
272+
```
273+
274+
For more details on additional arguments with an `asyncpg.Connection`, please
275+
visit the [official documentation][asyncpg-docs].
276+
277+
278+
[asyncpg-docs]: https://magicstack.github.io/asyncpg/current/api/index.html
279+
280+
### Async Context Manager
281+
282+
The `AsyncConnector` also may be used as an async context manager, removing the
283+
need for explicit calls to `connector.close()` to cleanup resources.
284+
285+
```python
286+
import asyncio
287+
import asyncpg
288+
289+
import sqlalchemy
290+
from sqlalchemy.ext.asyncio import AsyncEngine, create_async_engine
291+
292+
from google.cloud.alloydb.connector import AsyncConnector
293+
294+
async def init_connection_pool(connector: AsyncConnector) -> AsyncEngine:
295+
# initialize Connector object for connections to AlloyDB
296+
async def getconn() -> asyncpg.Connection:
297+
conn: asyncpg.Connection = await connector.connect(
298+
"projects/<YOUR_PROJECT>/locations/<YOUR_REGION>/clusters/<YOUR_CLUSTER>/instances/<YOUR_INSTANCE>",
299+
"asyncpg",
300+
user="my-user",
301+
password="my-password",
302+
db="my-db-name"
303+
# ... additional database driver args
304+
)
305+
return conn
306+
307+
# The AlloyDB Python Connector can be used along with SQLAlchemy using the
308+
# 'async_creator' argument to 'create_async_engine'
309+
pool = create_async_engine(
310+
"postgresql+asyncpg://",
311+
async_creator=getconn,
312+
)
313+
return pool
314+
315+
async def main():
316+
# initialize Connector object for connections to AlloyDB
317+
async with AsyncConnector() as connector:
318+
# initialize connection pool
319+
pool = await init_connection_pool(connector)
320+
321+
# example query
322+
async with pool.connect() as conn:
323+
await conn.execute(sqlalchemy.text("SELECT NOW()"))
324+
325+
# dispose of connection pool
326+
await pool.dispose()
327+
```
328+
205329
## Support policy
206330

207331
### Major version lifecycle

google/api/field_behavior_pb2.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
# -*- coding: utf-8 -*-
1717
# Generated by the protocol buffer compiler. DO NOT EDIT!
1818
# source: google/api/field_behavior.proto
19+
# isort: skip_file
1920
"""Generated protocol buffer code."""
2021
from google.protobuf import descriptor as _descriptor
2122
from google.protobuf import descriptor_pool as _descriptor_pool

google/cloud/alloydb/connector/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@
1111
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
14+
from google.cloud.alloydb.connector.async_connector import AsyncConnector
1415
from google.cloud.alloydb.connector.connector import Connector
1516
from google.cloud.alloydb.connector.version import __version__
1617

17-
18-
__all__ = ["__version__", "Connector"]
18+
__all__ = ["__version__", "Connector", "AsyncConnector"]

0 commit comments

Comments
 (0)