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
+13 -1
View File
@@ -1709,7 +1709,19 @@ WHERE k.key_prefix = $1
if bcrypt.CompareHashAndPassword([]byte(candidate.hash), []byte(secret)) != nil {
continue
}
if _, err := database.Exec(ctx, `UPDATE gateway_api_keys SET last_used_at = now(), updated_at = now() WHERE id = $1::uuid`, candidate.apiKeyID); err != nil {
// API key usage timestamps are telemetry, not authentication state. Keep
// their writes off the synchronous-replication critical path and coalesce
// hot-key updates so concurrent requests do not contend on the same row.
if _, err := database.Exec(ctx, `
WITH commit_policy AS (
SELECT set_config('synchronous_commit', 'off', true)
)
UPDATE gateway_api_keys
SET last_used_at = now(),
updated_at = now()
FROM commit_policy
WHERE id = $1::uuid
AND (last_used_at IS NULL OR last_used_at < now() - interval '1 minute')`, candidate.apiKeyID); err != nil {
return nil, err
}
return &auth.User{