Version: 0.9.66.dev.260504
后端: 1. 阶段 2 user/auth 服务边界落地,新增 `cmd/userauth` go-zero zrpc 服务、`services/userauth` 核心实现、gateway user API/zrpc client 与 shared contracts/ports,迁移注册、登录、刷新 token、登出、JWT、黑名单和 token 额度治理 2. gateway 与启动装配切流,`cmd/all` 只保留边缘路由、鉴权和轻量组合,通过 userauth zrpc 访问核心用户能力;拆分 MySQL/Redis 初始化与 AutoMigrate 边界,`userauth` 自迁 `users` 和 token 记账幂等表,`all` 不再迁用户表 3. 清退 Gin 单体旧 user/auth DAO、model、service、router、middleware 和 JWT handler,并同步调整 agent/schedule/cache/outbox 相关调用依赖 4. 补齐 refresh token 防并发重放、MySQL 幂等 token 记账、额度 `>=` 拦截和 RPC 错误映射,避免重复记账与内部错误透出 文档: 1. 新增《学习计划论坛与Token商店PRD》
This commit is contained in:
@@ -13,12 +13,11 @@ import (
|
||||
"gorm.io/gorm/clause"
|
||||
)
|
||||
|
||||
// Repository 是 outbox 状态机仓储。
|
||||
//
|
||||
// Repository 是 outbox 状态仓储。
|
||||
// 职责边界:
|
||||
// 1. 只负责 outbox 状态流转与通用事务编排;
|
||||
// 2. 不负责聊天、任务、通知等具体业务语义;
|
||||
// 3. 同一仓储实例只面向一个服务级 outbox 目录,避免把共享表当成终态。
|
||||
// 3. 同一仓储实例只面向一个服务级 outbox 目录。
|
||||
type Repository struct {
|
||||
db *gorm.DB
|
||||
route ServiceRoute
|
||||
@@ -28,7 +27,7 @@ func NewRepository(db *gorm.DB) *Repository {
|
||||
return &Repository{db: db}
|
||||
}
|
||||
|
||||
// WithTx 用外部事务句柄构造同事务仓储实例。
|
||||
// WithTx 使用外部事务句柄构造同事务仓储实例。
|
||||
func (d *Repository) WithTx(tx *gorm.DB) *Repository {
|
||||
if d == nil {
|
||||
return &Repository{db: tx}
|
||||
@@ -36,11 +35,9 @@ func (d *Repository) WithTx(tx *gorm.DB) *Repository {
|
||||
return &Repository{db: tx, route: d.route}
|
||||
}
|
||||
|
||||
// WithRoute 用指定服务目录构造服务级仓储。
|
||||
//
|
||||
// 职责边界:
|
||||
// 1. 只切换 outbox 物理目录,不改变事务句柄;
|
||||
// 2. 适合多个 service engine 共享同一 DB 连接;
|
||||
// WithRoute 使用指定服务路由构造仓储实例。
|
||||
// 1. 只切换 outbox 物理目录,不改变业务事务语义;
|
||||
// 2. 适合多个 service engine 共用同一 DB 连接时分别绑定各自 route;
|
||||
// 3. 保留 route 的 table/topic/group,避免回落到共享 topic。
|
||||
func (d *Repository) WithRoute(route ServiceRoute) *Repository {
|
||||
route = normalizeServiceRoute(route)
|
||||
@@ -50,11 +47,9 @@ func (d *Repository) WithRoute(route ServiceRoute) *Repository {
|
||||
return &Repository{db: d.db, route: route}
|
||||
}
|
||||
|
||||
// CreateMessage 把事件写入 outbox。
|
||||
//
|
||||
// 职责边界:
|
||||
// 1. 只接受 eventType、messageKey、payload 和 maxRetry,不再允许业务侧显式传 topic;
|
||||
// 2. table/topic/group 统一由 eventType -> service -> route 解析,确保服务级路由是唯一入口;
|
||||
// CreateMessage 将事件写入 outbox。
|
||||
// 1. 只接收 eventType、messageKey、payload 和 maxRetry,不再允许业务侧显式传 topic;
|
||||
// 2. table/topic/group 统一由 eventType -> service -> route 解析,保证服务级路由是唯一入口;
|
||||
// 3. eventType 未注册时直接返回 error,避免消息静默落到默认表或默认 topic。
|
||||
func (d *Repository) CreateMessage(ctx context.Context, eventType string, messageKey string, payload any, maxRetry int) (int64, error) {
|
||||
if d == nil || d.db == nil {
|
||||
@@ -109,8 +104,6 @@ func (d *Repository) GetByID(ctx context.Context, id int64) (*model.AgentOutboxM
|
||||
}
|
||||
|
||||
// ListDueMessages 拉取到期可投递消息。
|
||||
//
|
||||
// 说明:
|
||||
// 1. serviceName 为空时保持当前仓储目录内的扫描语义;
|
||||
// 2. serviceName 非空时只扫描对应服务的消息;
|
||||
// 3. 这样既能支持单服务 relay,也能支持后续多服务 relay。
|
||||
@@ -134,7 +127,7 @@ func (d *Repository) ListDueMessages(ctx context.Context, serviceName string, li
|
||||
return messages, nil
|
||||
}
|
||||
|
||||
// MarkPublished 标记消息已成功投递到 Kafka。
|
||||
// MarkPublished 标记消息已经成功投递到 Kafka。
|
||||
func (d *Repository) MarkPublished(ctx context.Context, id int64) error {
|
||||
now := time.Now()
|
||||
updates := map[string]interface{}{
|
||||
@@ -150,7 +143,20 @@ func (d *Repository) MarkPublished(ctx context.Context, id int64) error {
|
||||
return result.Error
|
||||
}
|
||||
|
||||
// MarkDead 把消息标记为死信。
|
||||
// MarkConsumed 标记消息已经在处理侧成功完成。
|
||||
func (d *Repository) MarkConsumed(ctx context.Context, id int64) error {
|
||||
now := time.Now()
|
||||
updates := map[string]interface{}{
|
||||
"status": model.OutboxStatusConsumed,
|
||||
"consumed_at": &now,
|
||||
"last_error": nil,
|
||||
"next_retry_at": nil,
|
||||
"updated_at": now,
|
||||
}
|
||||
return d.scopedDB(ctx).Model(&model.AgentOutboxMessage{}).Where("id = ?", id).Updates(updates).Error
|
||||
}
|
||||
|
||||
// MarkDead 将消息标记为死信。
|
||||
func (d *Repository) MarkDead(ctx context.Context, id int64, reason string) error {
|
||||
now := time.Now()
|
||||
lastErr := truncateError(reason)
|
||||
@@ -164,12 +170,10 @@ func (d *Repository) MarkDead(ctx context.Context, id int64, reason string) erro
|
||||
}
|
||||
|
||||
// MarkFailedForRetry 记录一次可重试失败并推进重试窗口。
|
||||
//
|
||||
// 步骤:
|
||||
// 1. 行级锁读取当前消息状态;
|
||||
// 2. 已进入 consumed/dead 时幂等短路;
|
||||
// 3. retry_count+1,并根据最大次数决定继续 pending 还是转 dead;
|
||||
// 4. 写回 last_error 和 next_retry_at,交给下一轮扫描继续投递。
|
||||
// 2. consumed/dead 状态直接短路;
|
||||
// 3. retry_count + 1,并根据最大次数决定继续 pending 还是转 dead;
|
||||
// 4. 写回 last_error 与 next_retry_at,交给下一轮扫描继续投递。
|
||||
func (d *Repository) MarkFailedForRetry(ctx context.Context, id int64, reason string) error {
|
||||
return d.db.WithContext(ctx).Transaction(func(tx *gorm.DB) error {
|
||||
var msg model.AgentOutboxMessage
|
||||
@@ -207,11 +211,9 @@ func (d *Repository) MarkFailedForRetry(ctx context.Context, id int64, reason st
|
||||
}
|
||||
|
||||
// ConsumeAndMarkConsumed 是通用“消费成功事务入口”。
|
||||
//
|
||||
// 步骤:
|
||||
// 1. 事务内锁定 outbox 记录;
|
||||
// 2. consumed/dead 状态幂等返回;
|
||||
// 3. 执行业务回调 fn(tx),让业务落库和 outbox 状态共用同一事务;
|
||||
// 1. 在事务内锁定 outbox 记录;
|
||||
// 2. consumed/dead 状态直接返回;
|
||||
// 3. 执行业务回调 fn(tx),让业务落库和 outbox 状态共享同一事务;
|
||||
// 4. 业务成功后统一标记 consumed。
|
||||
func (d *Repository) ConsumeAndMarkConsumed(ctx context.Context, outboxID int64, fn func(tx *gorm.DB) error) error {
|
||||
return d.db.WithContext(ctx).Transaction(func(tx *gorm.DB) error {
|
||||
@@ -245,6 +247,32 @@ func (d *Repository) ConsumeAndMarkConsumed(ctx context.Context, outboxID int64,
|
||||
})
|
||||
}
|
||||
|
||||
// ConsumeInTx 执行 outbox 业务事务,但不负责标记 consumed。
|
||||
// 1. 先锁定当前 outbox 记录,避免并发消费者同时处理同一条消息;
|
||||
// 2. 只要业务函数返回错误,就保持消息为 pending,交给上层 retry;
|
||||
// 3. 业务成功后再由上层单独标记 consumed,这样可以把远端 RPC 移到事务外。
|
||||
func (d *Repository) ConsumeInTx(ctx context.Context, outboxID int64, fn func(tx *gorm.DB) error) error {
|
||||
return d.db.WithContext(ctx).Transaction(func(tx *gorm.DB) error {
|
||||
var outboxMsg model.AgentOutboxMessage
|
||||
err := tx.Table(d.tableName()).Clauses(clause.Locking{Strength: "UPDATE"}).Where("id = ?", outboxID).First(&outboxMsg).Error
|
||||
if err != nil {
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
return nil
|
||||
}
|
||||
return err
|
||||
}
|
||||
if outboxMsg.Status == model.OutboxStatusConsumed || outboxMsg.Status == model.OutboxStatusDead {
|
||||
return nil
|
||||
}
|
||||
if fn != nil {
|
||||
if err = fn(tx); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
})
|
||||
}
|
||||
|
||||
func (d *Repository) scopedDB(ctx context.Context) *gorm.DB {
|
||||
return d.db.WithContext(ctx).Table(d.tableName())
|
||||
}
|
||||
@@ -285,10 +313,10 @@ func calcRetryBackoff(retryCount int) time.Duration {
|
||||
if retryCount <= 0 {
|
||||
return time.Second
|
||||
}
|
||||
if retryCount > 6 {
|
||||
retryCount = 6
|
||||
if retryCount > 10 {
|
||||
retryCount = 10
|
||||
}
|
||||
return time.Second * time.Duration(1<<(retryCount-1))
|
||||
return time.Duration(retryCount*retryCount) * time.Second
|
||||
}
|
||||
|
||||
func truncateError(reason string) string {
|
||||
|
||||
Reference in New Issue
Block a user