Skip to content

Commit

Permalink
Merge branch 'master' of github.com:aiokitchen/hasql into feature/met…
Browse files Browse the repository at this point in the history
…rics
  • Loading branch information
Pavkazzz committed Oct 8, 2023
2 parents 7864b01 + f73282a commit aedadf9
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 10 deletions.
51 changes: 44 additions & 7 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ Features
* autodetection of hosts role changes, in case replica
host will be promoted to master
* different policies for load balancing
* support for ``asyncpg``, ``psycopg3`` and ``aiopg``
* support for ``asyncpg``, ``psycopg3``, ``aiopg``, ``sqlalchemy`` and ``asyncpgsa``


Usage
Expand Down Expand Up @@ -94,7 +94,7 @@ Code example using ``aiopg``:
pool = PoolManager(multihost_dsn)
# Waiting for 1 master and 1 replica will be available
await pool_manager.ready(masters_count=1, replicas_count=1)
await pool.ready(masters_count=1, replicas_count=1)
return pool
Code example using ``aiopg.sa``:
Expand All @@ -115,7 +115,7 @@ Code example using ``aiopg.sa``:
pool = PoolManager(multihost_dsn)
# Waiting for 1 master and 1 replica will be available
await pool_manager.ready(masters_count=1, replicas_count=1)
await pool.ready(masters_count=1, replicas_count=1)
return pool
For ``asyncpg``
Expand All @@ -139,9 +139,46 @@ For ``asyncpg``
pool = PoolManager(multihost_dsn)
# Waiting for 1 master and 1 replica will be available
await pool_manager.ready(masters_count=1, replicas_count=1)
await pool.ready(masters_count=1, replicas_count=1)
return pool
For ``sqlalchemy``
~~~~~~~~~~~~~~~~~~

**sqlalchemy[asyncio] & asyncpg** must be installed as requirements

.. code-block:: python
from hasql.asyncsqlalchemy import PoolManager
hosts = ",".join([
"master-host:5432",
"replica-host-1:5432",
"replica-host-2:5432",
])
multihost_dsn = f"postgresql://user:password@{hosts}/dbname"
async def create_pool(dsn) -> PoolManager:
pool = PoolManager(
multihost_dsn,
# Use master for acquire_replica, if no replicas available
fallback_master=True,
# You can pass pool-specific options
pool_factory_kwargs=dict(
pool_size=10,
max_overflow=5
)
)
# Waiting for 1 master and 1 replica will be available
await pool.ready(masters_count=1, replicas_count=1)
return pool
For ``asyncpgsa``
~~~~~~~~~~~~~~~~~

Expand All @@ -164,8 +201,8 @@ For ``asyncpgsa``
# Waiting for 1 master and 1 replica will be available
await asyncio.gather(
pool_manager.wait_masters_ready(1),
pool_manager.wait_replicas_ready(1)
pool.wait_masters_ready(1),
pool.wait_replicas_ready(1)
)
return pool
Expand All @@ -191,7 +228,7 @@ For ``psycopg3``
pool = PoolManager(multihost_dsn)
# Waiting for 1 master and 1 replica will be available
await pool_manager.ready(masters_count=1, replicas_count=1)
await pool.ready(masters_count=1, replicas_count=1)
return pool
Expand Down
2 changes: 1 addition & 1 deletion hasql/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
__version_info__ = (0, 5, 8)
__version_info__ = (0, 5, 11)
__version__ = ".".join(map(str, __version_info__))

package_info = (
Expand Down
1 change: 1 addition & 0 deletions hasql/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -542,6 +542,7 @@ async def _wait_creating_pool(self, dsn: Dsn):
dsn.with_(password="******"),
exc_info=True,
)
await asyncio.sleep(self._refresh_delay)

async def _periodic_pool_check(self, pool, dsn: Dsn, sys_connection):
while not self._closing:
Expand Down
Empty file added hasql/py.typed
Empty file.
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
"Programming Language :: Python",
],
packages=find_packages(exclude=["tests", "example"]),
package_data={'hasql': ['py.typed']},
install_requires=[],
extras_require={
"aiopg": [
Expand Down
6 changes: 4 additions & 2 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
envlist = lint,mypy,py3{7,8,9,10,11,12}{,-uvloop}

[testenv]
passenv = COVERALLS_* FORCE_COLOR PG_DSN
passenv = COVERALLS_*, FORCE_COLOR, PG_DSN
allowlist_externals = coveralls

extras =
develop
Expand All @@ -16,6 +17,7 @@ commands=
- coveralls

[testenv:lint]
allowlist_externals = pylama
deps =
pyflakes==2.4.0
pylama
Expand All @@ -32,7 +34,7 @@ commands =
python setup.py checkdocs

[testenv:mypy]

allowlist_externals = mypy
basepython = python3.10
usedevelop = true

Expand Down

0 comments on commit aedadf9

Please sign in to comment.