S1-B 收口#1: send 拆三子包(设计规范§一)——submit(受理:SubmitService/Pipeline/校验/文件导入/退款+事件契约 SubmitEvent/EventDetail/SubmitTaskMeta+状态契约族 TaskSt*/DetailSt*/AuditReadyExempt 迁入并导出)/consume(handleDeferred 补全/补偿扫描)/dispatch(TimerScheduler+ChannelDispatcher/聚合/限速/背压/批量落库/段登记);根包保留别名门面(facade.go,40 符号)外部调用方零改动;依赖单向 consume→{submit,dispatch}、dispatch→submit 无环;桥接测试 helper 本地化(newTimerDB/fakeDispatcher/getTask/captureSink);全仓 -race 38 包 RC=0(router MySQL 硬断言口径绿)
This commit is contained in:
@@ -4,9 +4,9 @@ import (
|
||||
"testing"
|
||||
)
|
||||
|
||||
// TestInsertSQL 验证语句构造。
|
||||
// TestInsertSQL 验证语句构造(S1-B:标识符反引号化,兼容含连字符表名)。
|
||||
func TestInsertSQL(t *testing.T) {
|
||||
if got := insertSQL("mt", []string{"id", "mobile"}); got != "INSERT INTO mt (id,mobile)" {
|
||||
if got := insertSQL("mt", []string{"id", "mobile"}); got != "INSERT INTO `mt` (`id`,`mobile`)" {
|
||||
t.Errorf("insertSQL = %q", got)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -6,13 +6,15 @@
|
||||
// 顺带修复"手动审核通过后任务停滞"缺陷);
|
||||
// - 分发复用 ChannelDispatcher(router 选路→聚合/直发链路→明细终态);
|
||||
// 系统性错误回滚 status 1→0 留待重扫(claim 原子性保证重试幂等)。
|
||||
package send
|
||||
package consume
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"go-sms/internal/service/send/dispatch"
|
||||
"go-sms/internal/service/send/submit"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
@@ -26,64 +28,15 @@ import (
|
||||
"gorm.io/gorm/clause"
|
||||
)
|
||||
|
||||
// SubmitEvent sms-submit 事件载荷(Day46 openapi 五字段向后兼容+Day48 扩展+Day52 R2 明细全量)。
|
||||
type SubmitEvent struct {
|
||||
TaskID int64 `json:"task_id"`
|
||||
TaskNo string `json:"task_no"`
|
||||
AccountID int64 `json:"account_id"`
|
||||
SendType int8 `json:"send_type"`
|
||||
PassCount int `json:"pass_count"` // 过拦截条数(0=全拦,消费者跳过;Day52 起由 details 推导)
|
||||
Source string `json:"source"`
|
||||
CreatedAt string `json:"created_at"`
|
||||
Business string `json:"business,omitempty"` // Day 49:sms/video(biz_type 路由;空=sms 兼容 Day48 旧载荷)
|
||||
// Day52 R2:明细全量(入口预生成雪花 ID+拦截终态;消费侧批量落库,入口事务零明细写)。
|
||||
// 空=Day48 旧格式/兜底扫描场景,消费侧不落(明细已由降级路径或旧版本落库)。
|
||||
Details []EventDetail `json:"details,omitempty"`
|
||||
// P0-1(Day 63)接入异步化:Deferred=true 表示受理侧走 SubmitFast 快路径
|
||||
// (Validate+预扣+DetailID 预生成+投递,零 DB 写),task 落库/拦截判定/退费/
|
||||
// 审核分流全部由消费侧补全(handleDeferred);TaskID=0(自增主键消费侧生成)。
|
||||
Deferred bool `json:"deferred,omitempty"`
|
||||
// TaskMeta Deferred 专用:任务行补建字段(与 entity.MessageTask 对齐;
|
||||
// 旧事件零携带,消费侧按非 Deferred 老路径处理)。
|
||||
TaskMeta *SubmitTaskMeta `json:"task_meta,omitempty"`
|
||||
}
|
||||
|
||||
// SubmitTaskMeta 快路径任务补建载荷(受理侧未落库,消费侧 INSERT 重建)。
|
||||
type SubmitTaskMeta struct {
|
||||
Content string `json:"content"`
|
||||
Sign string `json:"sign"`
|
||||
MobileCount int `json:"mobile_count"`
|
||||
FeeTotal float64 `json:"fee_total"`
|
||||
SubmitIP string `json:"submit_ip"`
|
||||
CallData string `json:"call_data"`
|
||||
Source int8 `json:"source"` // 任务来源 int8(WEB=4/CMPP=2/SMPP=3/API=1)
|
||||
ScheduleAt string `json:"schedule_at,omitempty"` // RFC3339,空=非定时
|
||||
}
|
||||
|
||||
// EventDetail 明细行载荷(task_id/account_id 公共字段在事件级;字段与 entity.MessageDetail 对齐)。
|
||||
type EventDetail struct {
|
||||
ID int64 `json:"id"` // 雪花主键(入口预生成,回执配对键)
|
||||
Mobile string `json:"mobile"`
|
||||
Content string `json:"content"`
|
||||
Status int8 `json:"status"` // 0=存活 5=拦截终态(受理期已退费)
|
||||
Fee float64 `json:"fee"` // 计费条数
|
||||
ErrorCode string `json:"error_code,omitempty"`
|
||||
ExtCode string `json:"ext_code,omitempty"`
|
||||
CallData string `json:"call_data,omitempty"`
|
||||
// Day66:明细单表化冗余(空=旧载荷,persistDetailsDB 兜底 ev.Business/'sms')。
|
||||
Business string `json:"business,omitempty"`
|
||||
Source int8 `json:"source,omitempty"` // 0=旧载荷→兜底 API
|
||||
}
|
||||
|
||||
// SubmitConsumer 事件消费者。
|
||||
type SubmitConsumer struct {
|
||||
db *gorm.DB
|
||||
dp TaskDispatcher
|
||||
dp dispatch.TaskDispatcher
|
||||
log *zap.Logger
|
||||
blocked BlockedDeliverSink // Day62c:拦截终态 CMPP 下发出口(nil=静默)
|
||||
// P0-1(Day 63)Deferred 补全依赖(受理快路径的同步链后移;nil=对应环节降级)。
|
||||
chain *interceptor.Chain // 拦截器链(nil=全放行,兼容最小装配)
|
||||
rt TaskRouter // 审核分流(nil=停留待审,48h Scanner 兜底)
|
||||
rt submit.TaskRouter // 审核分流(nil=停留待审,48h Scanner 兜底)
|
||||
bal balance.Store // 拦截/审核拒退费(nil=不退,对账 Reconciler 兜底)
|
||||
}
|
||||
|
||||
@@ -109,7 +62,7 @@ func (c *SubmitConsumer) WithConsumerChain(ch *interceptor.Chain) *SubmitConsume
|
||||
}
|
||||
|
||||
// WithConsumerRouter 注入审核分流(P0-1 Deferred 补全;链式)。
|
||||
func (c *SubmitConsumer) WithConsumerRouter(rt TaskRouter) *SubmitConsumer {
|
||||
func (c *SubmitConsumer) WithConsumerRouter(rt submit.TaskRouter) *SubmitConsumer {
|
||||
c.rt = rt
|
||||
return c
|
||||
}
|
||||
@@ -121,7 +74,7 @@ func (c *SubmitConsumer) WithConsumerBalance(b balance.Store) *SubmitConsumer {
|
||||
}
|
||||
|
||||
// NewSubmitConsumer 构造。
|
||||
func NewSubmitConsumer(db *gorm.DB, dp TaskDispatcher, log *zap.Logger) *SubmitConsumer {
|
||||
func NewSubmitConsumer(db *gorm.DB, dp dispatch.TaskDispatcher, log *zap.Logger) *SubmitConsumer {
|
||||
if log == nil {
|
||||
log = zap.NewNop()
|
||||
}
|
||||
@@ -133,7 +86,7 @@ func NewSubmitConsumer(db *gorm.DB, dp TaskDispatcher, log *zap.Logger) *SubmitC
|
||||
// Day52 R2:明细批量落库先于认领/下发(回执配对键先于 submit 存在;
|
||||
// 雪花主键冲突 DoNothing=重投幂等;失败返回错误进重试——明细为对账事实源不能丢)。
|
||||
func (c *SubmitConsumer) HandlePayload(ctx context.Context, payload []byte) error {
|
||||
var ev SubmitEvent
|
||||
var ev submit.SubmitEvent
|
||||
if err := json.Unmarshal(payload, &ev); err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -161,7 +114,7 @@ func (c *SubmitConsumer) HandlePayload(ctx context.Context, payload []byte) erro
|
||||
// ①拦截器链判定(命中→明细终态 5+拦截费退)②task INSERT 补建(uk_task_no
|
||||
// 冲突=重投幂等:查回既有 ID,退费仅首次执行)③明细落库+拦截回执桥(Day62c)
|
||||
// ④全拦→任务终态 ⑤审核分流(拒绝→存活费退)⑥claim/dispatch(待审/定时自然跳过)。
|
||||
func (c *SubmitConsumer) handleDeferred(ctx context.Context, ev *SubmitEvent) error {
|
||||
func (c *SubmitConsumer) handleDeferred(ctx context.Context, ev *submit.SubmitEvent) error {
|
||||
if len(ev.Details) == 0 || ev.TaskMeta == nil {
|
||||
return nil // 防御:Deferred 事件必带明细与任务载荷(缺失静默,防毒丸循环重试)
|
||||
}
|
||||
@@ -248,7 +201,7 @@ func (c *SubmitConsumer) handleDeferred(ctx context.Context, ev *SubmitEvent) er
|
||||
_ = c.db.WithContext(ctx).Model(&entity.MessageTask{}).
|
||||
Where("id = ? AND status = ?", task.ID, 0).
|
||||
Updates(map[string]any{
|
||||
"status": taskStDone, "fail_count": len(ev.Details), "finished_at": time.Now(),
|
||||
"status": submit.TaskStDone, "fail_count": len(ev.Details), "finished_at": time.Now(),
|
||||
}).Error
|
||||
return nil
|
||||
}
|
||||
@@ -282,7 +235,7 @@ func isDupTaskNo(err error) bool {
|
||||
// (status=5)且 task.source=CMPP 逐条推送下发事件——拦截明细无上游回执可等,
|
||||
// 不推则 CMPP 客户永远收不到状态(真机报障实证)。
|
||||
// Day65:内核拆出 persistDetailsDB(exec)——deferred 路径与任务补建同事务执行。
|
||||
func (c *SubmitConsumer) persistDetails(ctx context.Context, ev *SubmitEvent) error {
|
||||
func (c *SubmitConsumer) persistDetails(ctx context.Context, ev *submit.SubmitEvent) error {
|
||||
inserted, err := persistDetailsDB(c.db.WithContext(ctx), ctx, ev)
|
||||
if err != nil {
|
||||
return err
|
||||
@@ -293,7 +246,7 @@ func (c *SubmitConsumer) persistDetails(ctx context.Context, ev *SubmitEvent) er
|
||||
|
||||
// persistDetailsDB 明细批量落库(exec=连接或事务;返回本轮实际插入行数——
|
||||
// 重投幂等锚=雪花主键 OnConflict DoNothing)。
|
||||
func persistDetailsDB(exec *gorm.DB, ctx context.Context, ev *SubmitEvent) (int64, error) {
|
||||
func persistDetailsDB(exec *gorm.DB, ctx context.Context, ev *submit.SubmitEvent) (int64, error) {
|
||||
if len(ev.Details) == 0 {
|
||||
return 0, nil
|
||||
}
|
||||
@@ -310,7 +263,7 @@ func persistDetailsDB(exec *gorm.DB, ctx context.Context, ev *SubmitEvent) (int6
|
||||
}
|
||||
}
|
||||
if src == 0 {
|
||||
src = TaskSourceAPI
|
||||
src = submit.TaskSourceAPI
|
||||
}
|
||||
rows = append(rows, entity.MessageDetail{
|
||||
SnowflakeModel: entity.SnowflakeModel{ID: d.ID},
|
||||
@@ -338,7 +291,7 @@ func persistDetailsDB(exec *gorm.DB, ctx context.Context, ev *SubmitEvent) (int6
|
||||
|
||||
// pushBlocked 拦截终态下发(Day62c):本轮新插入才推(RowsAffected=0=纯重投跳过;
|
||||
// 批内混合插入的极端重投重复可容忍,客户端按 Stat 幂等处理)。
|
||||
func (c *SubmitConsumer) pushBlocked(ctx context.Context, ev *SubmitEvent, inserted int64) {
|
||||
func (c *SubmitConsumer) pushBlocked(ctx context.Context, ev *submit.SubmitEvent, inserted int64) {
|
||||
if c.blocked == nil || inserted == 0 {
|
||||
return
|
||||
}
|
||||
@@ -361,7 +314,7 @@ func (c *SubmitConsumer) pushBlocked(ctx context.Context, ev *SubmitEvent, inser
|
||||
zap.Int64("task_id", ev.TaskID), zap.Error(err))
|
||||
return
|
||||
}
|
||||
if source != TaskSourceCMPP {
|
||||
if source != submit.TaskSourceCMPP {
|
||||
return // HTTP/控制台/SMPP 来源无 CMPP 会话诉求(SMPP 下发另立项)
|
||||
}
|
||||
submitAt := time.Now().Format("0102150405") // CMPP MMDDHHMMSS
|
||||
@@ -378,7 +331,7 @@ func (c *SubmitConsumer) pushBlocked(ctx context.Context, ev *SubmitEvent, inser
|
||||
func (c *SubmitConsumer) claim(ctx context.Context, id int64) bool {
|
||||
res := c.db.WithContext(ctx).Model(&entity.MessageTask{}).
|
||||
Where("id = ? AND status = 0 AND audit_status IN (1, 3) AND send_type IN (1, 2)", id).
|
||||
Update("status", taskStSending)
|
||||
Update("status", submit.TaskStSending)
|
||||
return res.RowsAffected == 1
|
||||
}
|
||||
|
||||
@@ -393,12 +346,12 @@ func (c *SubmitConsumer) dispatch(ctx context.Context, id int64) error {
|
||||
// Day62d:陈年孤儿任务(旧格式无明细,事件永不再来)一次性终态结案——
|
||||
// 否则 ReclaimScanner 5s/轮死循环重试(dev 实证 task 343 刷 warn 两日)。
|
||||
// 1h 宽限覆盖 Day52 事件在途窗口;正常任务明细早已落库。
|
||||
if errors.Is(derr, ErrDetailsNotReady) && time.Since(task.CreatedAt) > time.Hour {
|
||||
if errors.Is(derr, dispatch.ErrDetailsNotReady) && time.Since(task.CreatedAt) > time.Hour {
|
||||
c.log.Warn("orphan_task_finalized",
|
||||
zap.Int64("task_id", id), zap.Time("created_at", task.CreatedAt))
|
||||
res := c.db.WithContext(ctx).Model(&entity.MessageTask{}).
|
||||
Where("id = ? AND status = ?", id, taskStSending).
|
||||
Updates(map[string]any{"status": taskStDone, "finished_at": time.Now()})
|
||||
Where("id = ? AND status = ?", id, submit.TaskStSending).
|
||||
Updates(map[string]any{"status": submit.TaskStDone, "finished_at": time.Now()})
|
||||
if res.Error == nil && res.RowsAffected > 0 {
|
||||
return nil
|
||||
}
|
||||
@@ -407,9 +360,9 @@ func (c *SubmitConsumer) dispatch(ctx context.Context, id int64) error {
|
||||
return c.rollback(ctx, id)
|
||||
}
|
||||
res := c.db.WithContext(ctx).Model(&entity.MessageTask{}).
|
||||
Where("id = ? AND status = ?", id, taskStSending).
|
||||
Where("id = ? AND status = ?", id, submit.TaskStSending).
|
||||
Updates(map[string]any{
|
||||
"status": taskStDone, "success_count": sent, "fail_count": failed,
|
||||
"status": submit.TaskStDone, "success_count": sent, "fail_count": failed,
|
||||
"finished_at": time.Now(),
|
||||
})
|
||||
if res.Error != nil || res.RowsAffected == 0 {
|
||||
@@ -421,7 +374,7 @@ func (c *SubmitConsumer) dispatch(ctx context.Context, id int64) error {
|
||||
// rollback 认领回退(status 1→0,下轮重扫;幂等由 claim 保证)。
|
||||
func (c *SubmitConsumer) rollback(ctx context.Context, id int64) error {
|
||||
return c.db.WithContext(ctx).Model(&entity.MessageTask{}).
|
||||
Where("id = ? AND status = ?", id, taskStSending).
|
||||
Where("id = ? AND status = ?", id, submit.TaskStSending).
|
||||
Update("status", 0).Error
|
||||
}
|
||||
|
||||
@@ -433,7 +386,7 @@ type ReclaimScanner struct {
|
||||
}
|
||||
|
||||
// NewReclaimScanner 构造。
|
||||
func NewReclaimScanner(db *gorm.DB, dp TaskDispatcher, log *zap.Logger) *ReclaimScanner {
|
||||
func NewReclaimScanner(db *gorm.DB, dp dispatch.TaskDispatcher, log *zap.Logger) *ReclaimScanner {
|
||||
return &ReclaimScanner{db: db, sc: NewSubmitConsumer(db, dp, log), batch: 100}
|
||||
}
|
||||
|
||||
+18
-16
@@ -1,6 +1,6 @@
|
||||
// consumer_test.go Day 48:submit 消费者单测——事件认领分发终态/幂等重投/
|
||||
// 定时域不认领/补偿扫描(审核通过即发)/毒丸载荷回死信口径。
|
||||
package send
|
||||
package consume
|
||||
|
||||
import (
|
||||
"context"
|
||||
@@ -11,6 +11,8 @@ import (
|
||||
"time"
|
||||
|
||||
"go-sms/internal/model/entity"
|
||||
"go-sms/internal/service/send/dispatch"
|
||||
"go-sms/internal/service/send/submit"
|
||||
|
||||
"go.uber.org/zap"
|
||||
"gorm.io/gorm"
|
||||
@@ -40,7 +42,7 @@ func mkImmediateTask(t *testing.T, db *gorm.DB, audit int8, sendType int8) *enti
|
||||
|
||||
func evPayload(t *testing.T, task *entity.MessageTask, pass int) []byte {
|
||||
t.Helper()
|
||||
b, err := json.Marshal(SubmitEvent{TaskID: task.ID, TaskNo: task.TaskNo,
|
||||
b, err := json.Marshal(submit.SubmitEvent{TaskID: task.ID, TaskNo: task.TaskNo,
|
||||
AccountID: task.AccountID, SendType: task.SendType, PassCount: pass})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
@@ -86,7 +88,7 @@ func TestConsumerIdempotentReplay(t *testing.T) {
|
||||
// TestConsumerSkipDomains 域过滤:定时(3)/待审(audit=0)不认领。
|
||||
func TestConsumerSkipDomains(t *testing.T) {
|
||||
db := newTimerDB(t)
|
||||
timerTask := mkImmediateTask(t, db, 3, SendTypeTimer)
|
||||
timerTask := mkImmediateTask(t, db, 3, submit.SendTypeTimer)
|
||||
pending := mkImmediateTask(t, db, 0, 1)
|
||||
dp := &fakeDispatcher{}
|
||||
sc := NewSubmitConsumer(db, dp, zap.NewNop())
|
||||
@@ -178,11 +180,11 @@ func TestConsumerPersistDetailsIdempotent(t *testing.T) {
|
||||
if err := db.Create(task).Error; err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
dets := []EventDetail{
|
||||
dets := []submit.EventDetail{
|
||||
{ID: 9200000001, Mobile: "13800138000", Content: "【店】测试", Fee: 1},
|
||||
{ID: 9200000002, Mobile: "13800138001", Content: "【店】测试", Fee: 1},
|
||||
}
|
||||
b, err := json.Marshal(SubmitEvent{TaskID: task.ID, TaskNo: task.TaskNo,
|
||||
b, err := json.Marshal(submit.SubmitEvent{TaskID: task.ID, TaskNo: task.TaskNo,
|
||||
AccountID: task.AccountID, SendType: 1, PassCount: 2, Details: dets})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
@@ -227,9 +229,9 @@ func TestConsumerBlockedDeliver(t *testing.T) {
|
||||
source int8
|
||||
want int
|
||||
}{
|
||||
{TaskSourceCMPP, 2}, // CMPP:2 条拦截行各 1 次
|
||||
{TaskSourceAPI, 0}, // API 来源:不下发
|
||||
{TaskSourceWeb, 0}, // 控制台来源:不下发
|
||||
{submit.TaskSourceCMPP, 2}, // CMPP:2 条拦截行各 1 次
|
||||
{submit.TaskSourceAPI, 0}, // API 来源:不下发
|
||||
{submit.TaskSourceWeb, 0}, // 控制台来源:不下发
|
||||
} {
|
||||
db := newTimerDB(t)
|
||||
task := &entity.MessageTask{
|
||||
@@ -240,14 +242,14 @@ func TestConsumerBlockedDeliver(t *testing.T) {
|
||||
if err := db.Create(task).Error; err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
dets := []EventDetail{
|
||||
dets := []submit.EventDetail{
|
||||
{ID: 9300000001, Mobile: "13800138000", Content: task.Content, Fee: 1}, // 存活
|
||||
{ID: 9300000002, Mobile: "13800138001", Content: task.Content, Status: 5,
|
||||
ErrorCode: "ISC:004"}, // 拦截(签名)
|
||||
{ID: 9300000003, Mobile: "13800138002", Content: task.Content, Status: 5,
|
||||
ErrorCode: "ISC:009"}, // 拦截(其他码)
|
||||
}
|
||||
b, err := json.Marshal(SubmitEvent{TaskID: task.ID, TaskNo: task.TaskNo,
|
||||
b, err := json.Marshal(submit.SubmitEvent{TaskID: task.ID, TaskNo: task.TaskNo,
|
||||
AccountID: task.AccountID, SendType: 1, PassCount: 1, Details: dets})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
@@ -292,7 +294,7 @@ func TestOrphanTaskFinalized(t *testing.T) {
|
||||
return task
|
||||
}
|
||||
old, fresh := mk(-2*time.Hour), mk(-time.Minute)
|
||||
dp := &fakeDispatcher{err: ErrDetailsNotReady} // 明细缺失(事件在途/孤儿)
|
||||
dp := &fakeDispatcher{err: dispatch.ErrDetailsNotReady} // 明细缺失(事件在途/孤儿)
|
||||
rs := NewReclaimScanner(db, dp, zap.NewNop()).WithBatch(10)
|
||||
for i := 0; i < 2; i++ { // 两轮扫描:孤儿首轮结案;新任务始终回滚
|
||||
if _, _, err := rs.ScanOnce(context.Background()); err != nil {
|
||||
@@ -303,7 +305,7 @@ func TestOrphanTaskFinalized(t *testing.T) {
|
||||
if err := db.First(&got, old.ID).Error; err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if got.Status != taskStDone || got.FinishedAt.IsZero() {
|
||||
if got.Status != submit.TaskStDone || got.FinishedAt.IsZero() {
|
||||
t.Fatalf("孤儿任务应终态结案: status=%d finished_at=%v", got.Status, got.FinishedAt)
|
||||
}
|
||||
var freshGot entity.MessageTask
|
||||
@@ -321,15 +323,15 @@ func TestConsumerTimerEventPersistOnly(t *testing.T) {
|
||||
db := newTimerDB(t)
|
||||
task := &entity.MessageTask{
|
||||
TaskNo: "CS" + time.Now().Format("150405.000000001"), AccountID: 1001,
|
||||
SendType: SendTypeTimer, Content: "【店】定时", Sign: "店",
|
||||
SendType: submit.SendTypeTimer, Content: "【店】定时", Sign: "店",
|
||||
MobileCount: 1, AuditStatus: 3, Status: 0,
|
||||
}
|
||||
if err := db.Create(task).Error; err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
b, err := json.Marshal(SubmitEvent{TaskID: task.ID, TaskNo: task.TaskNo,
|
||||
AccountID: task.AccountID, SendType: SendTypeTimer, PassCount: 1,
|
||||
Details: []EventDetail{{ID: 9200000003, Mobile: "13800138000", Content: "【店】定时", Fee: 1}}})
|
||||
b, err := json.Marshal(submit.SubmitEvent{TaskID: task.ID, TaskNo: task.TaskNo,
|
||||
AccountID: task.AccountID, SendType: submit.SendTypeTimer, PassCount: 1,
|
||||
Details: []submit.EventDetail{{ID: 9200000003, Mobile: "13800138000", Content: "【店】定时", Fee: 1}}})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
+17
-16
@@ -1,11 +1,12 @@
|
||||
// deferred_test.go P0-1(Day 63)接入异步化——SubmitFast 快路径(零 DB 写+
|
||||
// deferred_test.go P0-1(Day 63)接入异步化——submit.SubmitFast 快路径(零 DB 写+
|
||||
// MsgID=DetailID 口径)+handleDeferred 消费侧补全(建任务/拦截终态/退费/
|
||||
// 全拦终态/claim 幂等)。
|
||||
package send
|
||||
package consume
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"go-sms/internal/service/send/submit"
|
||||
"testing"
|
||||
|
||||
"go-sms/internal/model/entity"
|
||||
@@ -52,13 +53,13 @@ func TestSubmitFastEmit(t *testing.T) {
|
||||
db := newTimerDB(t)
|
||||
sf, _ := util.NewSnowflake(9)
|
||||
fb, sink := &fakeBal{}, &captureSink{}
|
||||
svc := NewSubmitService(db, sf, nil, WithBalanceStore(fb), WithEventSink(sink))
|
||||
svc := submit.NewSubmitService(db, sf, nil, submit.WithBalanceStore(fb), submit.WithEventSink(sink))
|
||||
|
||||
req := &SubmitReq{AccountID: 1001, SendType: SendTypeBatch,
|
||||
Mobiles: []string{"13800138000"}, Content: "【梦网】验证码", Source: TaskSourceCMPP}
|
||||
req := &submit.SubmitReq{AccountID: 1001, SendType: submit.SendTypeBatch,
|
||||
Mobiles: []string{"13800138000"}, Content: "【梦网】验证码", Source: submit.TaskSourceCMPP}
|
||||
res, err := svc.SubmitFast(context.Background(), req, req.Content, "1.2.3.4")
|
||||
if err != nil {
|
||||
t.Fatalf("SubmitFast err=%v", err)
|
||||
t.Fatalf("submit.SubmitFast err=%v", err)
|
||||
}
|
||||
if len(fb.holds) != 1 {
|
||||
t.Fatalf("Hold 调用 %d 次,期望 1", len(fb.holds))
|
||||
@@ -84,17 +85,17 @@ func TestSubmitFastEmit(t *testing.T) {
|
||||
func TestSubmitFastFail(t *testing.T) {
|
||||
db := newTimerDB(t)
|
||||
sf, _ := util.NewSnowflake(9)
|
||||
req := &SubmitReq{AccountID: 1001, SendType: SendTypeBatch,
|
||||
req := &submit.SubmitReq{AccountID: 1001, SendType: submit.SendTypeBatch,
|
||||
Mobiles: []string{"13800138000"}, Content: "【梦网】x"}
|
||||
|
||||
fb := &fakeBal{failHold: true}
|
||||
svc := NewSubmitService(db, sf, nil, WithBalanceStore(fb), WithEventSink(&captureSink{}))
|
||||
svc := submit.NewSubmitService(db, sf, nil, submit.WithBalanceStore(fb), submit.WithEventSink(&captureSink{}))
|
||||
if _, err := svc.SubmitFast(context.Background(), req, req.Content, ""); err == nil {
|
||||
t.Fatal("欠费应同步拒绝")
|
||||
}
|
||||
|
||||
fb2 := &fakeBal{}
|
||||
svc2 := NewSubmitService(db, sf, nil, WithBalanceStore(fb2), WithEventSink(failSink{}))
|
||||
svc2 := submit.NewSubmitService(db, sf, nil, submit.WithBalanceStore(fb2), submit.WithEventSink(failSink{}))
|
||||
if _, err := svc2.SubmitFast(context.Background(), req, req.Content, ""); err == nil {
|
||||
t.Fatal("投递失败应拒绝")
|
||||
}
|
||||
@@ -106,11 +107,11 @@ func TestSubmitFastFail(t *testing.T) {
|
||||
// mkDeferredEv 构造快路径事件载荷。
|
||||
func mkDeferredEv(t *testing.T, taskNo, content string, detailID int64, mobile string) []byte {
|
||||
t.Helper()
|
||||
b, err := json.Marshal(SubmitEvent{
|
||||
TaskNo: taskNo, AccountID: 1001, SendType: SendTypeBatch, Deferred: true,
|
||||
TaskMeta: &SubmitTaskMeta{Content: content, Sign: "梦网", MobileCount: 1,
|
||||
FeeTotal: 1, SubmitIP: "protocol", Source: TaskSourceCMPP},
|
||||
Details: []EventDetail{{ID: detailID, Mobile: mobile, Content: content,
|
||||
b, err := json.Marshal(submit.SubmitEvent{
|
||||
TaskNo: taskNo, AccountID: 1001, SendType: submit.SendTypeBatch, Deferred: true,
|
||||
TaskMeta: &submit.SubmitTaskMeta{Content: content, Sign: "梦网", MobileCount: 1,
|
||||
FeeTotal: 1, SubmitIP: "protocol", Source: submit.TaskSourceCMPP},
|
||||
Details: []submit.EventDetail{{ID: detailID, Mobile: mobile, Content: content,
|
||||
Status: 0, Fee: 1}},
|
||||
})
|
||||
if err != nil {
|
||||
@@ -151,7 +152,7 @@ func TestHandleDeferredPass(t *testing.T) {
|
||||
if err := db.Where("task_no = ?", "DF-PASS-1").First(&task).Error; err != nil {
|
||||
t.Fatalf("任务应补建: %v", err)
|
||||
}
|
||||
if task.Source != TaskSourceCMPP || task.MobileCount != 1 || task.Status != taskStDone {
|
||||
if task.Source != submit.TaskSourceCMPP || task.MobileCount != 1 || task.Status != submit.TaskStDone {
|
||||
t.Fatalf("补建任务字段异常: source=%d mobile=%d status=%d",
|
||||
task.Source, task.MobileCount, task.Status)
|
||||
}
|
||||
@@ -215,7 +216,7 @@ func TestHandleDeferredBlocked(t *testing.T) {
|
||||
if err := db.Where("task_no = ?", "DF-BLK-1").First(&task).Error; err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if task.Status != taskStDone {
|
||||
if task.Status != submit.TaskStDone {
|
||||
t.Fatalf("全拦任务应终态,got status=%d", task.Status)
|
||||
}
|
||||
if len(dp.calls) != 0 {
|
||||
@@ -0,0 +1,64 @@
|
||||
// helpers_test.go S1-B 拆包:测试库构造自 dispatch/timer_test.go newTimerDB 同体复制
|
||||
// (跨包测试 helper 不可引,本地化;口径一致:sqlite 内存 + task/detail 两表)。
|
||||
package consume
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
|
||||
"github.com/glebarez/sqlite"
|
||||
"gorm.io/gorm"
|
||||
|
||||
"go-sms/internal/model/entity"
|
||||
)
|
||||
|
||||
// newTimerDB 消费域测试库。
|
||||
func newTimerDB(t *testing.T) *gorm.DB {
|
||||
t.Helper()
|
||||
db, err := gorm.Open(sqlite.Open(":memory:"), &gorm.Config{})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
for _, m := range []any{&entity.MessageTask{}, &entity.MessageDetail{}} {
|
||||
if err := db.Migrator().CreateTable(m); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
return db
|
||||
}
|
||||
|
||||
// fakeDispatcher 测试分发器(dispatch.TaskDispatcher 替身;自 dispatch/timer_test.go 同体复制)。
|
||||
type fakeDispatcher struct {
|
||||
calls []int64
|
||||
err error
|
||||
}
|
||||
|
||||
func (f *fakeDispatcher) Dispatch(_ context.Context, task *entity.MessageTask) (int64, int64, error) {
|
||||
if f.err != nil {
|
||||
return 0, 0, f.err
|
||||
}
|
||||
f.calls = append(f.calls, task.ID)
|
||||
return int64(task.MobileCount), 0, nil
|
||||
}
|
||||
|
||||
// getTask 取任务断言用(自 dispatch/timer_test.go 同体复制)。
|
||||
func getTask(t *testing.T, db *gorm.DB, id int64) entity.MessageTask {
|
||||
var task entity.MessageTask
|
||||
if err := db.First(&task, id).Error; err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
return task
|
||||
}
|
||||
|
||||
// captureSink 事件捕获(自 submit/pipeline_test.go 同体复制;桥接测试本地化)。
|
||||
type captureSink struct {
|
||||
events []*entity.MessageTask
|
||||
details [][]entity.MessageDetail
|
||||
}
|
||||
|
||||
func (c *captureSink) EmitSubmit(_ context.Context, t *entity.MessageTask,
|
||||
d []entity.MessageDetail) error {
|
||||
c.events = append(c.events, t)
|
||||
c.details = append(c.details, d)
|
||||
return nil
|
||||
}
|
||||
@@ -2,10 +2,11 @@
|
||||
// 通道+内容 相同的明细聚合为批:满 maxBatch(1000)立即 flush,或窗口(5s)到期统一
|
||||
// flush——同批一次批量提交(CMPP 单包 ≤100 号码,由通道层切包),显著降低
|
||||
// 提交调用次数(验收口径:较逐号提交减少 ≥80%)。
|
||||
package send
|
||||
package dispatch
|
||||
|
||||
import (
|
||||
"context"
|
||||
"go-sms/internal/service/send/submit"
|
||||
"strconv"
|
||||
"sync"
|
||||
"sync/atomic"
|
||||
@@ -180,11 +181,11 @@ func (a *SubmitAggregator) flushGroup(g *aggrGroup) {
|
||||
}
|
||||
a.singleTotal.Add(1)
|
||||
}
|
||||
a.markIDs(ctx, okIDs, detailStSubmitted, g.channelID, "")
|
||||
a.markIDs(ctx, failIDs, detailStFailed, g.channelID, errText(err))
|
||||
a.markIDs(ctx, okIDs, submit.DetailStSubmitted, g.channelID, "")
|
||||
a.markIDs(ctx, failIDs, submit.DetailStFailed, g.channelID, errText(err))
|
||||
return
|
||||
}
|
||||
a.markIDs(ctx, ids, detailStSubmitted, g.channelID, "")
|
||||
a.markIDs(ctx, ids, submit.DetailStSubmitted, g.channelID, "")
|
||||
return
|
||||
}
|
||||
|
||||
@@ -202,8 +203,8 @@ func (a *SubmitAggregator) flushGroup(g *aggrGroup) {
|
||||
}
|
||||
a.singleTotal.Add(1)
|
||||
}
|
||||
a.markIDs(ctx, okIDs, detailStSubmitted, g.channelID, "")
|
||||
a.markIDs(ctx, failIDs, detailStFailed, g.channelID, lastErr)
|
||||
a.markIDs(ctx, okIDs, submit.DetailStSubmitted, g.channelID, "")
|
||||
a.markIDs(ctx, failIDs, submit.DetailStFailed, g.channelID, lastErr)
|
||||
}
|
||||
|
||||
// markIDs 批量终态回写(mark 未绑定时忽略——纯内存测试场景)。
|
||||
+3
-2
@@ -1,11 +1,12 @@
|
||||
// aggregator_test.go Day 47:合包聚合器单测——双阈值触发(1000 条即时/窗口到期)、
|
||||
// 调用次数削减(验收口径 ≥80%)、批量失败逐号回退、分发器聚合接线。
|
||||
package send
|
||||
package dispatch
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"go-sms/internal/service/send/submit"
|
||||
"sync"
|
||||
"sync/atomic"
|
||||
"testing"
|
||||
@@ -195,7 +196,7 @@ func TestDispatcher_WithAggregator(t *testing.T) {
|
||||
t.Fatalf("批量调用=%d(任务级即时冲洗)", n)
|
||||
}
|
||||
var cnt int64
|
||||
db.Model(&entity.MessageDetail{}).Where("channel_id = ? AND status = ?", 7, detailStSubmitted).Count(&cnt)
|
||||
db.Model(&entity.MessageDetail{}).Where("channel_id = ? AND status = ?", 7, submit.DetailStSubmitted).Count(&cnt)
|
||||
if cnt != 20 {
|
||||
t.Fatalf("明细终态落库=%d/20", cnt)
|
||||
}
|
||||
+1
-1
@@ -5,7 +5,7 @@
|
||||
// L3 lag>500 万 → 暂停低优先级投放(消费组 Pause;恢复即 Resume)。
|
||||
//
|
||||
// 水位键由消费者进程写入(30s TTL,采样周期 10s),网关进程只读——跨进程解耦。
|
||||
package send
|
||||
package dispatch
|
||||
|
||||
import (
|
||||
"context"
|
||||
+1
-1
@@ -1,6 +1,6 @@
|
||||
// backpressure_test.go Day 48:三级水位单测——阈值判定/边缘动作(暂停恢复)/
|
||||
// 水位键写入(miniredis)/ReadBPLevel 网关侧读取。
|
||||
package send
|
||||
package dispatch
|
||||
|
||||
import (
|
||||
"context"
|
||||
+1
-1
@@ -2,7 +2,7 @@
|
||||
// 攒批(默认 500 条/批)× N 并发 worker 异步 UPDATE ... WHERE id IN(...),
|
||||
// 保证 MySQL 批量写入不阻塞 Kafka 消费(lag 不因落库增长)。
|
||||
// Add 非阻塞:通道满/已关闭返回 false,调用方直写兜底(可用性优先)。
|
||||
package send
|
||||
package dispatch
|
||||
|
||||
import (
|
||||
"sync"
|
||||
+1
-1
@@ -1,6 +1,6 @@
|
||||
// batchwriter_test.go Day 48:明细终态批量落库 writer 单测——分片落库/截断/
|
||||
// Close 冲刷/空批。
|
||||
package send
|
||||
package dispatch
|
||||
|
||||
import (
|
||||
"testing"
|
||||
@@ -4,7 +4,7 @@
|
||||
// - p50>50ms 持续 30s 自动降速 10%(单向,Reset 手动恢复);
|
||||
// - PaceBudget:群发匀速投放预算——每轮 min(剩余, Σ通道speed − 在途余量),
|
||||
// 防瞬时打满下游队列(技术方案 10.4.1)。
|
||||
package send
|
||||
package dispatch
|
||||
|
||||
import (
|
||||
"context"
|
||||
@@ -1,5 +1,5 @@
|
||||
// pacer_test.go Day 48:下发调度三件套单测——令牌桶限速/在途窗口/自动降速/匀速预算。
|
||||
package send
|
||||
package dispatch
|
||||
|
||||
import (
|
||||
"context"
|
||||
@@ -2,7 +2,7 @@
|
||||
// (msg_id+mobile 唯一),回执消费者按段置态聚合判 detail 终态(A-5.2)。
|
||||
// 写入失败仅日志不上抛:seg 缺失=该段回执到达时无配对(挂起日志),
|
||||
// 资损安全方向与 UNKNOWN 一致(宁可挂起不可错判)。
|
||||
package send
|
||||
package dispatch
|
||||
|
||||
import (
|
||||
"context"
|
||||
@@ -1,5 +1,5 @@
|
||||
// seg_test.go Day 56:段级配对登记构造单测——单号 N 段/合包包×片展开。
|
||||
package send
|
||||
package dispatch
|
||||
|
||||
import (
|
||||
"go-sms/internal/service/channel/pool"
|
||||
@@ -10,11 +10,12 @@
|
||||
// ③ Redis 不可用时 q=nil,纯 DB 扫描降级(行为等价,ZSet 仅省全表轮询);
|
||||
// ④ 到点分发经 TaskDispatcher(router 选路→CMPP 链路提交);
|
||||
// 逐号独立成败,任务终态置 2 完成并回填计数;系统性错误回滚 status=1→0 下轮重试。
|
||||
package send
|
||||
package dispatch
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"go-sms/internal/service/send/submit"
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
@@ -29,15 +30,6 @@ import (
|
||||
// ErrDetailsNotReady 明细尚未落库(Day52 R2:事件明细在途,回滚认领留待重扫)。
|
||||
var ErrDetailsNotReady = errors.New("明细尚未落库")
|
||||
|
||||
// 审核态/任务态稳定契约值(与 audit 包常量一致;避免 send→audit 本体依赖)。
|
||||
const (
|
||||
auditReadyExempt = 3 // 免审(可发)
|
||||
taskStSending = 1
|
||||
taskStDone = 2
|
||||
detailStSubmitted = 1 // 明细:已提交
|
||||
detailStFailed = 3 // 明细:失败
|
||||
)
|
||||
|
||||
// TaskQueue 定时任务唤醒索引(Redis ZSet 实现;测试可替身)。
|
||||
type TaskQueue interface {
|
||||
Enqueue(ctx context.Context, taskID int64, at time.Time) error
|
||||
@@ -177,7 +169,7 @@ func (d *ChannelDispatcher) Dispatch(ctx context.Context, task *entity.MessageTa
|
||||
AccountID: task.AccountID, Content: det.Content, Signature: task.Sign,
|
||||
})
|
||||
if perr != nil {
|
||||
d.markDetail(ctx, det.ID, detailStFailed, chID, "NO_ROUTE:"+perr.Error(), now)
|
||||
d.markDetail(ctx, det.ID, submit.DetailStFailed, chID, "NO_ROUTE:"+perr.Error(), now)
|
||||
failed++
|
||||
continue
|
||||
}
|
||||
@@ -191,7 +183,7 @@ func (d *ChannelDispatcher) Dispatch(ctx context.Context, task *entity.MessageTa
|
||||
// Day48④:直发路径限速(令牌桶+在途窗口;未启用直通)+延迟采样降速判定。
|
||||
if d.pacer != nil {
|
||||
if aerr := d.pacer.Acquire(ctx, chID, 1); aerr != nil {
|
||||
d.markDetail(ctx, det.ID, detailStFailed, chID, aerr.Error(), now)
|
||||
d.markDetail(ctx, det.ID, submit.DetailStFailed, chID, aerr.Error(), now)
|
||||
failed++
|
||||
continue
|
||||
}
|
||||
@@ -203,13 +195,13 @@ func (d *ChannelDispatcher) Dispatch(ctx context.Context, task *entity.MessageTa
|
||||
d.pacer.Release(chID, 1)
|
||||
}
|
||||
if serr != nil {
|
||||
d.markDetail(ctx, det.ID, detailStFailed, chID, serr.Error(), now)
|
||||
d.markDetail(ctx, det.ID, submit.DetailStFailed, chID, serr.Error(), now)
|
||||
failed++
|
||||
continue
|
||||
}
|
||||
// Day 56:段级配对登记(单号 1~9 段,每段独立 msg_id)。
|
||||
writeSegRows(ctx, d.db, segRowsFor(chID, det.ID, det.Mobile, msgIDs))
|
||||
d.markDetail(ctx, det.ID, detailStSubmitted, chID, "", now)
|
||||
d.markDetail(ctx, det.ID, submit.DetailStSubmitted, chID, "", now)
|
||||
sent++
|
||||
}
|
||||
if d.agg != nil {
|
||||
@@ -266,7 +258,7 @@ func (s *TimerScheduler) ScanOnce(ctx context.Context) (enq, claimed, done int,
|
||||
if s.q != nil {
|
||||
var pend []entity.MessageTask
|
||||
if err = s.db.WithContext(ctx).
|
||||
Where("send_type = ? AND status = 0 AND audit_status IN (1, 3)", SendTypeTimer).
|
||||
Where("send_type = ? AND status = 0 AND audit_status IN (1, 3)", submit.SendTypeTimer).
|
||||
Order("schedule_at ASC").Limit(200).Find(&pend).Error; err != nil {
|
||||
return 0, 0, 0, err
|
||||
}
|
||||
@@ -286,7 +278,7 @@ func (s *TimerScheduler) ScanOnce(ctx context.Context) (enq, claimed, done int,
|
||||
var invalid []entity.MessageTask
|
||||
if err = s.db.WithContext(ctx).
|
||||
Where("id IN ? AND NOT (send_type = ? AND status = 0 AND audit_status IN (1, 3))",
|
||||
all, SendTypeTimer).Find(&invalid).Error; err == nil {
|
||||
all, submit.SendTypeTimer).Find(&invalid).Error; err == nil {
|
||||
for _, t := range invalid {
|
||||
_ = s.q.Remove(ctx, t.ID)
|
||||
}
|
||||
@@ -305,7 +297,7 @@ func (s *TimerScheduler) ScanOnce(ctx context.Context) (enq, claimed, done int,
|
||||
var rows []entity.MessageTask
|
||||
if err = s.db.WithContext(ctx).
|
||||
Where("send_type = ? AND status = 0 AND audit_status IN (1, 3) AND schedule_at <= ?",
|
||||
SendTypeTimer, now).
|
||||
submit.SendTypeTimer, now).
|
||||
Order("schedule_at ASC").Limit(s.batch).Find(&rows).Error; err != nil {
|
||||
return enq, 0, 0, err
|
||||
}
|
||||
@@ -337,8 +329,8 @@ func (s *TimerScheduler) ScanOnce(ctx context.Context) (enq, claimed, done int,
|
||||
func (s *TimerScheduler) claim(ctx context.Context, id int64, now time.Time) (bool, error) {
|
||||
res := s.db.WithContext(ctx).Model(&entity.MessageTask{}).
|
||||
Where("id = ? AND send_type = ? AND status = 0 AND audit_status IN (1, 3) AND schedule_at <= ?",
|
||||
id, SendTypeTimer, now).
|
||||
Update("status", taskStSending)
|
||||
id, submit.SendTypeTimer, now).
|
||||
Update("status", submit.TaskStSending)
|
||||
return res.RowsAffected == 1, res.Error
|
||||
}
|
||||
|
||||
@@ -353,7 +345,7 @@ func (s *TimerScheduler) reconcile(ctx context.Context, id int64) {
|
||||
_ = s.q.Remove(ctx, id) // 已删除任务:清索引
|
||||
return
|
||||
}
|
||||
if t.Status == 0 && (t.AuditStatus == 1 || t.AuditStatus == auditReadyExempt) &&
|
||||
if t.Status == 0 && (t.AuditStatus == 1 || t.AuditStatus == submit.AuditReadyExempt) &&
|
||||
t.ScheduleAt != nil && t.ScheduleAt.After(time.Now()) {
|
||||
_ = s.q.Enqueue(ctx, id, *t.ScheduleAt) // 改期:重排新时刻
|
||||
return
|
||||
@@ -371,14 +363,14 @@ func (s *TimerScheduler) dispatch(ctx context.Context, id int64) bool {
|
||||
sent, failed, derr := s.dp.Dispatch(ctx, &task)
|
||||
if derr != nil {
|
||||
_ = s.db.WithContext(ctx).Model(&entity.MessageTask{}).
|
||||
Where("id = ? AND status = ?", id, taskStSending).
|
||||
Where("id = ? AND status = ?", id, submit.TaskStSending).
|
||||
Update("status", 0).Error
|
||||
return false
|
||||
}
|
||||
res := s.db.WithContext(ctx).Model(&entity.MessageTask{}).
|
||||
Where("id = ? AND status = ?", id, taskStSending).
|
||||
Where("id = ? AND status = ?", id, submit.TaskStSending).
|
||||
Updates(map[string]any{
|
||||
"status": taskStDone, "success_count": sent, "fail_count": failed,
|
||||
"status": submit.TaskStDone, "success_count": sent, "fail_count": failed,
|
||||
"finished_at": time.Now(),
|
||||
})
|
||||
if res.Error != nil || res.RowsAffected == 0 {
|
||||
@@ -1,9 +1,10 @@
|
||||
// timer_test.go Day 45:定时调度单测——miniredis 真 ZSet 队列+sqlite:
|
||||
// 到点认领分发终态/幂等二轮/未来不入/改期重排/取消清理/纯 DB 降级/分发错误回滚重试。
|
||||
package send
|
||||
package dispatch
|
||||
|
||||
import (
|
||||
"context"
|
||||
"go-sms/internal/service/send/submit"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
@@ -63,7 +64,7 @@ func mkTimerTask(t *testing.T, db *gorm.DB, at time.Time, audit int8) *entity.Me
|
||||
ttSeq++
|
||||
task := &entity.MessageTask{
|
||||
TaskNo: "TT" + time.Now().Format("150405.000000000") + "_",
|
||||
AccountID: 1001, SendType: SendTypeTimer, Content: "【店】测试", Sign: "店",
|
||||
AccountID: 1001, SendType: submit.SendTypeTimer, Content: "【店】测试", Sign: "店",
|
||||
MobileCount: 2, AuditStatus: audit, Status: 0, ScheduleAt: &at,
|
||||
}
|
||||
task.TaskNo += time.Duration(ttSeq).String()
|
||||
@@ -0,0 +1,79 @@
|
||||
// Package send 发送域门面(S1-B P2 拆包,2026-11-13):
|
||||
// 实现按 设计规范 §一 拆为三子包——submit(受理:SubmitService/Pipeline/校验/文件导入/
|
||||
// 退款,事件契约与状态契约族)、consume(消费:handleDeferred 补全/补偿扫描)、
|
||||
// dispatch(下发:TimerScheduler+ChannelDispatcher/聚合/限速/背压/批量落库/段登记)。
|
||||
// 本包保留类型/函数/常量别名门面,外部调用方(api/openapi/access/report/cmd)零改动;
|
||||
// 新代码建议直接 import 子包。依赖方向:consume→{submit,dispatch},dispatch→submit,单向无环。
|
||||
package send
|
||||
|
||||
import (
|
||||
"go-sms/internal/service/send/consume"
|
||||
"go-sms/internal/service/send/dispatch"
|
||||
"go-sms/internal/service/send/submit"
|
||||
)
|
||||
|
||||
// ---- submit(受理) ----
|
||||
|
||||
type (
|
||||
SubmitService = submit.SubmitService
|
||||
SubmitReq = submit.SubmitReq
|
||||
SubmitOption = submit.SubmitOption
|
||||
PersonalItem = submit.PersonalItem
|
||||
FileImportService = submit.FileImportService
|
||||
ProducerSink = submit.ProducerSink
|
||||
)
|
||||
|
||||
const (
|
||||
TaskSourceCMPP = submit.TaskSourceCMPP
|
||||
TaskSourceSMPP = submit.TaskSourceSMPP
|
||||
TaskSourceAPI = submit.TaskSourceAPI
|
||||
TaskSourceWeb = submit.TaskSourceWeb
|
||||
SendTypeTimer = submit.SendTypeTimer
|
||||
SendTypeBatch = submit.SendTypeBatch
|
||||
MaxPersonalItems = submit.MaxPersonalItems
|
||||
MaxMobiles = submit.MaxMobiles
|
||||
ContentEncodingUCS2 = submit.ContentEncodingUCS2
|
||||
ContentEncodingDefault = submit.ContentEncodingDefault
|
||||
)
|
||||
|
||||
var (
|
||||
NewSubmitService = submit.NewSubmitService
|
||||
WithEventSink = submit.WithEventSink
|
||||
WithBalanceStore = submit.WithBalanceStore
|
||||
WithInterceptorChain = submit.WithInterceptorChain
|
||||
NormalizeTaskSource = submit.NormalizeTaskSource
|
||||
NewFileImportService = submit.NewFileImportService
|
||||
)
|
||||
|
||||
// ---- consume(消费) ----
|
||||
|
||||
type ReclaimScanner = consume.ReclaimScanner
|
||||
|
||||
var (
|
||||
NewSubmitConsumer = consume.NewSubmitConsumer
|
||||
NewReclaimScanner = consume.NewReclaimScanner
|
||||
)
|
||||
|
||||
// ---- dispatch(下发) ----
|
||||
|
||||
type TaskQueue = dispatch.TaskQueue
|
||||
|
||||
const (
|
||||
DefaultAggrWindow = dispatch.DefaultAggrWindow
|
||||
DefaultAggrMaxBatch = dispatch.DefaultAggrMaxBatch
|
||||
DefaultWriterWorker = dispatch.DefaultWriterWorker
|
||||
DefaultWriterBatch = dispatch.DefaultWriterBatch
|
||||
DefaultChannelSpeed = dispatch.DefaultChannelSpeed
|
||||
)
|
||||
|
||||
var (
|
||||
QueueKey = dispatch.QueueKey
|
||||
NewRedisZSetQueue = dispatch.NewRedisZSetQueue
|
||||
NewSubmitAggregator = dispatch.NewSubmitAggregator
|
||||
NewChannelDispatcher = dispatch.NewChannelDispatcher
|
||||
NewTimerScheduler = dispatch.NewTimerScheduler
|
||||
NewDetailWriter = dispatch.NewDetailWriter
|
||||
NewChannelPacer = dispatch.NewChannelPacer
|
||||
NewBackpressure = dispatch.NewBackpressure
|
||||
NewRedisBPStore = dispatch.NewRedisBPStore
|
||||
)
|
||||
@@ -1,5 +1,5 @@
|
||||
// P0-1 诊断(临时,Day63 复压定位):快路径分段耗时打点。
|
||||
package send
|
||||
package submit
|
||||
|
||||
import (
|
||||
"time"
|
||||
@@ -0,0 +1,62 @@
|
||||
// event.go S1-B 拆包:submit 事件契约(原 consumer.go 顶部三类型;生产者持有契约,
|
||||
// consume 消费侧经 import submit 引用——依赖方向 consume→submit 单向)。
|
||||
// 状态契约常量族(原 dispatch/timer.go;跨三子包共用,迁契约所有者=submit 并导出)。
|
||||
package submit
|
||||
|
||||
// 审核态/任务态稳定契约值(与 audit 包常量一致;避免 send→audit 本体依赖)。
|
||||
const (
|
||||
AuditReadyExempt = 3 // 免审(可发)
|
||||
TaskStSending = 1
|
||||
TaskStDone = 2
|
||||
DetailStSubmitted = 1 // 明细:已提交
|
||||
DetailStFailed = 3 // 明细:失败
|
||||
)
|
||||
|
||||
// SubmitEvent sms-submit 事件载荷(Day46 openapi 五字段向后兼容+Day48 扩展+Day52 R2 明细全量)。
|
||||
type SubmitEvent struct {
|
||||
TaskID int64 `json:"task_id"`
|
||||
TaskNo string `json:"task_no"`
|
||||
AccountID int64 `json:"account_id"`
|
||||
SendType int8 `json:"send_type"`
|
||||
PassCount int `json:"pass_count"` // 过拦截条数(0=全拦,消费者跳过;Day52 起由 details 推导)
|
||||
Source string `json:"source"`
|
||||
CreatedAt string `json:"created_at"`
|
||||
Business string `json:"business,omitempty"` // Day 49:sms/video(biz_type 路由;空=sms 兼容 Day48 旧载荷)
|
||||
// Day52 R2:明细全量(入口预生成雪花 ID+拦截终态;消费侧批量落库,入口事务零明细写)。
|
||||
// 空=Day48 旧格式/兜底扫描场景,消费侧不落(明细已由降级路径或旧版本落库)。
|
||||
Details []EventDetail `json:"details,omitempty"`
|
||||
// P0-1(Day 63)接入异步化:Deferred=true 表示受理侧走 SubmitFast 快路径
|
||||
// (Validate+预扣+DetailID 预生成+投递,零 DB 写),task 落库/拦截判定/退费/
|
||||
// 审核分流全部由消费侧补全(handleDeferred);TaskID=0(自增主键消费侧生成)。
|
||||
Deferred bool `json:"deferred,omitempty"`
|
||||
// TaskMeta Deferred 专用:任务行补建字段(与 entity.MessageTask 对齐;
|
||||
// 旧事件零携带,消费侧按非 Deferred 老路径处理)。
|
||||
TaskMeta *SubmitTaskMeta `json:"task_meta,omitempty"`
|
||||
}
|
||||
|
||||
// SubmitTaskMeta 快路径任务补建载荷(受理侧未落库,消费侧 INSERT 重建)。
|
||||
type SubmitTaskMeta struct {
|
||||
Content string `json:"content"`
|
||||
Sign string `json:"sign"`
|
||||
MobileCount int `json:"mobile_count"`
|
||||
FeeTotal float64 `json:"fee_total"`
|
||||
SubmitIP string `json:"submit_ip"`
|
||||
CallData string `json:"call_data"`
|
||||
Source int8 `json:"source"` // 任务来源 int8(WEB=4/CMPP=2/SMPP=3/API=1)
|
||||
ScheduleAt string `json:"schedule_at,omitempty"` // RFC3339,空=非定时
|
||||
}
|
||||
|
||||
// EventDetail 明细行载荷(task_id/account_id 公共字段在事件级;字段与 entity.MessageDetail 对齐)。
|
||||
type EventDetail struct {
|
||||
ID int64 `json:"id"` // 雪花主键(入口预生成,回执配对键)
|
||||
Mobile string `json:"mobile"`
|
||||
Content string `json:"content"`
|
||||
Status int8 `json:"status"` // 0=存活 5=拦截终态(受理期已退费)
|
||||
Fee float64 `json:"fee"` // 计费条数
|
||||
ErrorCode string `json:"error_code,omitempty"`
|
||||
ExtCode string `json:"ext_code,omitempty"`
|
||||
CallData string `json:"call_data,omitempty"`
|
||||
// Day66:明细单表化冗余(空=旧载荷,persistDetailsDB 兜底 ev.Business/'sms')。
|
||||
Business string `json:"business,omitempty"`
|
||||
Source int8 `json:"source,omitempty"` // 0=旧载荷→兜底 API
|
||||
}
|
||||
@@ -1,7 +1,7 @@
|
||||
// fileimport.go Day 44:号码文件导入服务(文件入口)——multipart 上传→解析(TXT/CSV/XLSX)
|
||||
// →归一校验去重→原始文件落 MinIO→返回计数/预览/对象键。
|
||||
// 与 SubmitService 解耦:导入只做「号码准备」,提交仍走 POST /send/submit(文件类型 send_type=4)。
|
||||
package send
|
||||
package submit
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
+1
-1
@@ -1,5 +1,5 @@
|
||||
// fileimport_test.go Day 44:导入服务——去重/非法/表头启发/超限/对象键(依赖倒置 memStore)。
|
||||
package send
|
||||
package submit
|
||||
|
||||
import (
|
||||
"context"
|
||||
@@ -7,7 +7,7 @@
|
||||
//
|
||||
// 路由/下发不在受理期(同步链路 ≤5ms 禁令,技术方案 10.4):由 submit 消费者
|
||||
// (consumer.go)认领后经 ChannelDispatcher 路由→pacer 限速→聚合/直发链路完成。
|
||||
package send
|
||||
package submit
|
||||
|
||||
import (
|
||||
"context"
|
||||
+1
-1
@@ -1,6 +1,6 @@
|
||||
// pipeline_test.go Day 48:全流程状态机单测——预扣/拦截退费/全拦终态/审核拒绝退费/
|
||||
// 落库失败对冲/事件投递口径(验收:失败各环节回退余额)。
|
||||
package send
|
||||
package submit
|
||||
|
||||
import (
|
||||
"context"
|
||||
@@ -4,7 +4,7 @@
|
||||
// Day 54 R1:预扣/退款统一走 balance.Store(mysql 行锁|redis Lua+异步流水,
|
||||
// 评审 docs/review/r1-balance.md);本文件保留 MySQL 原路径实现,
|
||||
// 由 balance.MySQLStore 与回退路径复用。
|
||||
package send
|
||||
package submit
|
||||
|
||||
import (
|
||||
"context"
|
||||
@@ -1,4 +1,4 @@
|
||||
package send
|
||||
package submit
|
||||
|
||||
import (
|
||||
"context"
|
||||
@@ -181,9 +181,9 @@ func (s *SubmitService) Submit(ctx context.Context, req *SubmitReq, tpl *entity.
|
||||
_ = s.db.WithContext(ctx).Model(&entity.MessageTask{}).
|
||||
Where("id = ? AND status = ?", task.ID, 0).
|
||||
Updates(map[string]any{
|
||||
"status": taskStDone, "fail_count": len(mobiles), "finished_at": time.Now(),
|
||||
"status": TaskStDone, "fail_count": len(mobiles), "finished_at": time.Now(),
|
||||
}).Error
|
||||
task.Status = taskStDone
|
||||
task.Status = TaskStDone
|
||||
task.FailCount = len(mobiles)
|
||||
return &SubmitResult{Task: &task, Estimate: est}, nil
|
||||
}
|
||||
@@ -470,9 +470,9 @@ func (s *SubmitService) SubmitPersonal(ctx context.Context, accountID int64,
|
||||
_ = s.db.WithContext(ctx).Model(&entity.MessageTask{}).
|
||||
Where("id = ? AND status = ?", task.ID, 0).
|
||||
Updates(map[string]any{
|
||||
"status": taskStDone, "fail_count": len(merged), "finished_at": time.Now(),
|
||||
"status": TaskStDone, "fail_count": len(merged), "finished_at": time.Now(),
|
||||
}).Error
|
||||
task.Status = taskStDone
|
||||
task.Status = TaskStDone
|
||||
task.FailCount = len(merged)
|
||||
return &SubmitResult{Task: &task}, nil
|
||||
}
|
||||
+1
-1
@@ -1,7 +1,7 @@
|
||||
// submit_source_test.go Day 60(D24-25):受理来源打标——SubmitReq.Source/
|
||||
// SubmitPersonal source 归一(0/非法→API=1;入口显式 2 CMPP/3 SMPP/4 WEB),
|
||||
// message_task.source 落库供下行明细 v2 筛选。
|
||||
package send
|
||||
package submit
|
||||
|
||||
import (
|
||||
"context"
|
||||
@@ -1,6 +1,6 @@
|
||||
// submit_test.go Day 43 修复循环:最小提交链单测——立即/定时受理+明细雪花落库/
|
||||
// 三模式分流接线/余额只读校验/任务号幂等。
|
||||
package send
|
||||
package submit
|
||||
|
||||
import (
|
||||
"context"
|
||||
@@ -1,6 +1,6 @@
|
||||
// Package send Day 41(阶段 4 起步):发送域——Submit DTO+发送校验器+计费预估。
|
||||
// Day 44 群发/个性/文件服务与 Day 46 HTTP API 提交接口复用本包校验。
|
||||
package send
|
||||
package submit
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
+1
-1
@@ -1,5 +1,5 @@
|
||||
// validator_test.go Day 41:发送校验器——长度/号码/变量/签名拒绝路径+预估条数。
|
||||
package send
|
||||
package submit
|
||||
|
||||
import (
|
||||
"strings"
|
||||
Reference in New Issue
Block a user