Compare commits
2 Commits
cfdfe0eb50
...
master
| Author | SHA1 | Date | |
|---|---|---|---|
| c495b4d097 | |||
| 334b5f5892 |
15
connector.py
15
connector.py
@@ -1,4 +1,4 @@
|
|||||||
from typing import Protocol, runtime_checkable, Callable, Awaitable, Optional
|
from typing import AsyncIterator, Protocol, runtime_checkable, Callable, Awaitable, Optional
|
||||||
import logging
|
import logging
|
||||||
|
|
||||||
from contextvars import ContextVar
|
from contextvars import ContextVar
|
||||||
@@ -19,10 +19,13 @@ ctx_connection: Optional[ContextVar[psq.AsyncConnection]] = ContextVar('connecti
|
|||||||
class Connector:
|
class Connector:
|
||||||
cursor_factory = AsyncLoggingCursor
|
cursor_factory = AsyncLoggingCursor
|
||||||
|
|
||||||
def __init__(self, conn_args):
|
def __init__(self, conn_args, pool_min_size=1, pool_max_size=4):
|
||||||
self._conn_args = conn_args
|
self._conn_args = conn_args
|
||||||
self._conn_kwargs = dict(autocommit=True, row_factory=row_factory, cursor_factory=self.cursor_factory)
|
self._conn_kwargs = dict(autocommit=True, row_factory=row_factory, cursor_factory=self.cursor_factory)
|
||||||
|
|
||||||
|
self.pool_min_size = pool_min_size
|
||||||
|
self.pool_max_size = pool_max_size
|
||||||
|
|
||||||
self.pool = self.make_pool()
|
self.pool = self.make_pool()
|
||||||
|
|
||||||
self.conn_hooks = []
|
self.conn_hooks = []
|
||||||
@@ -47,8 +50,8 @@ class Connector:
|
|||||||
return AsyncConnectionPool(
|
return AsyncConnectionPool(
|
||||||
self._conn_args,
|
self._conn_args,
|
||||||
open=False,
|
open=False,
|
||||||
min_size=1,
|
min_size=self.pool_min_size,
|
||||||
max_size=4,
|
max_size=self.pool_max_size,
|
||||||
configure=self._setup_connection,
|
configure=self._setup_connection,
|
||||||
kwargs=self._conn_kwargs
|
kwargs=self._conn_kwargs
|
||||||
)
|
)
|
||||||
@@ -64,7 +67,7 @@ class Connector:
|
|||||||
old_pool = self.pool
|
old_pool = self.pool
|
||||||
self.pool = self.make_pool()
|
self.pool = self.make_pool()
|
||||||
await self.pool.open()
|
await self.pool.open()
|
||||||
logger.info(f"Old pool statistics: {self.pool.get_stats()}")
|
logger.info(f"Old pool statistics: {old_pool.get_stats()}")
|
||||||
await old_pool.close()
|
await old_pool.close()
|
||||||
logger.info("Pool refresh complete.")
|
logger.info("Pool refresh complete.")
|
||||||
|
|
||||||
@@ -95,7 +98,7 @@ class Connector:
|
|||||||
await self.pool.close()
|
await self.pool.close()
|
||||||
|
|
||||||
@asynccontextmanager
|
@asynccontextmanager
|
||||||
async def connection(self) -> psq.AsyncConnection:
|
async def connection(self) -> AsyncIterator[psq.AsyncConnection]:
|
||||||
"""
|
"""
|
||||||
Asynchronous context manager to get and manage a connection.
|
Asynchronous context manager to get and manage a connection.
|
||||||
|
|
||||||
|
|||||||
13
queries.py
13
queries.py
@@ -444,11 +444,14 @@ class Insert(ExtraMixin, TableQuery[QueryResult]):
|
|||||||
|
|
||||||
# TODO: Check efficiency of inserting multiple values like this
|
# TODO: Check efficiency of inserting multiple values like this
|
||||||
# Also implement a Copy query
|
# Also implement a Copy query
|
||||||
base = sql.SQL("INSERT INTO {table} ({columns}) VALUES {values_str}").format(
|
if self._columns:
|
||||||
table=self.tableid,
|
base = sql.SQL("INSERT INTO {table} ({columns}) VALUES {values_str}").format(
|
||||||
columns=columns,
|
table=self.tableid,
|
||||||
values_str=values_str
|
columns=columns,
|
||||||
)
|
values_str=values_str
|
||||||
|
)
|
||||||
|
else:
|
||||||
|
base = sql.SQL("INSERT INTO {table} DEFAULT VALUES").format(table=self.tableid)
|
||||||
|
|
||||||
sections = [
|
sections = [
|
||||||
RawExpr(base, tuple(chain(*self._values))),
|
RawExpr(base, tuple(chain(*self._values))),
|
||||||
|
|||||||
Reference in New Issue
Block a user