MagicStack / MagicStack/asyncpg
Feature Request: Retry on failed acquires (timeouterror/connectionerror)
还没有人认领这个 Issue。
- 主要语言
- Python
- 星标
- 8.1k
- 派生
- 468
- PR 合并指标
- 30 天内没有已合并 PR
描述
* **asyncpg version**: 0.21.0
* **PostgreSQL version**: any
* **Do you use a PostgreSQL SaaS? If so, which? Can you reproduce
the issue with a local PostgreSQL install?**: -- dont use it
* **Python version**: any
* **Platform**: any
* **Do you use pgbouncer?**: no
* **Did you install asyncpg with pip?**: yes
* **If you built asyncpg locally, which version of Cython did you use?**: not built locally
* **Can the issue be reproduced under both asyncio and
[uvloop](https://github.com/magicstack/uvloop)?**: yes
Would you accept a contribution to asyncpg's Pool in which we can retry failed acquires (timeouterrors/connectionresetbypeer/...) which are caused by network issues?
The default behaviour wouldn't retry, as it's happening right now.
This is useful for data processing pipelines with multiple acquires in it's flow (distinct dbs): Being able to configure acquire_max_retries when creating the pool would help keeping the code simpler and not have to wrap all our 'acquires' with retries. The retry decorator logic on acquire can become non-trivial because of the contextmanager auto-releasing the connection after successful usage OR other errors (unrelated to asyncpg) that happen after acquiring a connection+exiting the context (this caused some pain for us).
If you are willing to accept this feature, I'm happy to implement it on my own and submit a PR.
Just to give you an idea, this is what we had to implement on our project in order to have pool acquires with retries:
```
async def retry(coro, exceptions=(Exception,), max_retries=3, retry_delay_seconds=0):
for attempt in range(max_retries):
try:
return await coro()
except exceptions:
logger.exception(f"{coro} attempt failed. Attempt {attempt + 1}/{max_retries}")
if attempt + 1 == max_retries:
raise
await asyncio.sleep(retry_delay_seconds)
class PoolWithRetries:
def __init__(self, pool: asyncpg.pool.Pool, max_retries=3, retry_delay_seconds=0):
self.pool = pool
self.max_retries = max_retries
self.retry_delay_seconds = retry_delay_seconds
@asynccontextmanager
async def acquire(self, *args, **kwargs):
# Reimplementation of PoolAcquireContext with retries
# Only supports `async with this.acquire() as con` syntax
con = await retry(
partial(self.pool.acquire, *args, **kwargs),
exceptions=(asyncio.TimeoutError, ConnectionError),
max_retries=self.max_retries,
retry_delay_seconds=self.retry_delay_seconds,
)
try:
yield con
finally:
await self.pool.release(con)
async def release(self, *args, **kwargs):
await self.pool.release(*args, **kwargs)
async def close(self):
await self.pool.close()
```
The implementation becomes much simpler if we can do it inside asyncpg.
贡献指南
这个仓库没有索引到贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
调研方向
从 asyncpg Pool.acquire 入口点和 issue 中描述的 PoolAcquireContext 行为开始。定义可配置的 acquire 重试应如何处理超时和连接错误,同时不重试无关错误,也不干扰 context manager 的释放;然后为已接受的重试行为添加覆盖,再将该功能视为完成。
由索引模型根据 Issue 内容生成。
评估
- 技术栈
- postgresql, python
- 领域
- backend-api-design, database
- Issue 类型
- 功能
- 难度
- 5/5
- 预计耗时
- 一周以上
- 活跃度
- 停滞
- 描述清晰度
- 基本清楚
- 新手友好度
- 25/100