mirror of
https://github.com/Rain-kl/OpenFlare.git
synced 2026-09-29 14:06:36 +08:00
refactor(idgen): centralize negative ID handling via panic
Restore NextUint64ID() to uint64-only API so callers need no error checks. Retry logic stays in idgen; after 3 negative values it panics instead of returning 0, preventing silent NULL primary keys.
This commit is contained in:
@@ -386,14 +386,8 @@ func CreateUser(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
nextID, err := idgen.NextUint64ID()
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, response.Err(createUserFailed))
|
||||
return
|
||||
}
|
||||
|
||||
newUser := model.User{
|
||||
ID: nextID,
|
||||
ID: idgen.NextUint64ID(),
|
||||
Username: req.Username,
|
||||
Nickname: req.Nickname,
|
||||
Email: req.Email,
|
||||
|
||||
@@ -15,7 +15,6 @@ import (
|
||||
"github.com/Rain-kl/Wavelet/internal/db/idgen"
|
||||
"github.com/Rain-kl/Wavelet/internal/model"
|
||||
"github.com/Rain-kl/Wavelet/internal/util"
|
||||
"github.com/Rain-kl/Wavelet/pkg/logger"
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
@@ -70,14 +69,8 @@ func RiskControlMiddleware() gin.HandlerFunc {
|
||||
status = maxHTTPStatus
|
||||
}
|
||||
|
||||
logID, err := idgen.NextUint64ID()
|
||||
if err != nil {
|
||||
logger.ErrorF(c.Request.Context(), "[RiskControl] access log ID generation failed: %v", err)
|
||||
return
|
||||
}
|
||||
|
||||
logItem := &UserAccessLog{
|
||||
ID: logID,
|
||||
ID: idgen.NextUint64ID(),
|
||||
UserID: userObj.ID, // 直接从 Context 获取已登录用户ID,避免数据库查询
|
||||
Path: c.Request.URL.Path,
|
||||
Method: c.Request.Method,
|
||||
|
||||
@@ -140,11 +140,7 @@ func UploadFile(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
id, err := idgen.NextUint64ID()
|
||||
if err != nil {
|
||||
c.JSON(http.StatusOK, response.Err(ErrSaveUploadRecordFailed))
|
||||
return
|
||||
}
|
||||
id := idgen.NextUint64ID()
|
||||
subPath := fmt.Sprintf("uploads/%s/%d.%s", time.Now().Format("2006/01/02"), id, ext)
|
||||
|
||||
// 8. 写入当前活动存储驱动。
|
||||
@@ -376,11 +372,7 @@ func tryInstantUpload(ctx context.Context, c *gin.Context, currUser *model.User,
|
||||
return true, nil
|
||||
}
|
||||
|
||||
id, err := idgen.NextUint64ID()
|
||||
if err != nil {
|
||||
c.JSON(http.StatusOK, response.Err(ErrSaveUploadRecordFailed))
|
||||
return true, err
|
||||
}
|
||||
id := idgen.NextUint64ID()
|
||||
newUpload := model.Upload{
|
||||
ID: id,
|
||||
UserID: currUser.ID,
|
||||
|
||||
@@ -228,14 +228,8 @@ func Register(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
nextID, err := idgen.NextUint64ID()
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, response.Err(err.Error()))
|
||||
return
|
||||
}
|
||||
|
||||
user := model.User{
|
||||
ID: nextID,
|
||||
ID: idgen.NextUint64ID(),
|
||||
Username: req.Username,
|
||||
Nickname: req.Nickname,
|
||||
Email: req.Email,
|
||||
|
||||
@@ -6,7 +6,6 @@
|
||||
package idgen
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"log"
|
||||
|
||||
@@ -19,9 +18,6 @@ const epoch int64 = 1764547200000
|
||||
|
||||
const maxNegativeIDRetries = 3
|
||||
|
||||
// ErrNegativeSnowflakeID 表示 Snowflake 在重试后仍生成负值 ID。
|
||||
var ErrNegativeSnowflakeID = errors.New("snowflake generated negative ID")
|
||||
|
||||
var node *snowflake.Node
|
||||
|
||||
func init() {
|
||||
@@ -37,14 +33,14 @@ func init() {
|
||||
}
|
||||
|
||||
// NextUint64ID 生成下一个分布式唯一 ID。
|
||||
// 理论上不应出现负值;若出现则最多重试 maxNegativeIDRetries 次,仍失败则返回错误。
|
||||
func NextUint64ID() (uint64, error) {
|
||||
// 理论上不应出现负值;若出现则最多重试 maxNegativeIDRetries 次,仍失败则 panic。
|
||||
func NextUint64ID() uint64 {
|
||||
for attempt := 1; attempt <= maxNegativeIDRetries; attempt++ {
|
||||
id := node.Generate().Int64()
|
||||
if id >= 0 {
|
||||
return uint64(id), nil
|
||||
return uint64(id)
|
||||
}
|
||||
log.Printf("[Snowflake] generated negative ID: %d (attempt %d/%d)", id, attempt, maxNegativeIDRetries)
|
||||
}
|
||||
return 0, fmt.Errorf("%w: failed after %d attempts", ErrNegativeSnowflakeID, maxNegativeIDRetries)
|
||||
panic(fmt.Sprintf("[Snowflake] generated negative ID after %d attempts", maxNegativeIDRetries))
|
||||
}
|
||||
@@ -7,11 +7,9 @@ import (
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func TestNextUint64ID(t *testing.T) {
|
||||
id, err := NextUint64ID()
|
||||
require.NoError(t, err)
|
||||
id := NextUint64ID()
|
||||
assert.NotZero(t, id)
|
||||
}
|
||||
@@ -5,7 +5,6 @@ package model
|
||||
|
||||
const (
|
||||
errRegistrationDisabled = "注册已关闭"
|
||||
errInvalidUserID = "用户 ID 生成失败"
|
||||
errDatabaseNotInitialized = "database not initialized"
|
||||
errUsernameExists = "用户名已存在"
|
||||
errEmailAlreadyBound = "该邮箱已被其他账号绑定"
|
||||
|
||||
@@ -60,11 +60,7 @@ func (TaskExecution) TableName() string {
|
||||
|
||||
// CreateTaskExecution 创建任务执行记录
|
||||
func CreateTaskExecution(ctx context.Context, execution *TaskExecution) error {
|
||||
id, err := idgen.NextUint64ID()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
execution.ID = id
|
||||
execution.ID = idgen.NextUint64ID()
|
||||
return db.DB(ctx).Create(execution).Error
|
||||
}
|
||||
|
||||
|
||||
@@ -134,11 +134,7 @@ func (u *User) assignIDIfMissing() error {
|
||||
if u.ID != 0 {
|
||||
return nil
|
||||
}
|
||||
id, err := idgen.NextUint64ID()
|
||||
if err != nil {
|
||||
return errors.New(errInvalidUserID)
|
||||
}
|
||||
u.ID = id
|
||||
u.ID = idgen.NextUint64ID()
|
||||
return nil
|
||||
}
|
||||
|
||||
|
||||
@@ -115,10 +115,7 @@ func DispatchTask(ctx context.Context, taskType string, payload []byte, triggere
|
||||
}
|
||||
|
||||
// 生成唯一的 TaskID
|
||||
taskID, err := generateTaskID(taskType, triggeredBy)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
taskID := generateTaskID(taskType, triggeredBy)
|
||||
|
||||
// 创建任务执行记录
|
||||
execution := &model.TaskExecution{
|
||||
@@ -456,12 +453,8 @@ func handleSuccessfulTask(ctx context.Context, execution *model.TaskExecution, t
|
||||
}
|
||||
|
||||
// generateTaskID 生成任务 ID
|
||||
func generateTaskID(taskType string, triggeredBy string) (string, error) {
|
||||
uniqueID, err := idgen.NextUint64ID()
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
return fmt.Sprintf("%s_%s_%d", triggeredBy, taskType, uniqueID), nil
|
||||
func generateTaskID(taskType string, triggeredBy string) string {
|
||||
return fmt.Sprintf("%s_%s_%d", triggeredBy, taskType, idgen.NextUint64ID())
|
||||
}
|
||||
|
||||
// generateRetryTaskID 生成重试任务 ID
|
||||
|
||||
@@ -387,10 +387,8 @@ func TestRetryTaskNonExistent(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestGenerateTaskID(t *testing.T) {
|
||||
id1, err := generateTaskID("test_type", "manual")
|
||||
require.NoError(t, err)
|
||||
id2, err := generateTaskID("test_type", "manual")
|
||||
require.NoError(t, err)
|
||||
id1 := generateTaskID("test_type", "manual")
|
||||
id2 := generateTaskID("test_type", "manual")
|
||||
|
||||
// 两个 ID 应不同(包含 Snowflake ID)
|
||||
assert.NotEqual(t, id1, id2)
|
||||
|
||||
Reference in New Issue
Block a user