MagicStack / MagicStack/asyncpg

Feature Request: Middleware

未关闭
#152 3 条评论 5 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

enhancement
主要语言
Python
星标
8.1k
派生
468
PR 合并指标
30 天内没有已合并 PR

描述

As discussed in #128, it might be a cool idea to have a "middleware" layer in asyncpg where people can register their own code to be ran pre or post query.

One usecase is asyncpgsa, which is a project to allow sqlalchemy objects to be passed to asyncpg connection methods. If there was a middleware layer, asyncpgsa could simply be a middleware that would run right before asyncpg prepared the query to convert the sqlalchemy object into a string. This would make asyncpgsa stupid simple, and just be a conversion layer, allowing it to get out of the way, and let asyncpg be the source of truth for everything else.

Another use case is metrics. My org is starting to track metrics per endpoint and per database query. If asyncpg had middleware support, we could simply inject a middleware that tracked the query and the time it took, and send that information into our metric platform. This could be done today by wrapping asyncpg and adding a method around it that tracks the metrics. It would be amazing if instead asyncpg could be used directly, and the middleware handles that portion for me.

Middlewares could potentially be "added" when the pool is created. (they can only exist on a pool maybe?)

Anyways, I am happy to try to implement this if you would like, but first wanted to get a general discussion going on the topic.

贡献指南

这个仓库没有索引到贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

调研方向

先阅读 issue #128,然后检查 pool 创建和连接方法,以确定可以在哪里注册和调用 pre- 和 post-query middleware。比较 asyncpgsa 转换和 metrics 的使用场景,并在实现之前定义所需的 API、范围、顺序、async 行为和测试。

由索引模型根据 Issue 内容生成。

评估

技术栈
postgresql, python
领域
backend, databases
Issue 类型
功能
难度
5/5
预计耗时
一周以上
活跃度
停滞
描述清晰度
需要澄清
新手友好度
25/100

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。