fix(platform): 防止绑定任务压垮连接池和事件循环

This commit is contained in:
yml
2026-11-21 15:22:46 +08:00
parent d199e04d27
commit e1d979a296
13 changed files with 336 additions and 46 deletions
@@ -429,6 +429,10 @@
"20261121_0107_repair_douyu_active_binding_key.py": {
"revision": "20261121_0107",
"sha256": "29539b5fab9c6d8eaf7219b7d19376dfa6032d8b6cf02b76fc2a843c09891302"
},
"20261121_0108_account_pool_query_indexes.py": {
"revision": "20261121_0108",
"sha256": "b45a7f0c563e43e6516cf00ad12ce7a5fb78bb7bde6b3f8de02cb888ad761666"
}
}
}
@@ -0,0 +1,63 @@
"""为账号池自动选号增加组合索引。
自动选号会反复执行 accounts 候选扫描,并用 NOT EXISTS 排除活动协作单。
没有可用的组合索引时,生产库每次都要扫描约四万行账号再排序,造成 MySQL CPU
和请求延迟一起放大。
"""
from __future__ import annotations
import sqlalchemy as sa
from alembic import op
revision: str = "20261121_0108"
down_revision: str | None = "20261121_0107"
branch_labels: str | None = None
depends_on: str | None = None
ACCOUNT_INDEX = "ix_accounts_pool_candidate"
COLLAB_INDEX = "ix_douyu_external_orders_active_account"
def _index_exists(table: str, name: str) -> bool:
conn = op.get_bind()
return bool(
conn.execute(
sa.text(
"SELECT COUNT(*) FROM information_schema.statistics "
"WHERE table_schema = DATABASE() "
"AND table_name = :table AND index_name = :name"
),
{"table": table, "name": name},
).scalar()
)
def upgrade() -> None:
if not _index_exists("accounts", ACCOUNT_INDEX):
op.create_index(
ACCOUNT_INDEX,
"accounts",
[
"auto_managed",
"status",
"role_change_available_at",
"ck_check_status",
"points_expire_at",
"points",
"id",
],
)
if not _index_exists("douyu_external_orders", COLLAB_INDEX):
op.create_index(
COLLAB_INDEX,
"douyu_external_orders",
["account_id", "archived_at", "status"],
)
def downgrade() -> None:
if _index_exists("douyu_external_orders", COLLAB_INDEX):
op.drop_index(COLLAB_INDEX, table_name="douyu_external_orders")
if _index_exists("accounts", ACCOUNT_INDEX):
op.drop_index(ACCOUNT_INDEX, table_name="accounts")