perf(store): 消除同步复制下的鉴权与心跳锁阻塞

将 Worker 心跳与容量分配事务设为本地异步提交,避免同步副本延迟期间长期持有全局分配锁。API Key 使用时间改为异步提交并按分钟合并,消除高并发请求对同一热行的锁排队。业务任务、钱包、结算、并发租约等关键数据仍保持同步复制。验证通过完整 Go 测试、go vet、竞态测试、迁移安全检查及 PostgreSQL 18 集成测试。
This commit is contained in:
2026-07-29 23:25:54 +08:00
parent 91451b3c86
commit 98820378b7
4 changed files with 59 additions and 2 deletions
@@ -46,6 +46,12 @@ func (s *Store) RegisterWorkerInstance(ctx context.Context, input WorkerRegistra
return WorkerAllocation{}, err
}
defer tx.Rollback(ctx)
// Worker heartbeats and allocations are ephemeral coordination state that is
// refreshed every few seconds. Do not hold the global allocation lock while
// waiting for a synchronous replica to acknowledge the commit.
if _, err := tx.Exec(ctx, `SELECT set_config('synchronous_commit', 'off', true)`); err != nil {
return WorkerAllocation{}, err
}
if _, err := tx.Exec(ctx, `SELECT pg_advisory_xact_lock(hashtextextended('gateway-worker-capacity', 0))`); err != nil {
return WorkerAllocation{}, err
}