latch-postgres 0.1.20__py3-none-any.whl → 0.2.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- latch_postgres/postgres.py +23 -11
- {latch_postgres-0.1.20.dist-info → latch_postgres-0.2.0.dist-info}/METADATA +1 -1
- latch_postgres-0.2.0.dist-info/RECORD +8 -0
- latch_postgres-0.1.20.dist-info/RECORD +0 -8
- {latch_postgres-0.1.20.dist-info → latch_postgres-0.2.0.dist-info}/WHEEL +0 -0
- {latch_postgres-0.1.20.dist-info → latch_postgres-0.2.0.dist-info}/licenses/LICENSE +0 -0
latch_postgres/postgres.py
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import asyncio
|
|
2
2
|
import functools
|
|
3
3
|
import random
|
|
4
|
-
from collections.abc import AsyncGenerator, Awaitable, Callable, Iterable
|
|
4
|
+
from collections.abc import AsyncGenerator, Awaitable, Callable, Coroutine, Iterable
|
|
5
5
|
from contextlib import asynccontextmanager
|
|
6
6
|
from dataclasses import dataclass
|
|
7
7
|
from datetime import timedelta
|
|
@@ -52,6 +52,8 @@ from typing_extensions import Self
|
|
|
52
52
|
|
|
53
53
|
from latch_postgres.retries import CABackoff
|
|
54
54
|
|
|
55
|
+
YT = TypeVar("YT")
|
|
56
|
+
ST = TypeVar("ST")
|
|
55
57
|
T = TypeVar("T")
|
|
56
58
|
|
|
57
59
|
tracer = get_tracer(__name__)
|
|
@@ -455,10 +457,10 @@ def pg_error_to_dict(x: PGError, *, short: bool = False):
|
|
|
455
457
|
|
|
456
458
|
|
|
457
459
|
def with_conn_retry(
|
|
458
|
-
f: Callable[Concatenate[LatchAsyncConnection[Any], P],
|
|
460
|
+
f: Callable[Concatenate[LatchAsyncConnection[Any], P], Coroutine[YT, ST, T]],
|
|
459
461
|
pool: AsyncConnectionPool,
|
|
460
462
|
db_config: PostgresConnectionConfig,
|
|
461
|
-
) -> Callable[P,
|
|
463
|
+
) -> Callable[P, Coroutine[YT, ST, T]]:
|
|
462
464
|
@functools.wraps(f)
|
|
463
465
|
async def inner(*args: P.args, **kwargs: P.kwargs):
|
|
464
466
|
with tracer.start_as_current_span("database session") as s:
|
|
@@ -585,8 +587,8 @@ def with_conn_retry(
|
|
|
585
587
|
def get_with_conn_retry(
|
|
586
588
|
pool: AsyncConnectionPool, db_config: PostgresConnectionConfig
|
|
587
589
|
) -> Callable[
|
|
588
|
-
[Callable[Concatenate[LatchAsyncConnection[Any], P],
|
|
589
|
-
Callable[P,
|
|
590
|
+
[Callable[Concatenate[LatchAsyncConnection[Any], P], Coroutine[YT, ST, T]]],
|
|
591
|
+
Callable[P, Coroutine[YT, ST, T]],
|
|
590
592
|
]:
|
|
591
593
|
return functools.partial(with_conn_retry, pool=pool, db_config=db_config)
|
|
592
594
|
|
|
@@ -614,9 +616,12 @@ async def reset_conn(
|
|
|
614
616
|
# fixme(maximsmol): use autocommit transactions
|
|
615
617
|
def get_pool(
|
|
616
618
|
config: PostgresConnectionConfig,
|
|
619
|
+
*,
|
|
617
620
|
application_name: str,
|
|
618
621
|
read_only: bool = True,
|
|
619
622
|
isolation_level: IsolationLevel = IsolationLevel.SERIALIZABLE,
|
|
623
|
+
configure: Callable[[AsyncConnection[object]], Awaitable[None]] | None = None,
|
|
624
|
+
reset: Callable[[AsyncConnection[object]], Awaitable[None]] | None = None,
|
|
620
625
|
) -> TracedAsyncConnectionPool:
|
|
621
626
|
conn_str = make_conninfo(
|
|
622
627
|
host=config.host,
|
|
@@ -626,17 +631,24 @@ def get_pool(
|
|
|
626
631
|
password=config.password,
|
|
627
632
|
application_name=application_name,
|
|
628
633
|
)
|
|
634
|
+
|
|
635
|
+
async def configure_impl(x: AsyncConnection[object]) -> None:
|
|
636
|
+
await reset_conn(x, read_only=read_only, isolation_level=isolation_level)
|
|
637
|
+
if configure is not None:
|
|
638
|
+
await configure(x)
|
|
639
|
+
|
|
640
|
+
async def reset_impl(x: AsyncConnection[object]) -> None:
|
|
641
|
+
await reset_conn(x, read_only=read_only, isolation_level=isolation_level)
|
|
642
|
+
if reset is not None:
|
|
643
|
+
await reset(x)
|
|
644
|
+
|
|
629
645
|
return TracedAsyncConnectionPool(
|
|
630
646
|
conn_str,
|
|
631
647
|
min_size=1,
|
|
632
648
|
max_size=config.pool_size,
|
|
633
649
|
timeout=timedelta(seconds=5) / timedelta(seconds=1),
|
|
634
650
|
open=False,
|
|
635
|
-
configure=
|
|
636
|
-
|
|
637
|
-
),
|
|
638
|
-
reset=functools.partial(
|
|
639
|
-
reset_conn, read_only=read_only, isolation_level=isolation_level
|
|
640
|
-
),
|
|
651
|
+
configure=configure_impl,
|
|
652
|
+
reset=reset_impl,
|
|
641
653
|
connection_class=LatchAsyncConnection,
|
|
642
654
|
)
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
latch_postgres/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
2
|
+
latch_postgres/postgres.py,sha256=FKYt0C66xDSOEc63x7z1HuOAylgPcZWGPDNlD2Htbz8,24922
|
|
3
|
+
latch_postgres/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
4
|
+
latch_postgres/retries.py,sha256=r5yH00fd_6EJNYFXAq3dAljJBphScpoeST-_oMvyj8Q,1308
|
|
5
|
+
latch_postgres-0.2.0.dist-info/METADATA,sha256=GFxp7RS1vAyE60rlXgxY9KV0ETD_titcMqK57a-jzWM,606
|
|
6
|
+
latch_postgres-0.2.0.dist-info/WHEEL,sha256=qtCwoSJWgHk21S1Kb4ihdzI2rlJ1ZKaIurTj_ngOhyQ,87
|
|
7
|
+
latch_postgres-0.2.0.dist-info/licenses/LICENSE,sha256=wh3JZu6ITG7ceN-1g404ekupj5JXAw4pnQ_Qr4sJfew,7052
|
|
8
|
+
latch_postgres-0.2.0.dist-info/RECORD,,
|
|
@@ -1,8 +0,0 @@
|
|
|
1
|
-
latch_postgres/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
2
|
-
latch_postgres/postgres.py,sha256=EtrOpQN7VO1_dFFY_Gpivrv9p47mS-YGWh_NCYoy2L4,24441
|
|
3
|
-
latch_postgres/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
4
|
-
latch_postgres/retries.py,sha256=r5yH00fd_6EJNYFXAq3dAljJBphScpoeST-_oMvyj8Q,1308
|
|
5
|
-
latch_postgres-0.1.20.dist-info/METADATA,sha256=B3RxN3paL3WpYQV6XLpThnVmgKZUD5a4Jd0perGWag4,607
|
|
6
|
-
latch_postgres-0.1.20.dist-info/WHEEL,sha256=qtCwoSJWgHk21S1Kb4ihdzI2rlJ1ZKaIurTj_ngOhyQ,87
|
|
7
|
-
latch_postgres-0.1.20.dist-info/licenses/LICENSE,sha256=wh3JZu6ITG7ceN-1g404ekupj5JXAw4pnQ_Qr4sJfew,7052
|
|
8
|
-
latch_postgres-0.1.20.dist-info/RECORD,,
|
|
File without changes
|
|
File without changes
|