chore: 初始化仓库,导入项目基线代码
Academy v0.8.3 完整代码基线:Go 后端(internal/cmd/cli/mcp-server/docreader)、 Vue 前端(frontend)、数据库迁移(migrations)、部署编排(docker/helm/deploy)与文档。 今日开发的培训推送管理改动随后单独提交。
This commit is contained in:
commit
7ec21f35b0
5767 files changed
+1436788
No files matched your search
@@ -0,0 +1,45 @@
|
||||
package common
|
||||
|
||||
import (
|
||||
"context"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"academy/internal/logger"
|
||||
)
|
||||
|
||||
const defaultMaxDeadlockRetries = 3
|
||||
|
||||
// WithDeadlockRetry retries fn when MySQL returns Error 1213 (deadlock).
|
||||
// Retries up to 3 times with exponential backoff (50ms, 100ms, 200ms).
|
||||
// Safe for all DB drivers — non-deadlock errors are returned immediately.
|
||||
func WithDeadlockRetry(ctx context.Context, fn func() error) error {
|
||||
var err error
|
||||
for attempt := 0; attempt <= defaultMaxDeadlockRetries; attempt++ {
|
||||
err = fn()
|
||||
if err == nil || !IsDeadlockError(err) {
|
||||
return err
|
||||
}
|
||||
if attempt < defaultMaxDeadlockRetries {
|
||||
wait := time.Duration(50<<attempt) * time.Millisecond
|
||||
logger.Warnf(ctx, "[DeadlockRetry] attempt %d/%d failed with deadlock, retrying in %v: %v",
|
||||
attempt+1, defaultMaxDeadlockRetries, wait, err)
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
return ctx.Err()
|
||||
case <-time.After(wait):
|
||||
}
|
||||
}
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
// IsDeadlockError checks whether err is a MySQL deadlock (Error 1213).
|
||||
func IsDeadlockError(err error) bool {
|
||||
if err == nil {
|
||||
return false
|
||||
}
|
||||
msg := err.Error()
|
||||
return strings.Contains(msg, "Error 1213") ||
|
||||
strings.Contains(msg, "Deadlock found when trying to get lock")
|
||||
}
|
||||
Reference in new issue
Block a user