S4-D1/D2: C1 回调推送引擎+C2 上行自动拉黑——验收项「死信可查/拒收即拉黑」✅: ①C1=callback_task 队列表(迁移 v21,状态机 pending→sent/dead+attempt/next_retry_at/last_code)+PushWorker(2s 扫/批 100/并发 8/2^n 秒指数退避封顶 10min/10 次耗尽死信/UPDATE CAS 防竞态)+双挂点(report 终态首锁[Consumer+PairService,与 CMPP Deliver 同点同语义]+uplink 消费尾)+URL 门控 account_config.report_url/uplink_url(60s TTL 缓存,未配置零任务)+GET /ops/callback(列表+状态计数面板)/POST /ops/callback/:id/resend(casbin ops callback:read/write v21 所种)+前端回调管理页(ops-callback 路由系统管理组+api/callback.ts+状态面板/筛选/载荷展开/死信重派;matrix+guard 33 菜单显式更)+metrics callback_push_total{type,result}/blacklist_total+openapi v1.4.0;②C2=AutoBlacklister 等值词表(trim+lower 防误伤,默认 R/TD/退订/拒收;SystemConfig uplink.blacklist.keywords 可配 60s 刷新)→policy_blacklist(source=2/账号级/uk 幂等)+Redis Set 即时生效,拉黑不影响上行推送(需求原文);③真机全链六步实证(dev 中间件):回执 DELIVRD→detail 终态→HTTP 回调 JSON 送达(200/sent)+上行 R→拉黑(source=2+Redis=1)+推送送达+坏 URL→attempt10→dead→resend ISC:000→修复重投 sent+status_counts 面板;一次性探针 cmd/cbprobe 用后即删+冒烟数据全清;④单测 +13(callback 6[入队门控/载荷/状态机全链/并发批]+uplink 5[词表/配置覆盖/幂等缓存/消费链]+report 2[首锁一次/nil]);⑤门禁=build/vet/-race 40 包/lint 0/eslint0/vue-tsc0/vitest 148/build 全绿;迁移测试 v15..v21 两遍幂等+dev 实跑 v21;gofmt 存量 8 文件顺手清零(S2 遗留);⑥口径诚实标注:载荷无签名挂账/拦截终态 HTTP 未挂(CMPP 侧已有)/PushWorker 单实例 at-least-once;验收单 s4-d1d2-callback.md+CHANGELOG+memory-bank 回填
ci / test (push) Canceled after 0s
ci / web-test (push) Canceled after 0s
ci / build (push) Canceled after 0s
ci / deploy-dev (push) Canceled after 0s

This commit is contained in:
2026-11-16 11:19:55 +08:00
parent 4e7f77a41a
commit bb60d60529
36 changed files with 1544 additions and 52 deletions
+9
View File
@@ -2,6 +2,15 @@
> 本仓 CHANGELOG 自重建起记(POC 明细见归档仓 /data/poc-archive/repos/go-sms.git 的 CHANGELOG.md,已冻结)。 > 本仓 CHANGELOG 自重建起记(POC 明细见归档仓 /data/poc-archive/repos/go-sms.git 的 CHANGELOG.md,已冻结)。
## 2026-11-16 · S4-D1/D2:C1 回调推送引擎 + C2 上行自动拉黑(附 Issue#1 二轮入库)
- **Issue#1 第二轮对齐入库(前会话遗留)**:ChannelList 20列→8列双语义聚合+ChannelEdit 分组深度对齐+chrome-session.mjs 工具;门禁复验四件全绿。
- **C1 回调推送引擎**:callback_task 队列表(v21,状态机 pending→sent/dead)+PushWorker(2s 扫/2^n 秒指数退避封顶 10min/10 次耗尽死信/UPDATE CAS 防竞态);挂点=report 终态首锁(与 CMPP Deliver 同点,Consumer+PairService 双链)+uplink 消费尾;URL 门控=account_config.report_url/uplink_url(60s TTL 缓存,未配置零开销);GET /ops/callback(列表+状态面板)+POST /ops/callback/:id/resend(死信重派);openapi v1.4.0;前端回调管理页(ops-callback 路由+api);metrics callback_push_total/blacklist_total。
- **C2 上行自动拉黑**:AutoBlacklister 等值词表(默认 R/TD/退订/拒收,trim+lower 防误伤;SystemConfig `uplink.blacklist.keywords` 可配 60s 刷新)→policy_blacklist(source=2/账号级/uk 幂等)→Redis Set 即时生效;**拉黑不影响上行推送**(需求原文,真机实证)。
- **真机全链**:回执 DELIVRD→detail 终态→HTTP 回调 JSON 送达接收端(status=1/200);上行 R→拉黑落库+Redis=1+推送送达;坏 URL→attempt 10→dead→resend ISC:000→修复后重投 sent;状态面板 status_counts={dead:1,sent:2};冒烟数据全清+一次性探针即删。
- **门禁**:build/vet/-race **40 包全绿**/lint 0;eslint0/vue-tsc0/**vitest 148**(matrix+ops-callback;guard 菜单 32→33 显式更)/build;迁移 v15..v21 两遍幂等+dev 库实跑 v21;gofmt 存量 8 文件顺手清零(S2 遗留)。
- **口径诚实标注**:载荷无签名(需求未定义,挂账);拦截终态 HTTP 回调未挂(CMPP 侧已有);PushWorker 单实例语义(多实例 at-least-once 同 CMPP Deliver);验收单 docs/acceptance/s4-d1d2-callback.md。
## 2026-11-15(凌晨)· S3 前端收尾泳道收官(FE-6+结算页面族) ## 2026-11-15(凌晨)· S3 前端收尾泳道收官(FE-6+结算页面族)
- **盘点修正**:views 31 页经归档仓逐文件比对**已在位**(S1-B 已搬),S3 搬运项实为零残留;实际工作=FE-D34+FE-6 四件。 - **盘点修正**:views 31 页经归档仓逐文件比对**已在位**(S1-B 已搬),S3 搬运项实为零残留;实际工作=FE-D34+FE-6 四件。
+24 -1
View File
@@ -1,7 +1,7 @@
openapi: 3.0.3 openapi: 3.0.3
info: info:
title: go-sms API v1(阶段 2:账号与权限) title: go-sms API v1(阶段 2:账号与权限)
version: 1.3.0 version: 1.4.0
description: | description: |
阶段 2(Day 21-30)交付的账号与权限域全部端点。 阶段 2(Day 21-30)交付的账号与权限域全部端点。
统一响应 {code,msg,data}:code=ISC:000 成功;错误码三位制(ISC:4xx/5xx)。 统一响应 {code,msg,data}:code=ISC:000 成功;错误码三位制(ISC:4xx/5xx)。
@@ -986,6 +986,29 @@ paths:
application/json: application/json:
schema: {type: object, properties: {date: {type: string, format: date, description: 可回补历史日期(明细热窗内)}}} schema: {type: object, properties: {date: {type: string, format: date, description: 可回补历史日期(明细热窗内)}}}
responses: {"200": {description: "落账行数(账号/通道两表)"}} responses: {"200": {description: "落账行数(账号/通道两表)"}}
/ops/callback:
get:
tags: [callback]
summary: 回调推送任务列表(S4·C1;死信可查;status 0待推送/1已送达/2死信,ops)
parameters:
- {name: status, in: query, schema: {type: integer, enum: [0, 1, 2]}, description: 缺省=全部}
- {name: type, in: query, schema: {type: integer, enum: [1, 2]}, description: "1回执终态 2上行消息;缺省=全部"}
- {name: account_id, in: query, schema: {type: integer}}
- {name: page, in: query, schema: {type: integer, default: 1}}
- {name: page_size, in: query, schema: {type: integer, default: 20}}
responses:
"200":
description: "{total,list[],status_counts{pending,sent,dead}}"
/ops/callback/{id}/resend:
post:
tags: [callback]
summary: 死信重派(status 2→0/attempt 清零/立即投递;仅死信可派,幂等拒绝非死信,ops)
parameters:
- {name: id, in: path, required: true, schema: {type: integer}}
responses:
"200": {description: "{id,resent:true}"}
"404": {description: "任务不存在或非死信状态(ISC:404)"}
components: components:
securitySchemes: securitySchemes:
bearerAuth: {type: http, scheme: bearer, bearerFormat: JWT} bearerAuth: {type: http, scheme: bearer, bearerFormat: JWT}
+18 -3
View File
@@ -21,6 +21,7 @@ import (
"go-sms/internal/pkg/queue" "go-sms/internal/pkg/queue"
"go-sms/internal/service/audit" "go-sms/internal/service/audit"
"go-sms/internal/service/balance" "go-sms/internal/service/balance"
"go-sms/internal/service/callback"
"go-sms/internal/service/channel" "go-sms/internal/service/channel"
"go-sms/internal/service/interceptor" "go-sms/internal/service/interceptor"
"go-sms/internal/service/report" "go-sms/internal/service/report"
@@ -193,13 +194,27 @@ func main() {
zap.Bool("pacer", pacer.Open()), zap.Bool("pacer", pacer.Open()),
zap.String("reclaim", reclaimEvery.String()), zap.Bool("lag_reader", lagErr == nil)) zap.String("reclaim", reclaimEvery.String()), zap.Bool("lag_reader", lagErr == nil))
// S4·C1/C2:回调推送引擎(HTTP 回执终态+上行推送,指数退避+死信可查)
// + 上行自动拉黑(词表 SystemConfig 可配,拒收即拉黑)。
cbSvc := callback.NewService(db)
upBl := uplink.NewAutoBlacklister(db, rc)
blCtx, stopBl := context.WithCancel(context.Background())
go upBl.Start(blCtx)
a.OnShutdown(stopBl)
cbW := callback.NewPushWorker(db)
cbWCtx, stopCbW := context.WithCancel(context.Background())
go cbW.Run(cbWCtx, callback.DefaultScanEvery)
a.OnShutdown(stopCbW)
a.Lg().Info("callback_engine_ready",
zap.Bool("blacklist", true), zap.Duration("scan_every", callback.DefaultScanEvery))
// Day 49:上行消费组(sms-up;15 天窗口匹配下行明细回填 match_task_id; // Day 49:上行消费组(sms-up;15 天窗口匹配下行明细回填 match_task_id;
// 客户推送/自动拉黑属阶段 5 Day54)。独立 group 前缀防 offset 互扰。 // S4:上行推送+自动拉黑挂消费尾部,不阻断匹配主链)。独立 group 前缀防 offset 互扰。
upTopic := cfg.Kafka.Topics["up"] upTopic := cfg.Kafka.Topics["up"]
if upTopic == "" { if upTopic == "" {
upTopic = queue.TopicUp upTopic = queue.TopicUp
} }
uc := uplink.NewConsumer(db) uc := uplink.NewConsumer(db).WithCallback(cbSvc).WithBlacklist(upBl)
ucg, err := queue.NewConsumerGroup(queue.ConsumerGroupConfig{ ucg, err := queue.NewConsumerGroup(queue.ConsumerGroupConfig{
Consumer: queue.ConsumerConfig{ Consumer: queue.ConsumerConfig{
Brokers: cfg.Kafka.Brokers, Brokers: cfg.Kafka.Brokers,
@@ -256,7 +271,7 @@ func main() {
go comp.Run(compCtx, 10*time.Minute) go comp.Run(compCtx, 10*time.Minute)
a.OnShutdown(stopComp) a.OnShutdown(stopComp)
repC := report.NewConsumer(db).WithBilling(bill) repC := report.NewConsumer(db).WithBilling(bill).WithCallback(cbSvc) // S4·C1:回执终态 HTTP 回调入队
// Day 61(P0.5):CMPP 回执协议侧下发桥——终态锁定后投 cmpp-deliver // Day 61(P0.5):CMPP 回执协议侧下发桥——终态锁定后投 cmpp-deliver
// (gateway 进程消费→PushDeliver);producer 缺省时不注入(HTTP 来源无此诉求)。 // (gateway 进程消费→PushDeliver);producer 缺省时不注入(HTTP 来源无此诉求)。
if p := a.Producer(); p != nil { if p := a.Producer(); p != nil {
+6 -1
View File
@@ -25,6 +25,7 @@ import (
"go-sms/internal/service/audit" "go-sms/internal/service/audit"
"go-sms/internal/service/auth" "go-sms/internal/service/auth"
"go-sms/internal/service/balance" "go-sms/internal/service/balance"
"go-sms/internal/service/callback"
"go-sms/internal/service/channel" "go-sms/internal/service/channel"
"go-sms/internal/service/interceptor" "go-sms/internal/service/interceptor"
"go-sms/internal/service/openapi" "go-sms/internal/service/openapi"
@@ -205,7 +206,9 @@ func main() {
} }
// 阶段5收尾(D28 挂账清偿):回执重推+手动配对服务(终态链复用 // 阶段5收尾(D28 挂账清偿):回执重推+手动配对服务(终态链复用
// consumer 同款 finalizeDetail 单一实现;计费走 balStore 同一 Store)。 // consumer 同款 finalizeDetail 单一实现;计费走 balStore 同一 Store)。
pairSvc := report.NewPairService(db, report.NewBilling(db, balStore), sf) // S4·C1:手动配对终态同步入 HTTP 回调(与 consumer 回执链同挂点)。
cbSvc := callback.NewService(db)
pairSvc := report.NewPairService(db, report.NewBilling(db, balStore), sf).WithCallback(cbSvc)
if p := a.Producer(); p != nil { if p := a.Producer(); p != nil {
pairSvc = pairSvc.WithPushSink(&report.ProducerPushSink{P: p}) pairSvc = pairSvc.WithPushSink(&report.ProducerPushSink{P: p})
} }
@@ -236,6 +239,8 @@ func main() {
Pair: pairSvc, Pair: pairSvc,
// S2:结算域(D2=消耗日汇总;充值审核/月结/阶梯/利润随 D3~D5 追加) // S2:结算域(D2=消耗日汇总;充值审核/月结/阶梯/利润随 D3~D5 追加)
Settle: settle.NewService(db), Settle: settle.NewService(db),
// S4·C1:/ops/callback 死信查询与重派(管理面;投递在 consumer 进程)
Callback: cbSvc,
}) })
// Day68:运营总览汇总刷新(回补 30 天+60s ticker 增量 UPSERT stats_daily; // Day68:运营总览汇总刷新(回补 30 天+60s ticker 增量 UPSERT stats_daily;
// 幂等多实例无害,详见 stats_handler.go StartStatsRefresher)。 // 幂等多实例无害,详见 stats_handler.go StartStatsRefresher)。
+50
View File
@@ -0,0 +1,50 @@
# S4 验收单 D1/D2:C1 回调推送引擎 + C2 上行自动拉黑(2026-11-16)
> 泳道:S4 C1/C2+冷热(3 计划日)第 1-2 日;验收项「死信可查 / 拒收即拉黑」✅(冷查<1s 属 D3 未验)。
## 一、交付范围
### C1 HTTP 回调推送引擎(需求 2.5 / 实施计划 5.8)
| 组件 | 文件 | 说明 |
|---|---|---|
| 队列表 | `entity/callback.go` + 迁移 `v21_callback_domain` | callback_task(状态机 0 待推送/1 已送达/2 死信;attempt/next_retry_at/last_code/last_err;idx_cb_status_retry 扫描索引);casbin 种 ops callback:read/write |
| 入队服务 | `service/callback/callback.go` | EnqueueReport(终态首锁后)/EnqueueUplink(上行消费后);URL 门控=account_config.report_url/uplink_url(60s TTL 内存缓存,未配置零任务);Resend(死信 CAS 重派) |
| 推送引擎 | `service/callback/worker.go` | PushWorker 2s 扫描/批 100/并发 8/单请求 5s 超时;2xx→sent;失败 2^attempt 秒指数退避(封顶 10min);10 次耗尽→dead;UPDATE CAS 防并发竞态 |
| 挂点 | `report/consumer.go` + `report/pair.go` | 与 CMPP Deliver 同点(finalizeDetail 首锁后,事务提交后动作,与 bill.Confirm 同构);手动配对终态同推 |
| 挂点 | `uplink/consumer.go` | 匹配回填→拉黑→推送(拉黑不影响推送,需求原文) |
| 管理端点 | `api/http/v1/callback_handler.go` | GET /ops/callback(列表+状态计数面板)/ POST /ops/callback/:id/resend;openapi v1.4.0 |
| 管理页 | `web/src/views/ops/callback/index.vue` + `api/callback.ts` | 状态面板(待推送/已送达/死信存量)+筛选+载荷展开+死信重派;路由 ops-callback(admin/ops,系统管理组) |
| 观测 | metrics | go_sms_callback_push_total{type,result=sent/retry/dead} + go_sms_callback_blacklist_total |
| 装配 | `cmd/consumer`(引擎+双挂点)/ `cmd/gateway`(PairService+管理面) | — |
### C2 上行自动拉黑(需求 2.5 / 实施计划 5.7)
| 组件 | 说明 |
|---|---|
| `uplink/blacklist.go` | AutoBlacklister:词表等值匹配(trim+lower,默认 R/TD/退订/拒收)——防「我要退订它」类正常回复误伤;SystemConfig `uplink.blacklist.keywords` 逗号分隔可配,60s 刷新 |
| 落库 | policy_blacklist INSERT OnConflict DoNothing(**source=2 上行拒收,账号级=归属账号**,operator=uplink-auto;幂等锚 uk_mobile_account) |
| 即时生效 | interceptor.AddBlacklistCache → Redis Set `gosms:blacklist:local`(拦截链一级缓存,下一单即拦) |
## 二、真机全链冒烟(dev MySQL/Kafka/Redis,2026-11-16 11:15)
1. **回执终态→HTTP 回调**:种子 detail(73021000101,status=1)+seg(cbprobe-m1)→ 投 sms-report DELIVRD → 明细 2(SUCCESS)→ callback_task#2 type=1 → PushWorker 推送 → 接收端收到 `/report {"type":"report","detail_id":73021000101,…,"status":2,"stat":"SUCCESS"}`,status=1/last_code=200 ✅
2. **上行→拉黑+推送**:投 sms-up(content="R",account=90001)→ policy_blacklist(mobile=13877776666,source=2,operator=uplink-auto)✅ + Redis SISMEMBER=1 ✅ + 接收端收到 `/uplink {…,"content":"R"}` ✅(**拉黑不影响推送实证**)
3. **死信→重派**:坏 URL 任务 attempt=9 → 一轮失败 attempt=10 → **status=2 dead**(last_err=connection refused)✅ → `POST /ops/callback/3/resend` ISC:000 → URL 修复后重投 → status=1/sent/200 ✅
4. **管理面**:GET /ops/callback?status=2 → list 含死信行 + status_counts={dead:1,sent:2} ✅(非侵入 JWT,走查脚本同法)
5. **metrics**::8082 go_sms_callback_push_total{result="sent",type="report"}=1 / go_sms_callback_blacklist_total=1 ✅
6. 冒烟数据全清(callback_task/policy_blacklist/detail/seg 删除;account_config URL 置空);一次性探针 cmd/cbprobe 用后即删。
## 三、门禁(七件套)
- 后端:build ✓ / vet ✓ / **-race 40 包全绿** / golangci-lint 0 issues / 迁移测试末七窗口 v15..v21 两遍幂等+v21 断言;dev 库实跑 v21(callback_task 表+casbin 两策略核验)。
- 前端:eslint 0 警 / vue-tsc 0 / **vitest 148 全绿**(role-menu-matrix +ops-callback;guard 菜单 32→33 显式更)/ build ✓。
- 单测增量:callback 6 件(入队门控/载荷/状态机全链 500→退避→死信→重派→送达/并发批)+ uplink 5 件(词表/SystemConfig 覆盖/幂等+缓存/消费链挂点)+ report 2 件(首锁入队一次/nil 零开销)。
## 四、口径与假设(诚实标注)
1. **载荷无签名**:POST JSON 不带签名头(需求未定义);客户侧回调地址建议内网/HTTPS。后续可加 `X-Gosms-Signature=md5(api_secret+body)`(挂账)。
2. **拦截终态(受理期 ISC:xxx 拒收)暂不入 HTTP 回调**:CMPP 侧经 blockedDeliverAdapter 已推;HTTP 侧待用户反馈需求后再挂(范围控制)。
3. **多实例部署**:PushWorker 单实例语义;多实例重复推送容忍(at-least-once,CAS 保证状态机,客户端幂等兜底)——与 CMPP Deliver 同语义。
4. **退避参数**:2s 扫描/2^n 秒退避(封顶 10min)/10 次(累计 ~17min)为保守起步;S5 压测期重标。
5. 浏览器页面走查(回调管理页)留待用户本人(:5173,admin/ops 可见「系统管理→回调管理」)。
+7
View File
@@ -24,6 +24,7 @@ import (
"go-sms/internal/service/account" "go-sms/internal/service/account"
"go-sms/internal/service/audit" "go-sms/internal/service/audit"
"go-sms/internal/service/auth" "go-sms/internal/service/auth"
"go-sms/internal/service/callback"
"go-sms/internal/service/channel" "go-sms/internal/service/channel"
"go-sms/internal/service/interceptor" "go-sms/internal/service/interceptor"
"go-sms/internal/service/report" "go-sms/internal/service/report"
@@ -68,6 +69,9 @@ type Deps struct {
// Settle S2:结算域(消耗日汇总查询/手动回补;充值订单/月结/阶梯/利润 // Settle S2:结算域(消耗日汇总查询/手动回补;充值订单/月结/阶梯/利润
// 随 D3~D5 追加;nil=不注册,前端 404 优于半可用)。 // 随 D3~D5 追加;nil=不注册,前端 404 优于半可用)。
Settle *settle.Service Settle *settle.Service
// Callback S4·C1:回调任务管理(死信查询/重派;PushWorker 在 consumer
// 进程,gateway 侧仅管理面;nil=不注册)。
Callback *callback.Service
} }
// Register 注册 v1 路由组。 // Register 注册 v1 路由组。
@@ -197,6 +201,9 @@ func Register(rg *gin.RouterGroup, deps Deps) {
if deps.Settle != nil { if deps.Settle != nil {
registerSettleRoutes(protected, deps) registerSettleRoutes(protected, deps)
} }
if deps.Callback != nil {
registerCallbackRoutes(protected, deps)
}
// 阶段5收尾(D28):回执重推域四端点(回执分页/重推受理/挂起明细/手动配对; // 阶段5收尾(D28):回执重推域四端点(回执分页/重推受理/挂起明细/手动配对;
// 服务依赖 Billing/PushSink,装配见 cmd/gateway)。 // 服务依赖 Billing/PushSink,装配见 cmd/gateway)。
if deps.Pair != nil { if deps.Pair != nil {
+113
View File
@@ -0,0 +1,113 @@
// callback_handler.go S4·C1:回调任务管理端点(死信可查/重派)。
// 投递引擎(PushWorker)在 consumer 进程,gateway 侧仅管理面;
// 端点=GET /ops/callback(列表,状态/类型/账号筛选)+ POST /ops/callback/:id/resend
// (死信重派);casbin 策略 v21 所种(ops callback:read/write)。
package v1
import (
"strconv"
"github.com/gin-gonic/gin"
"gorm.io/gorm"
"go-sms/internal/middleware"
"go-sms/internal/model/entity"
"go-sms/internal/pkg/errors"
"go-sms/internal/pkg/response"
"go-sms/internal/service/callback"
)
func registerCallbackRoutes(protected *gin.RouterGroup, deps Deps) {
h := &callbackHandler{svc: deps.Callback, db: deps.DB}
// 任务列表(含死信;缺省不过滤状态,传 status=0/1/2 精筛)。
protected.GET("/ops/callback",
middleware.RequirePermission(deps.Perm, "callback", "read"), h.list)
// 死信重派(status 2→0;非死信 0 行影响=幂等拒绝)。
protected.POST("/ops/callback/:id/resend",
middleware.RequirePermission(deps.Perm, "callback", "write"), h.resend)
}
type callbackHandler struct {
svc *callback.Service
db *gorm.DB
}
// list GET /ops/callback?page=&page_size=&status=&type=&account_id=。
func (h *callbackHandler) list(c *gin.Context) {
q := h.db.WithContext(c.Request.Context()).Model(&entity.CallbackTask{})
if s := c.Query("status"); s != "" {
v, err := strconv.Atoi(s)
if err != nil || v < 0 || v > 2 {
response.ErrorWithMsg(c, errors.InvalidParam, "status 须为 0/1/2")
return
}
q = q.Where("status = ?", int8(v))
}
if s := c.Query("type"); s != "" {
v, err := strconv.Atoi(s)
if err != nil || v < 1 || v > 2 {
response.ErrorWithMsg(c, errors.InvalidParam, "type 须为 1/2")
return
}
q = q.Where("type = ?", int8(v))
}
if s := c.Query("account_id"); s != "" {
v, err := strconv.ParseInt(s, 10, 64)
if err != nil || v <= 0 {
response.ErrorWithMsg(c, errors.InvalidParam, "account_id 非法")
return
}
q = q.Where("account_id = ?", v)
}
page, size := 1, 20
if v, err := strconv.Atoi(c.Query("page")); err == nil && v > 0 {
page = v
}
if v, err := strconv.Atoi(c.Query("page_size")); err == nil && v > 0 && v <= 200 {
size = v
}
var total int64
if err := q.Count(&total).Error; err != nil {
bizResp(c, err)
return
}
var rows []entity.CallbackTask
if err := q.Order("id DESC").Limit(size).Offset((page - 1) * size).Find(&rows).Error; err != nil {
bizResp(c, err)
return
}
// 状态计数面板(死信可查验收项:一眼看死信存量)。
var statusAgg []struct {
Status int8
N int64
}
_ = h.db.WithContext(c.Request.Context()).Model(&entity.CallbackTask{}).
Select("status, COUNT(*) AS n").Group("status").Scan(&statusAgg).Error
agg := gin.H{}
for _, r := range statusAgg {
key := map[int8]string{0: "pending", 1: "sent", 2: "dead"}[r.Status]
if key != "" {
agg[key] = r.N
}
}
response.Success(c, gin.H{"total": total, "list": rows, "status_counts": agg})
}
// resend POST /ops/callback/:id/resend:死信重派(attempt 清零/立即到期)。
func (h *callbackHandler) resend(c *gin.Context) {
id, err := strconv.ParseInt(c.Param("id"), 10, 64)
if err != nil || id <= 0 {
response.ErrorWithMsg(c, errors.InvalidParam, "id 非法")
return
}
n, err := h.svc.Resend(c.Request.Context(), id)
if err != nil {
bizResp(c, err)
return
}
if n == 0 {
response.Error(c, errors.NotFound.WithMsg("任务不存在或非死信状态"))
return
}
response.Success(c, gin.H{"id": id, "resent": true})
}
+1 -1
View File
@@ -98,7 +98,7 @@ func TestSettleConsume_Scope(t *testing.T) {
} }
var res struct { var res struct {
Data struct { Data struct {
Total int64 `json:"total"` Total int64 `json:"total"`
List []entity.BillingConsumeLog `json:"list"` List []entity.BillingConsumeLog `json:"list"`
} `json:"data"` } `json:"data"`
} }
+2
View File
@@ -49,5 +49,7 @@ func All() []any {
// 统计与运维(7) // 统计与运维(7)
&StatsChannelDaily{}, &SystemConfig{}, &SystemAlertRule{}, &StatsChannelDaily{}, &SystemConfig{}, &SystemAlertRule{},
&SystemLoginLog{}, &SystemOperateLog{}, &SystemErrorCode{}, &FeErrorLog{}, &SystemLoginLog{}, &SystemOperateLog{}, &SystemErrorCode{}, &FeErrorLog{},
// 回调与通知(S4·C1)
&CallbackTask{},
} }
} }
+2 -2
View File
@@ -30,7 +30,7 @@ type BillingConsumeLog struct {
SubmitCount int `json:"submit_count"` SubmitCount int `json:"submit_count"`
SuccessCount int `json:"success_count"` SuccessCount int `json:"success_count"`
FailCount int `json:"fail_count"` FailCount int `json:"fail_count"`
UnknownCount int `json:"unknown_count"` // v20:未知(72h 窗后仍 UNKNOWN)——上家 success_unknown 口径依赖 UnknownCount int `json:"unknown_count"` // v20:未知(72h 窗后仍 UNKNOWN)——上家 success_unknown 口径依赖
FeeTotal float64 `gorm:"type:decimal(12,4)" json:"fee_total"` // 单位=条(=Σ明细 fee,片数口径 Day70) FeeTotal float64 `gorm:"type:decimal(12,4)" json:"fee_total"` // 单位=条(=Σ明细 fee,片数口径 Day70)
CreatedAt time.Time `gorm:"autoCreateTime" json:"created_at"` CreatedAt time.Time `gorm:"autoCreateTime" json:"created_at"`
UpdatedAt time.Time `gorm:"autoUpdateTime" json:"updated_at"` // v20:UPSERT 重跑痕迹 UpdatedAt time.Time `gorm:"autoUpdateTime" json:"updated_at"` // v20:UPSERT 重跑痕迹
@@ -90,7 +90,7 @@ type SettleLadder struct {
OwnerType string `gorm:"size:8;uniqueIndex:uk_owner_tier,priority:1" json:"owner_type"` // account/upstream OwnerType string `gorm:"size:8;uniqueIndex:uk_owner_tier,priority:1" json:"owner_type"` // account/upstream
OwnerID int64 `gorm:"uniqueIndex:uk_owner_tier,priority:2" json:"owner_id"` OwnerID int64 `gorm:"uniqueIndex:uk_owner_tier,priority:2" json:"owner_id"`
TierFrom int64 `gorm:"uniqueIndex:uk_owner_tier,priority:3" json:"tier_from"` // 档位下限(含,条) TierFrom int64 `gorm:"uniqueIndex:uk_owner_tier,priority:3" json:"tier_from"` // 档位下限(含,条)
Price float64 `gorm:"type:decimal(10,4)" json:"price"` // 该档单价(元/条) Price float64 `gorm:"type:decimal(10,4)" json:"price"` // 该档单价(元/条)
CreatedAt time.Time `gorm:"autoCreateTime" json:"created_at"` CreatedAt time.Time `gorm:"autoCreateTime" json:"created_at"`
} }
+38
View File
@@ -0,0 +1,38 @@
package entity
import "time"
// Callback 回调任务类型(S4·C1)。
const (
CallbackTypeReport int8 = 1 // 回执终态推送(账号 report_url)
CallbackTypeUplink int8 = 2 // 上行消息推送(账号 uplink_url)
)
// Callback 回调任务状态机(S4·C1)。
const (
CallbackPending int8 = 0 // 待推送(含退避等待)
CallbackSent int8 = 1 // 已送达(2xx)
CallbackDead int8 = 2 // 死信(重试耗尽,可重派)
)
// CallbackTask HTTP 回调推送任务(S4·C1:指数退避+死信可查)。
// 入队源=report 终态首锁/report 手动配对(EnqueueReport)与 uplink 消费
// (EnqueueUplink);PushWorker 扫描投递。幂等锚=无(每终态一条;report 侧
// 由 finalizeDetail 首锁语义天然至多一次,重放由 detail 终态锁定挡住)。
type CallbackTask struct {
ID int64 `gorm:"primaryKey;autoIncrement" json:"id"`
AccountID int64 `gorm:"index:idx_cb_account,priority:1" json:"account_id"`
Type int8 `gorm:"default:1" json:"type"` // 1 回执 2 上行(CallbackType*)
URL string `gorm:"size:512" json:"url"`
Payload string `gorm:"type:text" json:"payload"` // 客户回调 JSON 载荷
Status int8 `gorm:"index:idx_cb_status_retry,priority:1;default:0" json:"status"`
Attempt int `gorm:"default:0" json:"attempt"`
NextRetryAt time.Time `gorm:"index:idx_cb_status_retry,priority:2" json:"next_retry_at"`
LastCode int `json:"last_code"` // 最近一次 HTTP 状态码(0=连接层错误)
LastErr string `gorm:"size:255" json:"last_err"`
SentAt *time.Time `json:"sent_at"`
CreatedAt time.Time `gorm:"autoCreateTime;index:idx_cb_account,priority:2" json:"created_at"`
UpdatedAt time.Time `gorm:"autoUpdateTime" json:"updated_at"`
}
func (CallbackTask) TableName() string { return "callback_task" }
+18
View File
@@ -83,6 +83,13 @@ var (
// SettleAggregateErrors 消耗日汇总执行失败次数(失败 Warn 不中断, // SettleAggregateErrors 消耗日汇总执行失败次数(失败 Warn 不中断,
// 幂等下轮自愈;持续增长=真故障信号)。 // 幂等下轮自愈;持续增长=真故障信号)。
SettleAggregateErrors prometheus.Counter SettleAggregateErrors prometheus.Counter
// ---- 回调推送域(S4·C1) ----
// CallbackPushTotal 推送结果计数({type}: report/uplink × {result}:
// sent=2xx 送达/retry=失败待重试/dead=死信)。
CallbackPushTotal *prometheus.CounterVec
// CallbackBlacklistTotal 上行自动拉黑新增数(S4·C2;幂等冲突不计)。
CallbackBlacklistTotal prometheus.Counter
) )
var registry = prometheus.NewRegistry() var registry = prometheus.NewRegistry()
@@ -189,6 +196,16 @@ func init() {
Help: "消耗日汇总执行失败次数(幂等下轮自愈,持续增长=真故障,S2)", Help: "消耗日汇总执行失败次数(幂等下轮自愈,持续增长=真故障,S2)",
}) })
CallbackPushTotal = prometheus.NewCounterVec(prometheus.CounterOpts{
Name: "go_sms_callback_push_total",
Help: "回调推送结果(type: report/uplink × result: sent/retry/dead,S4·C1)",
}, []string{"type", "result"})
CallbackBlacklistTotal = prometheus.NewCounter(prometheus.CounterOpts{
Name: "go_sms_callback_blacklist_total",
Help: "上行自动拉黑新增数(幂等冲突不计,S4·C2)",
})
registry.MustRegister( registry.MustRegister(
HTTPRequestsTotal, HTTPRequestDuration, HTTPRequestsInFlight, HTTPRequestsTotal, HTTPRequestDuration, HTTPRequestsInFlight,
SMSSubmitTotal, SMSReportTotal, KafkaProducerMessages, SMSSubmitTotal, SMSReportTotal, KafkaProducerMessages,
@@ -198,6 +215,7 @@ func init() {
LongSmsExpireFailTotal, LongSmsExpireFailTotal,
ReportPendingTotal, ReportRecheckTotal, ReportRequeueTotal, ReportPendingTotal, ReportRecheckTotal, ReportRequeueTotal,
SettleAggregateRows, SettleAggregateErrors, SettleAggregateRows, SettleAggregateErrors,
CallbackPushTotal, CallbackBlacklistTotal,
) )
// Go 运行时与进程指标 // Go 运行时与进程指标
registry.MustRegister( registry.MustRegister(
+15 -11
View File
@@ -29,8 +29,8 @@ func TestMigrations_IdempotentAndVersioned(t *testing.T) {
seen[mig.ID] = true seen[mig.ID] = true
last = mig last = mig
} }
if last == nil || last.ID != "v20_settle_domain" { if last == nil || last.ID != "v21_callback_domain" {
t.Fatalf("终版迁移应为 v20_settle_domain,实际 %v", last) t.Fatalf("终版迁移应为 v21_callback_domain,实际 %v", last)
} }
db, err := gorm.Open(sqlite.Open(":memory:"), &gorm.Config{}) db, err := gorm.Open(sqlite.Open(":memory:"), &gorm.Config{})
@@ -42,28 +42,28 @@ func TestMigrations_IdempotentAndVersioned(t *testing.T) {
} }
opts := gormigrate.DefaultOptions opts := gormigrate.DefaultOptions
opts.TableName = "schema_migrations" opts.TableName = "schema_migrations"
// 注册末七迁移(现 v14..v20;历史 v10/V11 含 MessageTask AutoMigrate,gleberez // 注册末七迁移(现 v15..v21;历史 v10/V11 含 MessageTask AutoMigrate,gleberez
// sqlite 索引不查重,双块连跑撞 idx_account_time——同 v1+v6 已知方言缺口; // sqlite 索引不查重,双块连跑撞 idx_account_time——同 v1+v6 已知方言缺口;
// v14 各建各表、v15 AutoMigrate 加列、v16/v17/v20 空库守卫补建表后加列/索引、 // v15 AutoMigrate 加列、v16/v17/v20 空库守卫补建表后加列/索引、
// v18/v19 新表 AutoMigrate,v20 另含 DROP IF EXISTS 与 casbin HasTable 守卫, // v18/v19/v21 新表 AutoMigrate,v20 另含 DROP IF EXISTS 与 casbin HasTable 守卫,
// 单块两遍覆盖幂等)。 // 单块两遍覆盖幂等)。
migs := Migrations() migs := Migrations()
gm := gormigrate.New(db.Session(&gorm.Session{Logger: logger.Discard}), opts, migs[len(migs)-7:]) gm := gormigrate.New(db.Session(&gorm.Session{Logger: logger.Discard}), opts, migs[len(migs)-7:])
if err := gm.Migrate(); err != nil { if err := gm.Migrate(); err != nil {
t.Fatalf("v14..v20 首遍失败: %v", err) t.Fatalf("v15..v21 首遍失败: %v", err)
} }
if err := gm.Migrate(); err != nil { if err := gm.Migrate(); err != nil {
t.Fatalf("v14..v20 二遍不幂等: %v", err) t.Fatalf("v15..v21 二遍不幂等: %v", err)
} }
v, err := CurrentMigrationVersion(db) v, err := CurrentMigrationVersion(db)
if err != nil { if err != nil {
t.Fatalf("查版本失败: %v", err) t.Fatalf("查版本失败: %v", err)
} }
if v == nil || *v != "v20_settle_domain" { if v == nil || *v != "v21_callback_domain" {
t.Fatalf("版本应为 v20_settle_domain,实际 %v", v) t.Fatalf("版本应为 v21_callback_domain,实际 %v", v)
} }
// (v13 断言随窗口右移移除:message_report_seg/pk_total 由 v13 建, // (v13/v14 断言随窗口右移移除:各表由 v13/v14 建,
// 现窗口 v14..v20 不含 v13;全量链路覆盖仍由 dev MySQL 实库验证。) // 现窗口 v15..v21 不含;全量链路覆盖仍由 dev MySQL 实库验证。)
// Day 62:v15 后 account_config 应有账号级 CMPP 配额两列。 // Day 62:v15 后 account_config 应有账号级 CMPP 配额两列。
if !db.Migrator().HasColumn(&entity.AccountConfig{}, "CmppMaxConns") || if !db.Migrator().HasColumn(&entity.AccountConfig{}, "CmppMaxConns") ||
!db.Migrator().HasColumn(&entity.AccountConfig{}, "CmppSpeed") { !db.Migrator().HasColumn(&entity.AccountConfig{}, "CmppSpeed") {
@@ -97,6 +97,10 @@ func TestMigrations_IdempotentAndVersioned(t *testing.T) {
if db.Migrator().HasTable("stats_user_daily") { if db.Migrator().HasTable("stats_user_daily") {
t.Fatal("v20 后 stats_user_daily 应已 DROP(R2 单一事实源裁决)") t.Fatal("v20 后 stats_user_daily 应已 DROP(R2 单一事实源裁决)")
} }
// S4·C1:v21 后 callback_task 建表(空库守卫 AutoMigrate 路径)。
if !db.Migrator().HasTable(&entity.CallbackTask{}) {
t.Fatal("v21 后应存在 callback_task 表")
}
} }
// newTestDB 内存 SQLite + 自动建表。 // newTestDB 内存 SQLite + 自动建表。
+36
View File
@@ -467,6 +467,42 @@ func Migrations() []*gormigrate.Migration {
return nil // 列/表/策略可手工重建或清除,回滚为空操作(v16~v19 惯例) return nil // 列/表/策略可手工重建或清除,回滚为空操作(v16~v19 惯例)
}, },
}, },
// v21(S4·C1)回调推送域:callback_task 队列表(指数退避+死信可查)
// + ops 角色回调管理策略。表仅 AutoMigrate 空库守卫建(同 v18~v20 惯例;
// 存量库首跑由 migrate 链自动补建,无列变更)。
{
ID: "v21_callback_domain",
Migrate: func(tx *gorm.DB) error {
if !tx.Migrator().HasTable(&entity.CallbackTask{}) {
if err := tx.AutoMigrate(&entity.CallbackTask{}); err != nil {
return err
}
}
if tx.Migrator().HasTable(&casbinRule{}) { // v5 才建 casbin_rule
policies := []casbinRule{
{Ptype: "p", V0: "ops", V1: "callback", V2: "read"}, // 死信/任务查询
{Ptype: "p", V0: "ops", V1: "callback", V2: "write"}, // 死信重派
}
for _, p := range policies {
var n int64
if err := tx.Model(&casbinRule{}).
Where("ptype = ? AND v0 = ? AND v1 = ? AND v2 = ?", p.Ptype, p.V0, p.V1, p.V2).
Count(&n).Error; err != nil {
return err
}
if n == 0 {
if err := tx.Create(&p).Error; err != nil {
return err
}
}
}
}
return nil
},
Rollback: func(tx *gorm.DB) error {
return nil // 新表可手工清除,回滚为空操作(v16~v20 惯例)
},
},
} }
} }
+168
View File
@@ -0,0 +1,168 @@
// Package callback S4·C1:HTTP 回调推送引擎(指数退避+死信可查)。
// 入队源:report 终态首锁(EnqueueReport,与 CMPP Deliver 同挂点)/uplink
// 消费(EnqueueUplink);投递=PushWorker 周期扫描 callback_task(需求 2.5
// 回执/上行绑定地址;实施计划 5.8 重试队列+指数退避)。
// 客户通知双通道并存:CMPP 活跃会话走 cmpp-deliver(deliver_push.go),
// 配置 report_url/uplink_url 的账号走本引擎(验收=死信可查/重派可恢复)。
package callback
import (
"context"
"encoding/json"
"sync"
"time"
"go-sms/internal/model/entity"
"go-sms/internal/pkg/logger"
"go.uber.org/zap"
"gorm.io/gorm"
)
// urlCacheTTL 账号回调地址缓存 TTL(策略变更最迟生效窗口)。
const urlCacheTTL = 60 * time.Second
// statForStatus 明细终态 int8 → 回调载荷状态词(与 statForDeliver 同口径;
// 成功=DELIVRD,失败/未知=errCode 透传截 16B,空兜底 UNKNOWN)。
func statForStatus(status int8, errCode string) string {
if status == 2 {
return "DELIVRD"
}
if errCode == "" {
return "UNKNOWN"
}
if r := []rune(errCode); len(r) > 16 {
return string(r[:16])
}
return errCode
}
// ReportPayload 回执终态回调载荷(标准 JSON;detail_id 与 Submit_Resp MsgID
// 同口径,客户据此对账)。
type ReportPayload struct {
Type string `json:"type"` // "report"
DetailID int64 `json:"detail_id"`
AccountID int64 `json:"account_id"`
Mobile string `json:"mobile"`
Status int8 `json:"status"` // 2 成功 3 失败 4 未知
Stat string `json:"stat"` // DELIVRD/UNDELIV/上游原始码
ErrorCode string `json:"error_code"`
ReportAt time.Time `json:"report_at"`
}
// UplinkPayload 上行消息回调载荷(需求 2.5:拉黑不影响上行推送)。
type UplinkPayload struct {
Type string `json:"type"` // "uplink"
UplinkID int64 `json:"uplink_id"`
AccountID int64 `json:"account_id"`
DestCode string `json:"dest_code"`
SrcMobile string `json:"src_mobile"`
Content string `json:"content"`
RecvAt string `json:"recv_at"` // RFC3339(透传事件原值)
}
type urlEntry struct {
report, uplink string
exp time.Time
}
// Service 回调入队器:URL 缓存(60s TTL)+ 载荷构造 + callback_task 写入。
// 实现 report.ReportEnqueuer / uplink.UplinkEnqueuer(装配层注入)。
type Service struct {
db *gorm.DB
mu sync.RWMutex
urls map[int64]urlEntry
}
// NewService 构造。
func NewService(db *gorm.DB) *Service {
return &Service{db: db, urls: map[int64]urlEntry{}}
}
// urlsFor 账号回调地址(缓存 miss 回源 account_config;DB 故障返回空串=
// 该账号本轮跳过,下轮回源自愈)。
func (s *Service) urlsFor(ctx context.Context, accountID int64) (report, uplink string) {
now := time.Now()
s.mu.RLock()
e, ok := s.urls[accountID]
s.mu.RUnlock()
if ok && now.Before(e.exp) {
return e.report, e.uplink
}
var cfg entity.AccountConfig
if err := s.db.WithContext(ctx).
Where("account_id = ? AND business = ?", accountID, "sms").
First(&cfg).Error; err != nil {
if err != gorm.ErrRecordNotFound {
logger.L().Warn("callback_url_lookup_failed",
zap.Int64("account_id", accountID), zap.Error(err))
}
return "", "" // 不缓存失败:下轮重查
}
s.mu.Lock()
s.urls[accountID] = urlEntry{report: cfg.ReportURL, uplink: cfg.UplinkURL, exp: now.Add(urlCacheTTL)}
s.mu.Unlock()
return cfg.ReportURL, cfg.UplinkURL
}
// EnqueueReport 回执终态入队(report 域调用;失败仅返回错误由调用方 Warn,
// 终态不回滚——与 CMPP Deliver 下发同口径)。
func (s *Service) EnqueueReport(ctx context.Context, detailID int64,
status int8, st, errCode, mobile string, now time.Time) error {
var accountID int64
if err := s.db.WithContext(ctx).Table("message_detail").
Select("account_id").Where("id = ?", detailID).Scan(&accountID).Error; err != nil {
return err
}
reportURL, _ := s.urlsFor(ctx, accountID)
if reportURL == "" {
return nil // 未配置回调地址:零任务(多数账号)
}
p := ReportPayload{
Type: "report", DetailID: detailID, AccountID: accountID, Mobile: mobile,
Status: status, Stat: statForStatus(status, errCode), ErrorCode: errCode,
ReportAt: now,
}
if st != "" { // report 域三态词优先(DELIVRD/FAILURE/UNKNOWN)
p.Stat = st
}
return s.insert(ctx, accountID, entity.CallbackTypeReport, reportURL, p)
}
// EnqueueUplink 上行消息入队(uplink 域调用;拉黑判定在前不影响推送)。
func (s *Service) EnqueueUplink(ctx context.Context, uplinkID, accountID int64,
destCode, srcMobile, content, recvAt string) error {
_, uplinkURL := s.urlsFor(ctx, accountID)
if uplinkURL == "" {
return nil
}
return s.insert(ctx, accountID, entity.CallbackTypeUplink, uplinkURL, UplinkPayload{
Type: "uplink", UplinkID: uplinkID, AccountID: accountID,
DestCode: destCode, SrcMobile: srcMobile, Content: content, RecvAt: recvAt,
})
}
func (s *Service) insert(ctx context.Context, accountID int64, typ int8, url string, payload any) error {
body, err := json.Marshal(payload)
if err != nil {
return err
}
return s.db.WithContext(ctx).Create(&entity.CallbackTask{
AccountID: accountID, Type: typ, URL: url,
Payload: string(body), Status: entity.CallbackPending,
NextRetryAt: time.Now(),
}).Error
}
// Resend 死信重派(status 2→0/attempt 清零/立即到期;CAS 仅死信可派)。
// 返回影响行数(0=非死信或不存在)。
func (s *Service) Resend(ctx context.Context, id int64) (int64, error) {
res := s.db.WithContext(ctx).Model(&entity.CallbackTask{}).
Where("id = ? AND status = ?", id, entity.CallbackDead).
Updates(map[string]any{
"status": entity.CallbackPending, "attempt": 0,
"next_retry_at": time.Now(), "last_err": "resend by ops",
})
return res.RowsAffected, res.Error
}
+203
View File
@@ -0,0 +1,203 @@
// callback_test.go S4·C1:入队(URL 配置门控/载荷)+ 推送状态机
// (sent/退避/死信/重派)——httptest 模拟客户回调地址。
package callback
import (
"context"
"encoding/json"
"net/http"
"net/http/httptest"
"testing"
"time"
"go-sms/internal/model/entity"
"github.com/glebarez/sqlite"
"gorm.io/gorm"
"gorm.io/gorm/logger"
)
func newCallbackDB(t *testing.T) *gorm.DB {
t.Helper()
db, err := gorm.Open(sqlite.Open(":memory:"), &gorm.Config{Logger: logger.Discard})
if err != nil {
t.Fatalf("打开内存库失败: %v", err)
}
if err := db.AutoMigrate(&entity.CallbackTask{}, &entity.AccountConfig{}, &entity.MessageDetail{}); err != nil {
t.Fatalf("AutoMigrate 失败: %v", err)
}
return db
}
func mkDetailRow(t *testing.T, db *gorm.DB, accountID int64) int64 {
t.Helper()
d := entity.MessageDetail{TaskID: 1, AccountID: accountID, Mobile: "13800001111", Status: 2}
d.ID = time.Now().UnixNano()
if err := db.Create(&d).Error; err != nil {
t.Fatal(err)
}
return d.ID
}
// TestEnqueueReport_NoURL 未配置 report_url:零任务(多数账号零开销)。
func TestEnqueueReport_NoURL(t *testing.T) {
db := newCallbackDB(t)
s := NewService(db)
detailID := mkDetailRow(t, db, 5)
if err := s.EnqueueReport(context.Background(), detailID, 2, "SUCCESS", "", "13800001111", time.Now()); err != nil {
t.Fatal(err)
}
var n int64
db.Model(&entity.CallbackTask{}).Count(&n)
if n != 0 {
t.Fatalf("未配置 URL 应零任务, got %d", n)
}
}
// TestEnqueueReport_WithURL 配置 report_url:入队 type=report,载荷字段完整。
func TestEnqueueReport_WithURL(t *testing.T) {
db := newCallbackDB(t)
if err := db.Create(&entity.AccountConfig{
AccountID: 5, Business: "sms", ReportURL: "http://cb.example/hook",
}).Error; err != nil {
t.Fatal(err)
}
s := NewService(db)
detailID := mkDetailRow(t, db, 5)
if err := s.EnqueueReport(context.Background(), detailID, 3, "FAILURE", "UNDELIV", "13800001111", time.Now()); err != nil {
t.Fatal(err)
}
var task entity.CallbackTask
if err := db.First(&task).Error; err != nil {
t.Fatalf("任务未入队: %v", err)
}
if task.Type != entity.CallbackTypeReport || task.URL != "http://cb.example/hook" || task.Status != entity.CallbackPending {
t.Fatalf("type/url/status 异常: %+v", task)
}
var p ReportPayload
if err := json.Unmarshal([]byte(task.Payload), &p); err != nil {
t.Fatal(err)
}
if p.DetailID != detailID || p.AccountID != 5 || p.Stat != "FAILURE" || p.Mobile != "13800001111" {
t.Fatalf("载荷字段异常: %+v", p)
}
}
// TestEnqueueUplink 配置 uplink_url 才入队(type=uplink)。
func TestEnqueueUplink(t *testing.T) {
db := newCallbackDB(t)
s := NewService(db)
if err := s.EnqueueUplink(context.Background(), 9, 5, "10690", "13811112222", "R", "2026-11-16T10:00:00+08:00"); err != nil {
t.Fatal(err)
}
var n int64
db.Model(&entity.CallbackTask{}).Count(&n)
if n != 0 {
t.Fatalf("未配置 uplink_url 应零任务, got %d", n)
}
db.Create(&entity.AccountConfig{AccountID: 5, Business: "sms", UplinkURL: "http://cb.example/mo"})
if err := s.EnqueueUplink(context.Background(), 9, 5, "10690", "13811112222", "R", "2026-11-16T10:00:00+08:00"); err != nil {
t.Fatal(err)
}
var task entity.CallbackTask
if err := db.First(&task).Error; err != nil {
t.Fatalf("任务未入队: %v", err)
}
if task.Type != entity.CallbackTypeUplink {
t.Fatalf("type 期望 uplink(2), got %d", task.Type)
}
var p UplinkPayload
if err := json.Unmarshal([]byte(task.Payload), &p); err != nil {
t.Fatal(err)
}
if p.UplinkID != 9 || p.SrcMobile != "13811112222" || p.Content != "R" {
t.Fatalf("上行载荷异常: %+v", p)
}
}
// TestWorkerSentToDeadLifecycle 状态机全链:500→退避重排→耗尽死信→Resend→200 送达。
func TestWorkerSentToDeadLifecycle(t *testing.T) {
db := newCallbackDB(t)
code := 500
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(code)
}))
defer srv.Close()
task := entity.CallbackTask{
AccountID: 5, Type: entity.CallbackTypeReport, URL: srv.URL,
Payload: `{"type":"report"}`, Status: entity.CallbackPending,
NextRetryAt: time.Now(),
}
if err := db.Create(&task).Error; err != nil {
t.Fatal(err)
}
w := NewPushWorker(db)
w.maxAttempt = 2 // 测试压缩(生产 10)
// 第一轮:500 → attempt=1 重排(退避后到期)。
if n := w.Tick(context.Background()); n != 1 {
t.Fatalf("第一轮应处理 1 条, got %d", n)
}
var got entity.CallbackTask
db.First(&got, task.ID)
if got.Status != entity.CallbackPending || got.Attempt != 1 || !got.NextRetryAt.After(time.Now()) {
t.Fatalf("失败后应 pending+attempt=1+未来重试: %+v", got)
}
// 未到期不重扫。
if n := w.Tick(context.Background()); n != 0 {
t.Fatalf("退避窗口内不应重扫, got %d", n)
}
// 到期重扫 → 耗尽 → 死信。
db.Model(&entity.CallbackTask{}).Where("id = ?", task.ID).
Update("next_retry_at", time.Now().Add(-time.Second))
w.Tick(context.Background())
db.First(&got, task.ID)
if got.Status != entity.CallbackDead || got.Attempt != 2 {
t.Fatalf("重试耗尽应死信: %+v", got)
}
// 死信不参与扫描。
if n := w.Tick(context.Background()); n != 0 {
t.Fatalf("死信不应被扫描, got %d", n)
}
// 重派 → 服务恢复 → 送达。
if n, err := NewService(db).Resend(context.Background(), task.ID); err != nil || n != 1 {
t.Fatalf("Resend: n=%d err=%v", n, err)
}
code = http.StatusOK
w.Tick(context.Background())
db.First(&got, task.ID)
if got.Status != entity.CallbackSent || got.SentAt == nil || got.LastCode != 200 {
t.Fatalf("重派后应送达: %+v", got)
}
// 非死信重派=幂等拒绝(0 行)。
if n, _ := NewService(db).Resend(context.Background(), task.ID); n != 0 {
t.Fatalf("非死信 Resend 应 0 行, got %d", n)
}
}
// TestWorkerConcurrentBatch 并发批次全部推进(信号量不丢任务)。
func TestWorkerConcurrentBatch(t *testing.T) {
db := newCallbackDB(t)
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(200)
}))
defer srv.Close()
for i := 0; i < 20; i++ {
db.Create(&entity.CallbackTask{
AccountID: 5, Type: entity.CallbackTypeUplink, URL: srv.URL,
Payload: "{}", Status: entity.CallbackPending, NextRetryAt: time.Now(),
})
}
w := NewPushWorker(db)
w.batch = 20
w.concurrency = 1 // sqlite 内存库并发写锁:单测串行(批次不丢任务语义不变)
if n := w.Tick(context.Background()); n != 20 {
t.Fatalf("应处理 20 条, got %d", n)
}
var sent int64
db.Model(&entity.CallbackTask{}).Where("status = ?", entity.CallbackSent).Count(&sent)
if sent != 20 {
t.Fatalf("应全部送达, got %d", sent)
}
}
+175
View File
@@ -0,0 +1,175 @@
// worker.go S4·C1:回调推送 Worker——周期扫描 callback_task(status=0 且到期)
// →HTTP POST→2xx 置 sent/失败指数退避/耗尽置 dead。单实例部署(与 consumer
// 同进程);多实例时批次 SELECT 无锁会重复推送(at-least-once,UPDATE CAS
// 保证状态机不乱,重复送达客户侧幂等兜底——与 CMPP Deliver 同语义)。
package callback
import (
"bytes"
"context"
"fmt"
"io"
"net/http"
"time"
"go-sms/internal/model/entity"
"go-sms/internal/pkg/logger"
"go-sms/internal/pkg/metrics"
"go.uber.org/zap"
"gorm.io/gorm"
)
// 推送参数默认值(HDD→SSD 后保守起步;S5 基线期按压测重标)。
const (
DefaultScanEvery = 2 * time.Second // 扫描周期
DefaultBatch = 100 // 单轮上限
DefaultConcurrency = 8 // 并发推送信号量
DefaultTimeout = 5 * time.Second // 单次 HTTP 超时
DefaultMaxAttempt = 10 // 重试上限(超过=死信)
DefaultMaxBackoff = 10 * time.Minute // 退避封顶
)
// PushWorker 回调推送器。
type PushWorker struct {
db *gorm.DB
client *http.Client
batch int
concurrency int
maxAttempt int
maxBackoff time.Duration
}
// NewPushWorker 构造(零值参数取默认)。
func NewPushWorker(db *gorm.DB) *PushWorker {
return &PushWorker{
db: db,
client: &http.Client{
Timeout: DefaultTimeout,
},
batch: DefaultBatch,
concurrency: DefaultConcurrency,
maxAttempt: DefaultMaxAttempt,
maxBackoff: DefaultMaxBackoff,
}
}
// Run 常驻循环(caller 起 goroutine;ctx 取消即退出,每轮独立 tick)。
func (w *PushWorker) Run(ctx context.Context, every time.Duration) {
if every <= 0 {
every = DefaultScanEvery
}
t := time.NewTicker(every)
defer t.Stop()
for {
select {
case <-ctx.Done():
return
case <-t.C:
w.Tick(ctx)
}
}
}
// Tick 单轮扫描推送(导出供测试/手动触发);返回本轮处理条数。
func (w *PushWorker) Tick(ctx context.Context) int {
var rows []entity.CallbackTask
if err := w.db.WithContext(ctx).
Where("status = ? AND next_retry_at <= ?", entity.CallbackPending, time.Now()).
Order("id ASC").Limit(w.batch).Find(&rows).Error; err != nil {
logger.L().Warn("callback_scan_failed", zap.Error(err))
return 0
}
sem := make(chan struct{}, w.concurrency)
done := make(chan struct{}, len(rows))
for i := range rows {
sem <- struct{}{}
go func(task entity.CallbackTask) {
defer func() { <-sem; done <- struct{}{} }()
w.pushOne(ctx, task)
}(rows[i])
}
for range rows {
<-done
}
return len(rows)
}
// pushOne 单条推送+状态机推进(CAS WHERE status=0:与 Resend/并发实例至多一次生效)。
func (w *PushWorker) pushOne(ctx context.Context, task entity.CallbackTask) {
typ := "report"
if task.Type == entity.CallbackTypeUplink {
typ = "uplink"
}
req, err := http.NewRequestWithContext(ctx, http.MethodPost, task.URL, bytes.NewReader([]byte(task.Payload)))
if err != nil {
w.markRetry(ctx, &task, 0, err.Error(), typ)
return
}
req.Header.Set("Content-Type", "application/json")
resp, err := w.client.Do(req)
if err != nil {
w.markRetry(ctx, &task, 0, truncate(err.Error(), 255), typ)
return
}
defer func() { _ = resp.Body.Close() }()
_, _ = io.Copy(io.Discard, io.LimitReader(resp.Body, 4<<10))
if resp.StatusCode >= 200 && resp.StatusCode < 300 {
res := w.db.WithContext(ctx).Model(&entity.CallbackTask{}).
Where("id = ? AND status = ?", task.ID, entity.CallbackPending).
Updates(map[string]any{
"status": entity.CallbackSent, "last_code": resp.StatusCode,
"last_err": "", "sent_at": time.Now(),
})
if res.Error != nil {
logger.L().Warn("callback_mark_sent_failed", zap.Int64("id", task.ID), zap.Error(res.Error))
}
if res.RowsAffected > 0 {
metrics.CallbackPushTotal.WithLabelValues(typ, "sent").Inc()
}
return
}
w.markRetry(ctx, &task, resp.StatusCode, fmt.Sprintf("http %d", resp.StatusCode), typ)
}
// markRetry 失败推进:attempt+1 → 耗尽置 dead,否则退避重排。
func (w *PushWorker) markRetry(ctx context.Context, task *entity.CallbackTask, httpCode int, msg, typ string) {
attempt := task.Attempt + 1
updates := map[string]any{
"attempt": attempt, "last_code": httpCode, "last_err": truncate(msg, 255),
}
if attempt >= w.maxAttempt {
updates["status"] = entity.CallbackDead
updates["next_retry_at"] = time.Now() // MySQL 不收 zero time;死信不扫靠 status
} else {
backoff := time.Duration(1<<uint(attempt)) * time.Second // 2^attempt s
if backoff > w.maxBackoff {
backoff = w.maxBackoff
}
updates["next_retry_at"] = time.Now().Add(backoff)
}
res := w.db.WithContext(ctx).Model(&entity.CallbackTask{}).
Where("id = ? AND status = ?", task.ID, entity.CallbackPending).
Updates(updates)
if res.Error != nil {
logger.L().Warn("callback_mark_retry_failed", zap.Int64("id", task.ID), zap.Error(res.Error))
return
}
if res.RowsAffected > 0 {
if attempt >= w.maxAttempt {
metrics.CallbackPushTotal.WithLabelValues(typ, "dead").Inc()
logger.L().Warn("callback_task_dead",
zap.Int64("id", task.ID), zap.Int64("account_id", task.AccountID),
zap.Int("attempt", attempt), zap.String("url", task.URL))
} else {
metrics.CallbackPushTotal.WithLabelValues(typ, "retry").Inc()
}
}
}
func truncate(s string, n int) string {
if r := []rune(s); len(r) > n {
return string(r[:n])
}
return s
}
+34
View File
@@ -26,9 +26,18 @@ type Consumer struct {
db *gorm.DB db *gorm.DB
bill *Billing // Day 57:计费确认(nil=跳过,Day56 零破坏) bill *Billing // Day 57:计费确认(nil=跳过,Day56 零破坏)
push PushSink // Day 61:CMPP Deliver 下发出口(nil=跳过) push PushSink // Day 61:CMPP Deliver 下发出口(nil=跳过)
cb ReportEnqueuer // S4·C1:HTTP 回调入队出口(nil=跳过)
requeue func(ev *Event) error // Day 69 三轮:unpaired 短重投出口(nil=直接按杂音丢弃) requeue func(ev *Event) error // Day 69 三轮:unpaired 短重投出口(nil=直接按杂音丢弃)
} }
// ReportEnqueuer 回执终态 HTTP 回调入队出口(S4·C1;装配层注入
// *callback.Service。终态首锁后调用,与 bill.Confirm 同为事务提交后动作;
// 内部失败由实现方/调用方 Warn 吞错——终态不回滚)。
type ReportEnqueuer interface {
EnqueueReport(ctx context.Context, detailID int64,
status int8, st, errCode, mobile string, now time.Time) error
}
// NewConsumer 构造。 // NewConsumer 构造。
func NewConsumer(db *gorm.DB) *Consumer { return &Consumer{db: db} } func NewConsumer(db *gorm.DB) *Consumer { return &Consumer{db: db} }
@@ -45,6 +54,25 @@ func (c *Consumer) WithPushSink(p PushSink) *Consumer {
return c return c
} }
// WithCallback 注入 HTTP 回调入队出口(S4·C1;链式,nil=跳过)。
func (c *Consumer) WithCallback(e ReportEnqueuer) *Consumer {
c.cb = e
return c
}
// enqueueReportLocked 终态首锁后的回调入队(吞错 Warn:推送失败不影响
// 已锁定的对账事实源——与 CMPP Deliver 下发同口径)。
func (c *Consumer) enqueueReportLocked(ctx context.Context, act *finAction, now time.Time) {
if c.cb == nil || act == nil {
return
}
if err := c.cb.EnqueueReport(ctx, act.detailID, act.status,
string(act.st), act.errCode, act.mobile, now); err != nil {
logger.L().Warn("callback_report_enqueue_failed",
zap.Int64("detail_id", act.detailID), zap.Error(err))
}
}
// WithRequeue 注入 unpaired 短重投出口(Day 69 三轮;链式,nil=不重投)。 // WithRequeue 注入 unpaired 短重投出口(Day 69 三轮;链式,nil=不重投)。
// 场景:秒回模拟器回执(push→kafka→消费,~10ms)可跑赢发送侧 seg 落库 // 场景:秒回模拟器回执(push→kafka→消费,~10ms)可跑赢发送侧 seg 落库
// (submit 往返+批量写,~20ms+)——合法回执 unpaired 若直接丢弃=detail // (submit 往返+批量写,~20ms+)——合法回执 unpaired 若直接丢弃=detail
@@ -121,6 +149,9 @@ func (c *Consumer) Process(ctx context.Context, ev *Event) error {
if locked && c.bill != nil { // 首到终态锁定且事务已提交:计费确认(内部吞错,方向=少退不少扣) if locked && c.bill != nil { // 首到终态锁定且事务已提交:计费确认(内部吞错,方向=少退不少扣)
c.bill.Confirm(ctx, act.detailID, act.status) c.bill.Confirm(ctx, act.detailID, act.status)
} }
if locked { // S4·C1:HTTP 回调入队(账号配置 report_url 才产生任务)
c.enqueueReportLocked(ctx, act, now)
}
if !paired { if !paired {
return c.handleUnpaired(ev) // Day 69 三轮:秒回竞态短重投,耗尽才按杂音丢弃 return c.handleUnpaired(ev) // Day 69 三轮:秒回竞态短重投,耗尽才按杂音丢弃
} }
@@ -188,6 +219,9 @@ func (c *Consumer) recheckPending(ctx context.Context, ev *Event, st Status, now
if locked && c.bill != nil { if locked && c.bill != nil {
c.bill.Confirm(ctx, act.detailID, act.status) c.bill.Confirm(ctx, act.detailID, act.status)
} }
if locked { // S4·C1:HTTP 回调入队(补刀终态同推)
c.enqueueReportLocked(ctx, act, now)
}
return nil return nil
} }
@@ -0,0 +1,62 @@
// consumer_callback_test.go S4·C1:回执终态首锁后的 HTTP 回调入队挂点
// (重复回执/已锁定不重复入队;挂点语义=与 bill.Confirm 同为事务后动作)。
package report
import (
"context"
"testing"
"time"
)
type capEnqueuer struct {
calls []capEnqCall
}
type capEnqCall struct {
detailID int64
status int8
}
func (e *capEnqueuer) EnqueueReport(_ context.Context, detailID int64,
status int8, st, errCode, mobile string, _ time.Time) error {
e.calls = append(e.calls, capEnqCall{detailID: detailID, status: status})
return nil
}
// TestConsumer_CallbackEnqueuedOnce 单段成功终态=入队一次;重复回执(终态
// 已锁定)=零重复——首锁幂等语义延伸到回调面。
func TestConsumer_CallbackEnqueuedOnce(t *testing.T) {
db := newConsumerDB(t)
enq := &capEnqueuer{}
c := NewConsumer(db).WithCallback(enq)
d := mkDetail(t, db, 1, "13800005555", 1)
mid := mkSeg(t, db, d.ID, 7, "13800005555", 1, 1)
if err := c.Process(testCtx, ev(7, mid, "DELIVRD", "13800005555")); err != nil {
t.Fatal(err)
}
if len(enq.calls) != 1 || enq.calls[0].detailID != d.ID || enq.calls[0].status != 2 {
t.Fatalf("首锁应入队一次: %+v", enq.calls)
}
// 同一回执重放:终态已锁定,跳过(零重复入队)。
if err := c.Process(testCtx, ev(7, mid, "DELIVRD", "13800005555")); err != nil {
t.Fatal(err)
}
if len(enq.calls) != 1 {
t.Fatalf("重复回执不应重复入队: %+v", enq.calls)
}
}
// TestConsumer_CallbackNilSink 注入 nil=零开销跳过(旧装配兼容)。
func TestConsumer_CallbackNilSink(t *testing.T) {
db := newConsumerDB(t)
c := NewConsumer(db) // 无 WithCallback
d := mkDetail(t, db, 1, "13800006666", 1)
mid := mkSeg(t, db, d.ID, 7, "13800006666", 1, 1)
if err := c.Process(testCtx, ev(7, mid, "UNDELIV", "13800006666")); err != nil {
t.Fatal(err)
}
if got := getDetail(t, db, d.ID); got.Status != 3 {
t.Fatalf("status=%d 期望 3", got.Status)
}
}
+15
View File
@@ -82,6 +82,7 @@ type PairService struct {
db *gorm.DB db *gorm.DB
bill *Billing bill *Billing
push PushSink push PushSink
cb ReportEnqueuer // S4·C1:HTTP 回调入队(nil=跳过)
sf *util.Snowflake // 重推受理号(雪花;D29 异步任务表落地后即任务主键) sf *util.Snowflake // 重推受理号(雪花;D29 异步任务表落地后即任务主键)
} }
@@ -96,6 +97,12 @@ func (p *PairService) WithPushSink(s PushSink) *PairService {
return p return p
} }
// WithCallback 注入 HTTP 回调入队出口(S4·C1;链式,nil=跳过)。
func (p *PairService) WithCallback(e ReportEnqueuer) *PairService {
p.cb = e
return p
}
// DB 裸查询出口(handler 回执分页 JOIN 查询复用同一连接)。 // DB 裸查询出口(handler 回执分页 JOIN 查询复用同一连接)。
func (p *PairService) DB() *gorm.DB { return p.db } func (p *PairService) DB() *gorm.DB { return p.db }
@@ -282,6 +289,14 @@ func (p *PairService) Pair(ctx context.Context, req PairRequest) (PairResult, er
if p.bill != nil { if p.bill != nil {
p.bill.Confirm(ctx, id, req.TargetStatus) p.bill.Confirm(ctx, id, req.TargetStatus)
} }
// S4·C1:HTTP 回调入队(与 Consumer 回执链同挂点;吞错 Warn)。
if p.cb != nil {
if err := p.cb.EnqueueReport(ctx, id, req.TargetStatus,
string(st), "MANUAL", d.Mobile, now); err != nil {
logger.L().Warn("callback_report_enqueue_failed",
zap.Int64("detail_id", id), zap.Error(err))
}
}
} else { } else {
res.Skipped++ // 圈中窗口与执行窗口间被回执/并发配对锁定 res.Skipped++ // 圈中窗口与执行窗口间被回执/并发配对锁定
} }
+2 -2
View File
@@ -81,7 +81,7 @@ func (s *Service) GenerateCustomerBills(ctx context.Context, period string) (*Ge
Amount: amount, Amount: amount,
} }
if err := s.db.WithContext(ctx).Clauses(clause.OnConflict{ if err := s.db.WithContext(ctx).Clauses(clause.OnConflict{
Columns: []clause.Column{{Name: "account_id"}, {Name: "period"}}, Columns: []clause.Column{{Name: "account_id"}, {Name: "period"}},
DoUpdates: clause.AssignmentColumns([]string{"settle_no", "success_count", "amount"}), DoUpdates: clause.AssignmentColumns([]string{"settle_no", "success_count", "amount"}),
}).Create(&bill).Error; err != nil { }).Create(&bill).Error; err != nil {
return nil, err return nil, err
@@ -166,7 +166,7 @@ func (s *Service) GenerateUpstreamBills(ctx context.Context, period string) (*Ge
Amount: amount, Amount: amount,
} }
if err := s.db.WithContext(ctx).Clauses(clause.OnConflict{ if err := s.db.WithContext(ctx).Clauses(clause.OnConflict{
Columns: []clause.Column{{Name: "upstream_id"}, {Name: "period"}}, Columns: []clause.Column{{Name: "upstream_id"}, {Name: "period"}},
DoUpdates: clause.AssignmentColumns([]string{"settle_no", "success_count", "amount"}), DoUpdates: clause.AssignmentColumns([]string{"settle_no", "success_count", "amount"}),
}).Create(&bill).Error; err != nil { }).Create(&bill).Error; err != nil {
return nil, err return nil, err
+6 -5
View File
@@ -1,11 +1,12 @@
// price.go S2-D4:生效价引擎——R1/R6 全链唯一「条→元」换算点。 // price.go S2-D4:生效价引擎——R1/R6 全链唯一「条→元」换算点。
// //
// 口径(D4-0 用户批准方案默认): // 口径(D4-0 用户批准方案默认):
// Q1 下家:金额=Σ(日成功条数×当日生效价);PriceAt=价格时间线 EffectiveAt≤当日 //
// 取最近;无价日按 0 元入账(账单明细如实体现,不阻断生成); // Q1 下家:金额=Σ(日成功条数×当日生效价);PriceAt=价格时间线 EffectiveAt≤当日
// Q2 阶梯:月成功总量落档后**整月单价按档位价覆写**(不做分档混合计价); // 取最近;无价日按 0 元入账(账单明细如实体现,不阻断生成);
// Q3 上家 all 档=submit_count(全部提交);success_unknown=成功+未知; // Q2 阶梯:月成功总量落档后**整月单价按档位价覆写**(不做分档混合计价);
// 空 billing_mode 按 success(保守)。 // Q3 上家 all 档=submit_count(全部提交);success_unknown=成功+未知;
// 空 billing_mode 按 success(保守)。
package settle package settle
import ( import (
+1
View File
@@ -15,6 +15,7 @@
// (返点从平台口径流出、计入上级账号口径); // (返点从平台口径流出、计入上级账号口径);
// - 通道侧:cost=Σ(日成功×PriceAtChannel);revenue=全平台用户收入×通道成功占比 // - 通道侧:cost=Σ(日成功×PriceAtChannel);revenue=全平台用户收入×通道成功占比
// (同分摊口径,与用户侧共用同一分母,Σ通道revenue=Σ用户revenue 恒成立)。 // (同分摊口径,与用户侧共用同一分母,Σ通道revenue=Σ用户revenue 恒成立)。
//
// 单位:金额均=元(R6;条→元换算仍只在 price.go)。 // 单位:金额均=元(R6;条→元换算仍只在 price.go)。
package settle package settle
+5 -5
View File
@@ -97,17 +97,17 @@ func (s *Service) CreateOrder(ctx context.Context, req *CreateOrderRequest) (*en
// ConfirmResult confirm 返回(新余额与流水号)。 // ConfirmResult confirm 返回(新余额与流水号)。
type ConfirmResult struct { type ConfirmResult struct {
Order *entity.BillingOrder Order *entity.BillingOrder
Balance int64 Balance int64
BalanceLogID int64 BalanceLogID int64
} }
// ConfirmOrder 付款审核确认(0→1):单事务=状态 CAS→余额自增 count→回读→流水。 // ConfirmOrder 付款审核确认(0→1):单事务=状态 CAS→余额自增 count→回读→流水。
func (s *Service) ConfirmOrder(ctx context.Context, orderID int64, operator string) (*ConfirmResult, error) { func (s *Service) ConfirmOrder(ctx context.Context, orderID int64, operator string) (*ConfirmResult, error) {
var ( var (
order entity.BillingOrder order entity.BillingOrder
balance int64 balance int64
balanceLog entity.AccountBalanceLog balanceLog entity.AccountBalanceLog
) )
err := s.db.WithContext(ctx).Transaction(func(tx *gorm.DB) error { err := s.db.WithContext(ctx).Transaction(func(tx *gorm.DB) error {
// ① 状态 CAS:非 0 即冲突(并发/重复/已驳回),余额零副作用。 // ① 状态 CAS:非 0 即冲突(并发/重复/已驳回),余额零副作用。
+6 -5
View File
@@ -10,6 +10,7 @@
// (Q4/Q6 快照语义:生成后换绑/改模式/重刷会如实暴露差异——这正是对账目的); // (Q4/Q6 快照语义:生成后换绑/改模式/重刷会如实暴露差异——这正是对账目的);
// - 金额不比:结算单金额是出账时点价/阶梯快照(Q6),事后调价重算必生噪音; // - 金额不比:结算单金额是出账时点价/阶梯快照(Q6),事后调价重算必生噪音;
// 金额核对走账单导出 XLSX 人工比对(D4 已验)。 // 金额核对走账单导出 XLSX 人工比对(D4 已验)。
//
// 注意:明细热窗仅 5 天(S4 分区规划)——历史月的"明细直查"侧在数据清退后 // 注意:明细热窗仅 5 天(S4 分区规划)——历史月的"明细直查"侧在数据清退后
// 不再完整,届时对账基线=日汇总 vs 结算单两方;当前 dev 明细全量在库。 // 不再完整,届时对账基线=日汇总 vs 结算单两方;当前 dev 明细全量在库。
package settle package settle
@@ -57,12 +58,12 @@ type UpstreamDiffRow struct {
// ReconcileReport 对账报告(AsOf 时点;rows 仅异常行,全对=空)。 // ReconcileReport 对账报告(AsOf 时点;rows 仅异常行,全对=空)。
type ReconcileReport struct { type ReconcileReport struct {
Period string `json:"period"` Period string `json:"period"`
AsOf time.Time `json:"as_of"` AsOf time.Time `json:"as_of"`
Accounts []AccountDiffRow `json:"accounts"` Accounts []AccountDiffRow `json:"accounts"`
Channels []ChannelDiffRow `json:"channels"` Channels []ChannelDiffRow `json:"channels"`
Upstreams []UpstreamDiffRow `json:"upstreams"` Upstreams []UpstreamDiffRow `json:"upstreams"`
Summary ReconcileSummary `json:"summary"` Summary ReconcileSummary `json:"summary"`
} }
// successAgg 明细直查聚合 DTO。 // successAgg 明细直查聚合 DTO。
+3 -2
View File
@@ -90,8 +90,9 @@ func TestReconcile_AllZero(t *testing.T) {
// TestReconcile_ExposesDiffs 篡改三处事实源逐项暴露: // TestReconcile_ExposesDiffs 篡改三处事实源逐项暴露:
// ①consume acc1 success 2→5 → AccountDiffs=1(明细2/汇总5/账单2 同行列出); // ①consume acc1 success 2→5 → AccountDiffs=1(明细2/汇总5/账单2 同行列出);
// ②stats ch1 success 2→9 → ChannelDiffs=1(明细2/日汇总9)+ 上家重推(9+1)+1=11 // ②stats ch1 success 2→9 → ChannelDiffs=1(明细2/日汇总9)+ 上家重推(9+1)+1=11
// vs 账单4 → UpstreamDiffs=1;③删 acc2 账单 → BillMissing=1 且 AllZero 仍真 //
// (在①②还原后单独验证:此处先验证 missing 计数,AllZero 断言只看 diffs)。 // vs 账单4 → UpstreamDiffs=1;③删 acc2 账单 → BillMissing=1 且 AllZero 仍真
// (在①②还原后单独验证:此处先验证 missing 计数,AllZero 断言只看 diffs)。
func TestReconcile_ExposesDiffs(t *testing.T) { func TestReconcile_ExposesDiffs(t *testing.T) {
db := newBillDB(t) db := newBillDB(t)
seedReconcile(t, db) seedReconcile(t, db)
+145
View File
@@ -0,0 +1,145 @@
// blacklist.go S4·C2:上行自动拉黑——回复「R/退订/拒收」等(词表 SystemConfig
// 可配,等值匹配)→ 写 policy_blacklist(source=2 上行拒收,账号级=归属账号)
// + Redis 黑名单缓存即时生效(拦截链检测 IN (0, account_id),下一单即拦)。
// 语义(需求 2.5/实施计划 5.7):拉黑不影响上行推送与 message_uplink 记录。
package uplink
import (
"context"
"strings"
"sync"
"time"
"go-sms/internal/model/entity"
"go-sms/internal/pkg/logger"
"go-sms/internal/pkg/metrics"
"go-sms/internal/service/interceptor"
"go.uber.org/zap"
"gorm.io/gorm"
"gorm.io/gorm/clause"
"github.com/redis/go-redis/v9"
)
// CfgKeyBlacklistWords 拉黑词表 SystemConfig 键(逗号分隔;等值匹配,
// 大小写不敏感;未配置=默认词表)。
const CfgKeyBlacklistWords = "uplink.blacklist.keywords"
// DefaultBlacklistWords 默认拉黑词表(需求原文「回复"拒收"等」+通用退订惯例)。
var DefaultBlacklistWords = []string{"R", "TD", "退订", "拒收"}
// defaultReloadEvery 词表刷新周期(运营改配置最迟生效窗口)。
const defaultReloadEvery = 60 * time.Second
// AutoBlacklister 上行自动拉黑器(词表内存缓存+定时刷新)。
type AutoBlacklister struct {
db *gorm.DB
rdb redis.UniversalClient
mu sync.RWMutex
words map[string]struct{} // lower(trim(word))
}
// NewAutoBlacklister 构造(首次 Reload 失败仅记日志,用默认词表)。
func NewAutoBlacklister(db *gorm.DB, rdb redis.UniversalClient) *AutoBlacklister {
b := &AutoBlacklister{db: db, rdb: rdb, words: lowerSet(DefaultBlacklistWords)}
if err := b.Reload(context.Background()); err != nil {
logger.L().Warn("uplink_blacklist_reload_failed_use_default", zap.Error(err))
}
return b
}
func lowerSet(words []string) map[string]struct{} {
m := make(map[string]struct{}, len(words))
for _, w := range words {
if w = strings.ToLower(strings.TrimSpace(w)); w != "" {
m[w] = struct{}{}
}
}
return m
}
// Reload 从 SystemConfig 重载词表(空/缺省行=默认词表)。
func (b *AutoBlacklister) Reload(ctx context.Context) error {
var row entity.SystemConfig
err := b.db.WithContext(ctx).
Where("cfg_key = ?", CfgKeyBlacklistWords).First(&row).Error
if err != nil {
if err == gorm.ErrRecordNotFound {
b.mu.Lock()
b.words = lowerSet(DefaultBlacklistWords)
b.mu.Unlock()
return nil
}
return err
}
parts := strings.Split(row.CfgValue, ",")
words := lowerSet(parts)
if len(words) == 0 {
words = lowerSet(DefaultBlacklistWords)
}
b.mu.Lock()
b.words = words
b.mu.Unlock()
return nil
}
// Start 定时刷新(caller 起 goroutine;ctx 取消即退出)。
func (b *AutoBlacklister) Start(ctx context.Context) {
t := time.NewTicker(defaultReloadEvery)
defer t.Stop()
for {
select {
case <-ctx.Done():
return
case <-t.C:
if err := b.Reload(ctx); err != nil {
logger.L().Warn("uplink_blacklist_reload_failed", zap.Error(err))
}
}
}
}
// hit 内容等值命中(trim+lower;非前缀/包含,防正常回复误伤)。
func (b *AutoBlacklister) hit(content string) bool {
w := strings.ToLower(strings.TrimSpace(content))
if w == "" {
return false
}
b.mu.RLock()
defer b.mu.RUnlock()
_, ok := b.words[w]
return ok
}
// MaybeBlock 拉黑判定+落库:命中词表→INSERT policy_blacklist(uk 幂等,
// OnConflict DoNothing)→ Redis 缓存即时生效。返回是否新增(已存在=幂等跳过)。
func (b *AutoBlacklister) MaybeBlock(ctx context.Context, accountID int64, mobile, content string) bool {
if !b.hit(content) {
return false
}
row := entity.PolicyBlacklist{
Mobile: mobile, Source: 2, AccountID: accountID, Operator: "uplink-auto",
}
res := b.db.WithContext(ctx).Clauses(clause.OnConflict{DoNothing: true}).
Create(&row)
if res.Error != nil {
logger.L().Warn("uplink_blacklist_insert_failed",
zap.Int64("account_id", accountID), zap.String("mobile", mobile), zap.Error(res.Error))
return false
}
if res.RowsAffected == 0 {
return false // 已在黑名单(手工/先前拉黑):幂等跳过
}
metrics.CallbackBlacklistTotal.Inc()
if b.rdb != nil {
if err := interceptor.AddBlacklistCache(ctx, b.rdb, mobile); err != nil {
logger.L().Warn("uplink_blacklist_cache_add_failed", zap.String("mobile", mobile), zap.Error(err))
}
}
logger.L().Info("uplink_blacklist_added",
zap.Int64("account_id", accountID), zap.String("mobile", mobile),
zap.String("content", content))
return true
}
+158
View File
@@ -0,0 +1,158 @@
// blacklist_test.go S4·C2:上行自动拉黑——词表匹配/SystemConfig 可配/
// 落库幂等/Redis 缓存即时生效。
package uplink
import (
"context"
"encoding/json"
"testing"
"go-sms/internal/model/entity"
"go-sms/internal/service/interceptor"
"github.com/alicebob/miniredis/v2"
"github.com/redis/go-redis/v9"
"gorm.io/gorm"
)
func newBlacklistDB(t *testing.T) *gorm.DB {
t.Helper()
db := newTestDB(t)
if err := db.AutoMigrate(&entity.PolicyBlacklist{}, &entity.SystemConfig{}); err != nil {
t.Fatalf("AutoMigrate 失败: %v", err)
}
return db
}
func mustJSON(t *testing.T, v any) []byte {
t.Helper()
b, err := json.Marshal(v)
if err != nil {
t.Fatal(err)
}
return b
}
// TestBlacklistHit 词表等值匹配:R/r/退订/拒收 命中;含字/空/正常回复不误伤。
func TestBlacklistHit(t *testing.T) {
db := newBlacklistDB(t)
b := NewAutoBlacklister(db, nil)
cases := []struct {
content string
want bool
}{
{"R", true}, {"r", true}, {" R ", true},
{"TD", true}, {"退订", true}, {"拒收", true},
{"我要退订", false}, {"请别再发了", false}, {"", false}, {"RR", false},
}
for _, c := range cases {
if got := b.hit(c.content); got != c.want {
t.Fatalf("hit(%q)=%v 期望 %v", c.content, got, c.want)
}
}
}
// TestBlacklistReload SystemConfig 词表覆盖默认(等值;去掉 R 只认「回T」)。
func TestBlacklistReload(t *testing.T) {
db := newBlacklistDB(t)
db.Create(&entity.SystemConfig{CfgKey: CfgKeyBlacklistWords, CfgValue: "回T, N"})
b := NewAutoBlacklister(db, nil)
if err := b.Reload(context.Background()); err != nil {
t.Fatal(err)
}
if !b.hit("回T") || !b.hit("n") {
t.Fatal("自定义词表未生效")
}
if b.hit("R") {
t.Fatal("自定义词表应替换默认(R 不应命中)")
}
// 空配置回落默认词表。
db.Model(&entity.SystemConfig{}).Where("cfg_key = ?", CfgKeyBlacklistWords).
Update("cfg_value", " , ")
if err := b.Reload(context.Background()); err != nil {
t.Fatal(err)
}
if !b.hit("R") {
t.Fatal("空配置应回落默认词表")
}
}
// TestMaybeBlockIdempotent 拉黑落库幂等+Redis 缓存即时生效(uk 冲突跳过)。
func TestMaybeBlockIdempotent(t *testing.T) {
db := newBlacklistDB(t)
mr := miniredis.RunT(t)
rdb := redis.NewClient(&redis.Options{Addr: mr.Addr()})
b := NewAutoBlacklister(db, rdb)
if !b.MaybeBlock(context.Background(), 5, "13800001111", "R") {
t.Fatal("首次拉黑应返回 true")
}
if b.MaybeBlock(context.Background(), 5, "13800001111", "R") {
t.Fatal("重复拉黑应幂等跳过(false)")
}
var rows []entity.PolicyBlacklist
db.Find(&rows)
if len(rows) != 1 || rows[0].Source != 2 || rows[0].AccountID != 5 || rows[0].Operator != "uplink-auto" {
t.Fatalf("拉黑行异常: %+v", rows)
}
n, err := rdb.SIsMember(context.Background(), interceptor.BlacklistCacheKey, "13800001111").Result()
if err != nil || !n {
t.Fatalf("Redis 黑名单缓存未生效: n=%v err=%v", n, err)
}
// 非拉黑词:零副作用。
if b.MaybeBlock(context.Background(), 5, "13800002222", "收到谢谢") {
t.Fatal("非拉黑词不应触发")
}
var cnt int64
db.Model(&entity.PolicyBlacklist{}).Count(&cnt)
if cnt != 1 {
t.Fatalf("不应新增行, got %d", cnt)
}
}
// TestConsumerBlacklistAndPush 消费链挂点:拒收词→拉黑(不影响推送)+ 推送入队。
func TestConsumerBlacklistAndPush(t *testing.T) {
db := newBlacklistDB(t)
if err := db.AutoMigrate(&entity.CallbackTask{}, &entity.AccountConfig{}); err != nil {
t.Fatalf("AutoMigrate 失败: %v", err)
}
db.Create(&entity.AccountConfig{AccountID: 5, Business: "sms", UplinkURL: "http://cb/mo"})
b := NewAutoBlacklister(db, nil)
// 推送出口打桩:捕获上行载荷。
got := make(chan string, 1)
c := NewConsumer(db).WithBlacklist(b).WithCallback(stubUplinkEnq{got})
payload := mustJSON(t, Event{
UplinkID: 9, DestCode: "10690", SrcMobile: "13900001111",
Content: "R", AccountID: 5, ChannelID: 7,
RecvAt: "2026-11-16T10:00:00+08:00",
})
if err := c.HandlePayload(context.Background(), payload); err != nil {
t.Fatal(err)
}
// 拉黑生效(source=2)。
var bl entity.PolicyBlacklist
if err := db.Where("mobile = ?", "13900001111").First(&bl).Error; err != nil {
t.Fatalf("拒收未拉黑: %v", err)
}
if bl.Source != 2 || bl.AccountID != 5 {
t.Fatalf("拉黑行异常: %+v", bl)
}
// 推送不因拉黑中断。
select {
case p := <-got:
if p == "" {
t.Fatal("推送载荷为空")
}
default:
t.Fatal("上行推送未触发")
}
}
type stubUplinkEnq struct{ ch chan string }
func (s stubUplinkEnq) EnqueueUplink(_ context.Context, uplinkID, accountID int64,
destCode, srcMobile, content, recvAt string) error {
s.ch <- srcMobile + ":" + content
return nil
}
+42 -2
View File
@@ -1,6 +1,8 @@
// consumer.go Day 49:sms-up 消费者——15 天窗口匹配下行明细,回填 // consumer.go Day 49:sms-up 消费者——15 天窗口匹配下行明细,回填
// message_uplink.match_task_id(需求 2.5"只匹配 15 天内发送数据")。 // message_uplink.match_task_id(需求 2.5"只匹配 15 天内发送数据")。
// 客户推送(CMPP 优先/HTTP,uplink_url)与上行自动拉黑属阶段 5(Day 54)。 // S4 落地:C1 上行推送(WithCallback→callback.Service,账号 uplink_url)+
// C2 自动拉黑(WithBlacklist→AutoBlacklister,blacklist.go);两者均不
// 阻断匹配回填主链,失败 Warn 吞错。
package uplink package uplink
import ( import (
@@ -9,28 +11,66 @@ import (
"time" "time"
"go-sms/internal/model/entity" "go-sms/internal/model/entity"
"go-sms/internal/pkg/logger"
"go.uber.org/zap"
"gorm.io/gorm" "gorm.io/gorm"
) )
// MatchWindow 上行↔下行匹配窗口(15 天,需求 2.5)。 // MatchWindow 上行↔下行匹配窗口(15 天,需求 2.5)。
const MatchWindow = 15 * 24 * time.Hour const MatchWindow = 15 * 24 * time.Hour
// UplinkEnqueuer 上行 HTTP 回调入队出口(S4·C1;装配层注入 *callback.Service)。
type UplinkEnqueuer interface {
EnqueueUplink(ctx context.Context, uplinkID, accountID int64,
destCode, srcMobile, content, recvAt string) error
}
// Consumer sms-up 消费者。 // Consumer sms-up 消费者。
type Consumer struct { type Consumer struct {
db *gorm.DB db *gorm.DB
cb UplinkEnqueuer // S4·C1 上行推送(nil=跳过)
bl *AutoBlacklister // S4·C2 自动拉黑(nil=跳过)
} }
// NewConsumer 构造。 // NewConsumer 构造。
func NewConsumer(db *gorm.DB) *Consumer { return &Consumer{db: db} } func NewConsumer(db *gorm.DB) *Consumer { return &Consumer{db: db} }
// WithCallback 注入上行推送出口(链式,nil=跳过)。
func (c *Consumer) WithCallback(e UplinkEnqueuer) *Consumer {
c.cb = e
return c
}
// WithBlacklist 注入自动拉黑器(链式,nil=跳过)。
func (c *Consumer) WithBlacklist(b *AutoBlacklister) *Consumer {
c.bl = b
return c
}
// HandlePayload 消费入口(Kafka handler;载荷即 Event JSON)。 // HandlePayload 消费入口(Kafka handler;载荷即 Event JSON)。
func (c *Consumer) HandlePayload(_ context.Context, value []byte) error { func (c *Consumer) HandlePayload(_ context.Context, value []byte) error {
var ev Event var ev Event
if err := json.Unmarshal(value, &ev); err != nil { if err := json.Unmarshal(value, &ev); err != nil {
return err // 非法载荷交由消费组重试→死信(与 submit 消费者同口径) return err // 非法载荷交由消费组重试→死信(与 submit 消费者同口径)
} }
return c.MatchAndFill(context.Background(), &ev) ctx := context.Background()
if err := c.MatchAndFill(ctx, &ev); err != nil {
return err
}
// S4·C2:拒收即拉黑(先于推送;拉黑不影响推送——需求原文语义)。
if c.bl != nil {
c.bl.MaybeBlock(ctx, ev.AccountID, ev.SrcMobile, ev.Content)
}
// S4·C1:上行推送(未配置 uplink_url 的账号零任务)。
if c.cb != nil {
if err := c.cb.EnqueueUplink(ctx, ev.UplinkID, ev.AccountID,
ev.DestCode, ev.SrcMobile, ev.Content, ev.RecvAt); err != nil {
logger.L().Warn("callback_uplink_enqueue_failed",
zap.Int64("uplink_id", ev.UplinkID), zap.Error(err))
}
}
return nil
} }
// MatchAndFill 匹配并回填:同账号+同手机号、recv_at 前 15 天窗口内最近一条 // MatchAndFill 匹配并回填:同账号+同手机号、recv_at 前 15 天窗口内最近一条
+7 -8
View File
@@ -1,15 +1,14 @@
# 当前工作上下文(最近更新:2026-11-15) # 当前工作上下文(最近更新:2026-11-16)
## 当前状态快照(2026-11-15 凌晨,**S3 泳道收官,一期 15/27 计划日**) ## 当前状态快照(2026-11-16,**S4 D1/D2 完成,一期 17/27 计划日**)
- **仓库**:main 已推 Gitea;每日 bundle+push 双轨。 - **仓库**:main 已推 Gitea;每日 bundle+push 双轨。
- **一期进度**:S0✅ S1✅ S2✅(11-14 深夜③,六项裁决 R1~R6 全闭环,出口=结算单测+对账差异 0 全过)/ **S3 ✅ 前端收尾**(11-15 凌晨)——**盘点修正:views 31 页 S1-B 已搬零残留**(归档仓逐文件比对);实际交付=FE-D34 利润分析页(/stats/profit 双 Tab+汇总条+口径诚实标注)+FE-6 四件(Mock 浏览器通道下线/msw 仅存 vitest 层、五角色×33 菜单固化矩阵快照、manualChunks 拆包全 chunk gzip<500KB[业务 entry ~19KB+EP 341KB+vue-vendor 62KB]、ci.yml web-test job+web/Dockerfile nginx 多阶段+deploy/k8s/web.yaml NodePort 30081 备档);vitest 148 全绿。**两项环境受阻如实标注:LCP 真浏览器实测不可执行(本机 chromium newPage 即 Target crashed 3 轮全崩,dmesg 无痕)、kind 未装(manifests 随 S5 apply)**。 - **一期进度**:S0✅ S1✅ S2✅ / S3 ✅(11-15 凌晨)/ **S4-D1/D2 ✅(11-16)**——C1 回调推送引擎(callback_task v21+PushWorker 指数退避/死信可查可重派+双挂点 report 终态/uplink 消费+GET /ops/callback 管理面+前端回调管理页 ops-callback+metrics)+ C2 上行自动拉黑(等值词表 SystemConfig 可配→policy_blacklist source=2+Redis 即时生效;拉黑不影响推送);**真机全链六步实证**(回执→HTTP 回调 JSON 送达/上行 R→拉黑+推送/坏 URL→死信→resend→重投 sent/状态面板);门禁全绿(-race 40 包/vitest 148/lint 0);验收单 s4-d1d2-callback.md。**S4 剩余=D3 明细按天分区热 5 天+ClickHouse 冷热切换+冷查<1s**(动核心大表,方案先行)。
- **下一步=S4 C1/C2+冷热(3 日)**:C1 回调推送引擎补强(report 基座已在)/C2 拉黑(拒收即拉黑)/ClickHouse 冷热切换/**明细按天分区热 5 天**(分区落地后 settle-check 对账基线自动=日汇总↔结算单两方,reconcile.go 头注已预声明)。 - **C1/C2 口径留痕**:载荷无签名(挂账);拦截终态 HTTP 回调未挂(CMPP 侧 blockedDeliverAdapter 已有);PushWorker 单实例语义(多实例 at-least-once);退避 2^n s 封顶 10min×10 次(S5 重标);casbin 策略=ops callback:read/write(v21 所种,SQL 直插 g 重启生效老规矩)。
- **⚠️ 流程补正在途(两项,不阻塞开工)**:①S2 用户本人走查(s2-walkthrough.sh D2~D5,AI 预走查 27 PASS);②S2 结算页+S3 利润页浏览器走查(:5173,s2demo 登录)。**casbin 留痕:SQL 直插 g 规则需重启 gateway 生效**。 - **⚠️ 流程补正在途(两项,不阻塞开工)**:①S2 用户本人走查(s2-walkthrough.sh D2~D5,AI 预走查 27 PASS);②S2 结算页+S3 利润页+**S4 回调管理页**浏览器走查(:5173,s2demo 登录[admin 角色];回调页在「系统管理→回调管理」)。
- **24h 稳定性门槛 ✅ 终判通过**(S5 压测前置确认)。 - **24h 稳定性门槛 ✅ 终判通过**(S5 压测前置;S4-D3 动大表后建议 S5 前复核 dmesg)。
- **真浏览器通道就位(2026-11-15)**:Google Chrome 157 官方 deb 直装 + Playwright `channel:'chrome'` 实测可用(`scripts/chrome-session.mjs` 通用会话工具,单 page 会话铁律见 techContext 工具链)——**参考短信平台登录参考 UI + AI 前端自验通道已打通**;注意本 AI 会话不支持读图,自验交付口径=截图供用户复核 + DOM/CSS/accessibility 结构化提取由 AI 分析;账号一律 env 临时传入不落盘。 - **真浏览器通道就位(2026-11-15)**:Google Chrome 157 官方 deb 直装 + Playwright `channel:'chrome'` 实测可用(`scripts/chrome-session.mjs` 通用会话工具,单 page 会话铁律见 techContext 工具链)——**参考短信平台登录参考 UI + AI 前端自验通道已打通**;注意本 AI 会话不支持读图,自验交付口径=截图供用户复核 + DOM/CSS/accessibility 结构化提取由 AI 分析;账号一律 env 临时传入不落盘。
- **Issue#1 第一轮:调研+新建页重构(2026-11-15)**:盈讯通(sms.inscom.ltd,Ant Design Vue)登录调研完成——**计算题图片验证码 OCR 方案可行**(tesseract 5.5+PIL 放大4x+Otsu 二值化+多 psm 投票+三态算式解析,脚本 /tmp/cap-solve.py;注意该平台登录 toast「登录成功」后跳转慢须 waitForURL≥9s)。调研结论:短信通道列表 `/gateway/sms/list`(7 列双语义聚合)、新增通道 `/gateway/sms/detail/new`(编号 5 组 collapse,33 字段,表单 1500/1760 无上限,label 135px)。首轮重构 ChannelEdit:修 `>>` 垃圾字符、去 max-width:860px→100%、编号分区 01-06、粘性玻璃底栏。 - **Issue#1 两轮交付均已入库(2026-11-15/16)**:盈讯通调研+ChannelList 8 列双语义聚合+ChannelEdit 分组深度对齐+chrome-session 工具;工单评论仍待用户贴 Gitea(或给 token)。
- **Issue#1 第二轮对齐交付(2026-11-15)**:用户反馈「列表未重构/新建页只改了宽度」后深度对齐——①`ChannelList.vue` 20 列→**8 列双语义聚合**(ID/运营商·通道名称[名称+协议tag+兜底tag/运营商tags]/企业ID·速度[sp_id/单速×连接=总速]/接入码·地址/上家/优先级·限额[P值·权重/日限·夜间禁发]/状态switch/操作),删 7 低频列(签名模式/扩展码长/模板ID/黑关键字/屏蔽地区/备注/创建时间,信息仍在编辑页与回执抽屉);夜间>0 语义修正显示「禁发」;script 增 `carrierLabels()`(CARRIERS 码值→中文)。②`ChannelEdit.vue` 分组深度对齐:通道号(接入码)从 02基本信息→**04通道配置**(密码后,在 v-if http 条件块外——http 通道也有接入码)、02 组内顺序对齐参考平台(名称/运营商/计费长度/付款/计费/价格/上家/分类/状态/上行组合/兜底归左尾)、行式交替排布。门禁四绿(eslint 0 警/tsc/vitest 148/build);Chrome 实机 VERIFY_PASS(截图 /tmp/verify-list-v2.png、verify-edit-v2.png)。走查登录口径:shot.mjs 的 admin/admin123 已失效,**s2demo/S2demo@2026** 可用。工单评论待用户给 Gitea token 或手动贴文案。
- **六项裁决速查(S2 全闭环)**:R1/R6 换算只在 price.go / R2 事实源 D2 落账 / R3 双通道 D3 落地(ref_id 区分)/ R4 rebate_rate D5 落地(profit.go 现算不落档)/ R5 settle_ladder D4 落地。 - **六项裁决速查(S2 全闭环)**:R1/R6 换算只在 price.go / R2 事实源 D2 落账 / R3 双通道 D3 落地(ref_id 区分)/ R4 rebate_rate D5 落地(profit.go 现算不落档)/ R5 settle_ladder D4 落地。
- 归档仓(只读):/data/poc-archive/repos/go-sms.git。 - 归档仓(只读):/data/poc-archive/repos/go-sms.git。
- **【铁律】测试端口固定=5173**:Web 测试/联调/走查/对外验收口径**一律** `http://<服务器IP>:5173`(vite dev,`make web-dev`);**禁止**用 WEB_PORT 覆盖到其他端口、禁止 preview(4173 等)/临时容器端口(30099 等)作为验收与走查入口——一次性探测端口仅限本机调试且不入文档口径(2026-11-15 用户裁定)。 - **【铁律】测试端口固定=5173**:Web 测试/联调/走查/对外验收口径**一律** `http://<服务器IP>:5173`(vite dev,`make web-dev`);**禁止**用 WEB_PORT 覆盖到其他端口、禁止 preview(4173 等)/临时容器端口(30099 等)作为验收与走查入口——一次性探测端口仅限本机调试且不入文档口径(2026-11-15 用户裁定)。
+3 -1
View File
@@ -19,8 +19,10 @@
| S2-D5 利润/返点/对账 | profit.go(时间线价收入+成本分摊+R4 返点现算+子树过滤;通道利润守恒分摊)+reconcile.go(三方对账,金额不比,missing 不判失败)+settle-check 子命令+/stats/profit 双端点;**真机 all_zero=true EXIT=0=S2 出口判据**;走查脚本扩 D2~D5 全域 27 PASS | ✅ 2026-11-14 深夜③(用户走查在途;s2-d5.md) | | S2-D5 利润/返点/对账 | profit.go(时间线价收入+成本分摊+R4 返点现算+子树过滤;通道利润守恒分摊)+reconcile.go(三方对账,金额不比,missing 不判失败)+settle-check 子命令+/stats/profit 双端点;**真机 all_zero=true EXIT=0=S2 出口判据**;走查脚本扩 D2~D5 全域 27 PASS | ✅ 2026-11-14 深夜③(用户走查在途;s2-d5.md) |
| S2 泳道收官 | 出口两项(结算单测+对账差异 0)全过;六项裁决 R1~R6 全闭环 | ✅ 2026-11-14 深夜③(判定单 s2-closeout.md;用户走查在途补正) | | S2 泳道收官 | 出口两项(结算单测+对账差异 0)全过;六项裁决 R1~R6 全闭环 | ✅ 2026-11-14 深夜③(判定单 s2-closeout.md;用户走查在途补正) |
| S3 前端收尾 | 盘点修正:views 31 页已在位零残留;FE-D34 利润页(/stats/profit)+FE-6 四件(Mock 浏览器通道下线/五角色×33 菜单矩阵快照/拆包全 chunk gzip<500KB/ci.yml web job+Dockerfile nginx+NodePort 30081 备档);vitest 148 全绿;LCP 实测与 kind 两项环境受阻如实标注 | ✅ 2026-11-15 凌晨(s3-closeout.md;页面走查在途) | | S3 前端收尾 | 盘点修正:views 31 页已在位零残留;FE-D34 利润页(/stats/profit)+FE-6 四件(Mock 浏览器通道下线/五角色×33 菜单矩阵快照/拆包全 chunk gzip<500KB/ci.yml web job+Dockerfile nginx+NodePort 30081 备档);vitest 148 全绿;LCP 实测与 kind 两项环境受阻如实标注 | ✅ 2026-11-15 凌晨(s3-closeout.md;页面走查在途) |
| S4-D1/D2 C1 回调推送+C2 拉黑 | callback_task(v21)+PushWorker(指数退避/死信/重派)+双挂点(report 终态 Consumer/PairService+uplink 消费)+GET /ops/callback 管理面+前端回调管理页;C2 等值词表可配拉黑+Redis 即时生效;真机全链六步实证;**死信可查/拒收即拉黑两项验收 ✅** | ✅ 2026-11-16(s4-d1d2-callback.md) |
| S4-D3 分区+冷热 | 明细按天分区热 5 天(v17 口径上 DDL 改造,方案先行)+CH 冷热切换(5 天前迁出)+record/history 冷查<1s+settle-check 对账基线自动切换验证 | ⬜ 下一步 |
| 观察项 | 24h 稳定性门槛(11-13 17:55 起计;期内 21:33 宿主主动重启=已知事件不判失败)期满核验 11-14 17:55 | ✅ 2026-11-14 18:02 终判通过(dmesg 0/零重启/驻留+md5 一致;S5 压测前置确认,S1 完全闭环) | | 观察项 | 24h 稳定性门槛(11-13 17:55 起计;期内 21:33 宿主主动重启=已知事件不判失败)期满核验 11-14 17:55 | ✅ 2026-11-14 18:02 终判通过(dmesg 0/零重启/驻留+md5 一致;S5 压测前置确认,S1 完全闭环) |
| S2~S5 | 结算域/前端收尾/C1C2+冷热/基线与灰度 | ⬜ 见《Go-sms-重建计划.md》 | | S2~S5 | 结算域/前端收尾/C1C2+冷热/基线与灰度 | 🔄 S2✅S3✅S4 D1/D2✅(见《Go-sms-重建计划.md》) |
## 阶段总览(8 阶段 / 80 工作日) ## 阶段总览(8 阶段 / 80 工作日)
+34
View File
@@ -0,0 +1,34 @@
/** 回调任务管理 API(S4·C1;openapi v1.4.0 契约)。 */
import { get, post } from './request'
/** 回调任务(status: 0待推送 1已送达 2死信;type: 1回执终态 2上行)。 */
export interface CallbackTask {
id: number
account_id: number
type: number
url: string
payload: string
status: number
attempt: number
next_retry_at: string
last_code: number
last_err: string
sent_at: string | null
created_at: string
}
/** 列表结果(含状态计数面板)。 */
export interface CallbackListResult {
total: number
list: CallbackTask[]
status_counts: { pending?: number; sent?: number; dead?: number }
}
export const callbackApi = {
list: (params: Record<string, unknown>) => get<CallbackListResult>('/ops/callback', params),
resend: (id: number) => post<{ id: number; resent: boolean }>(`/ops/callback/${id}/resend`),
}
/** 状态码值 → 中文标签。 */
export const CALLBACK_STATUS: Record<number, string> = { 0: '待推送', 1: '已送达', 2: '死信' }
export const CALLBACK_TYPE: Record<number, string> = { 1: '回执终态', 2: '上行消息' }
+2
View File
@@ -71,6 +71,8 @@ export const asyncRoutes: RouteRecordRaw[] = [
{ path: 'stats/channel', name: 'stats-channel', component: () => import('@/views/coming/ComingSoon.vue'), meta: { title: '通道统计', icon: 'TrendCharts', menu: true, group: '统计结算', roles: ['admin', 'ops'] } }, { path: 'stats/channel', name: 'stats-channel', component: () => import('@/views/coming/ComingSoon.vue'), meta: { title: '通道统计', icon: 'TrendCharts', menu: true, group: '统计结算', roles: ['admin', 'ops'] } },
{ path: 'stats/profit', name: 'stats-profit', component: () => import('@/views/stats/profit/index.vue'), meta: { title: '利润分析', icon: 'Coin', menu: true, group: '统计结算', roles: ['admin', 'ops', 'service'], subtitle: '用户利润(含下级返点)· 通道利润 · 平台净利' } }, { path: 'stats/profit', name: 'stats-profit', component: () => import('@/views/stats/profit/index.vue'), meta: { title: '利润分析', icon: 'Coin', menu: true, group: '统计结算', roles: ['admin', 'ops', 'service'], subtitle: '用户利润(含下级返点)· 通道利润 · 平台净利' } },
{ path: 'settle', name: 'settle', component: () => import('@/views/settle/index.vue'), meta: { title: '结算管理', icon: 'Wallet', menu: true, group: '统计结算', roles: ['admin', 'ops'] } }, { path: 'settle', name: 'settle', component: () => import('@/views/settle/index.vue'), meta: { title: '结算管理', icon: 'Wallet', menu: true, group: '统计结算', roles: ['admin', 'ops'] } },
{ path: 'ops/callback', name: 'ops-callback', component: () => import('@/views/ops/callback/index.vue'), meta: { title: '回调管理', icon: 'Promotion', menu: true, group: '系统管理', roles: ['admin', 'ops'], subtitle: '客户回调推送任务 · 指数退避 · 死信可查可重派' } },
{ path: 'logs', name: 'logs', component: () => import('@/views/coming/ComingSoon.vue'), meta: { title: '日志报表', icon: 'Document', menu: true, group: '系统管理', roles: ['admin', 'ops', 'service'] } }, { path: 'logs', name: 'logs', component: () => import('@/views/coming/ComingSoon.vue'), meta: { title: '日志报表', icon: 'Document', menu: true, group: '系统管理', roles: ['admin', 'ops', 'service'] } },
{ path: 'system', name: 'system', component: () => import('@/views/coming/ComingSoon.vue'), meta: { title: '系统配置', icon: 'Setting', menu: true, group: '系统管理', roles: ['admin'] } }, { path: 'system', name: 'system', component: () => import('@/views/coming/ComingSoon.vue'), meta: { title: '系统配置', icon: 'Setting', menu: true, group: '系统管理', roles: ['admin'] } },
+130
View File
@@ -0,0 +1,130 @@
<template>
<div class="page-stack">
<section class="card cb-card">
<div class="cb-head">
<div>
<span class="card-head-title">回调管理</span>
<el-tag size="small" type="warning" class="cb-badge">S4·C1 验收版</el-tag>
</div>
<span class="cb-sub">客户回调推送任务(回执终态/上行)· 指数退避重试 · 死信可查可重派</span>
</div>
<!-- 数据脉象:状态计数面板(死信存量一眼可见) -->
<div class="cb-counts">
<div class="cb-count"><span class="cb-num">{{ counts.pending ?? 0 }}</span><span class="cb-label">待推送/重试中</span></div>
<div class="cb-count"><span class="cb-num cb-ok">{{ counts.sent ?? 0 }}</span><span class="cb-label">已送达</span></div>
<div class="cb-count"><span class="cb-num" :class="{ 'cb-dead': (counts.dead ?? 0) > 0 }">{{ counts.dead ?? 0 }}</span><span class="cb-label">死信(重试耗尽)</span></div>
</div>
<div class="cb-filters">
<el-select v-model="q.status" placeholder="状态(全部)" clearable style="width: 140px" @change="load">
<el-option label="待推送" :value="0" /><el-option label="已送达" :value="1" /><el-option label="死信" :value="2" />
</el-select>
<el-select v-model="q.type" placeholder="类型(全部)" clearable style="width: 130px" @change="load">
<el-option label="回执终态" :value="1" /><el-option label="上行消息" :value="2" />
</el-select>
<el-input-number v-model="q.account_id" :min="0" placeholder="账号ID" :controls="false" style="width: 110px" @change="load" />
<el-button type="primary" :loading="loading" @click="load">查询</el-button>
<el-button @click="showPayload = !showPayload">{{ showPayload ? '隐藏载荷' : '展开载荷' }}</el-button>
</div>
<el-table v-loading="loading" :data="rows" size="small">
<el-table-column prop="id" label="ID" width="80" sortable />
<el-table-column prop="account_id" label="账号" width="80" />
<el-table-column label="类型" width="90">
<template #default="{ row }">
<el-tag size="small" :type="row.type === 2 ? 'warning' : 'primary'">{{ CALLBACK_TYPE[row.type] ?? row.type }}</el-tag>
</template>
</el-table-column>
<el-table-column prop="url" label="回调地址" min-width="200" show-overflow-tooltip />
<el-table-column label="状态" width="100">
<template #default="{ row }">
<el-tag size="small" :type="row.status === 1 ? 'success' : row.status === 2 ? 'danger' : 'info'">
{{ CALLBACK_STATUS[row.status] ?? row.status }}
</el-tag>
</template>
</el-table-column>
<el-table-column prop="attempt" label="重试" width="70" />
<el-table-column label="最近结果" min-width="150" show-overflow-tooltip>
<template #default="{ row }">{{ row.last_code ? `HTTP ${row.last_code}` : '—' }}{{ row.last_err ? ` · ${row.last_err}` : '' }}</template>
</el-table-column>
<el-table-column prop="next_retry_at" label="下次重试" width="170" />
<el-table-column prop="created_at" label="入队时间" width="170" />
<el-table-column v-if="showPayload" label="载荷(JSON)" min-width="260">
<template #default="{ row }"><code class="cb-payload">{{ row.payload }}</code></template>
</el-table-column>
<el-table-column label="操作" width="110" fixed="right">
<template #default="{ row }">
<el-button v-if="row.status === 2" size="small" type="primary" @click="resend(row)">重派</el-button>
<span v-else class="cb-done">—</span>
</template>
</el-table-column>
<template #empty>
<el-empty description="暂无回调任务——账号配置 report_url/uplink_url 后,回执终态与上行消息自动入队推送" />
</template>
</el-table>
<el-pagination v-if="total > q.page_size" v-model:current-page="q.page" :page-size="q.page_size" :total="total"
layout="total, prev, pager, next" class="cb-pager" @current-change="load" />
</section>
</div>
</template>
<script setup lang="ts">
/** 回调管理页(S4·C1):callback_task 列表 + 状态面板 + 死信重派。
* 口径:PushWorker 在 consumer 进程投递(2s 扫描/2^n 秒退避/10 次耗尽死信);
* 本页仅管理面——列表查询 + 死信重派(POST /ops/callback/:id/resend)。 */
import { onMounted, reactive, ref } from 'vue'
import { ElMessage, ElMessageBox } from 'element-plus'
import { callbackApi, CALLBACK_STATUS, CALLBACK_TYPE, type CallbackTask } from '@/api/callback'
const rows = ref<CallbackTask[]>([])
const counts = ref<Record<string, number>>({})
const total = ref(0)
const loading = ref(false)
const showPayload = ref(false)
const q = reactive<Record<string, number>>({ page: 1, page_size: 20, status: -1, type: 0, account_id: 0 })
async function load() {
loading.value = true
try {
const params: Record<string, unknown> = { page: q.page, page_size: q.page_size }
if (q.status >= 0) params.status = q.status
if (q.type > 0) params.type = q.type
if (q.account_id > 0) params.account_id = q.account_id
const res = await callbackApi.list(params)
rows.value = res.list ?? []
total.value = res.total ?? 0
counts.value = res.status_counts ?? {}
} finally {
loading.value = false
}
}
async function resend(row: CallbackTask) {
await ElMessageBox.confirm(`重派任务 #${row.id} 到 ${row.url}?重试计数将清零并立即投递。`, '死信重派', { type: 'warning' })
await callbackApi.resend(row.id)
ElMessage.success(`任务 #${row.id} 已重派`)
await load()
}
onMounted(load)
</script>
<style scoped>
.cb-card { display: grid; gap: 14px; }
.cb-head { display: flex; justify-content: space-between; align-items: baseline; gap: 12px; flex-wrap: wrap; }
.cb-badge { margin-left: 8px; }
.cb-sub { font-size: 12px; opacity: 0.65; }
.cb-counts { display: flex; gap: 12px; }
.cb-count { flex: 1; display: grid; gap: 2px; padding: 10px 14px; border-radius: 10px; background: var(--el-fill-color-light); }
.cb-num { font-size: 22px; font-weight: 600; font-variant-numeric: tabular-nums; }
.cb-ok { color: var(--el-color-success); }
.cb-dead { color: var(--el-color-danger); }
.cb-label { font-size: 12px; opacity: 0.65; }
.cb-filters { display: flex; gap: 10px; flex-wrap: wrap; align-items: center; }
.cb-payload { font-size: 11px; word-break: break-all; opacity: 0.85; }
.cb-done { opacity: 0.4; }
.cb-pager { justify-self: end; }
</style>
+3 -3
View File
@@ -2,13 +2,13 @@ import { describe, expect, it } from 'vitest'
import { accessibleRoutes } from '@/router/guard' import { accessibleRoutes } from '@/router/guard'
describe('路由权限过滤 accessibleRoutes', () => { describe('路由权限过滤 accessibleRoutes', () => {
it('admin 直通全部业务路由(31 模块菜单,含 FE-2 通道与策略五入口+D19 定时任务+D20 模板管理+D22 结果反馈+D23 短信聚合页+D24 下行记录+D27 上行记录+D28 回执工具+S3·FE-D34 利润分析)', () => { it('admin 直通全部业务路由(32 模块菜单,含 FE-2 通道与策略五入口+D19 定时任务+D20 模板管理+D22 结果反馈+D23 短信聚合页+D24 下行记录+D27 上行记录+D28 回执工具+S3·FE-D34 利润分析+S4·C1 回调管理)', () => {
const all = accessibleRoutes(['admin']) const all = accessibleRoutes(['admin'])
const menus = all.flatMap((r) => (r.children ?? []).filter((c) => c.meta?.menu)) const menus = all.flatMap((r) => (r.children ?? []).filter((c) => c.meta?.menu))
expect(menus.length).toBe(32) expect(menus.length).toBe(33)
// FE-2/D23/S3 关键入口在菜单(D11 上家/D14 通道组/D16 导流+报备/D19 定时任务/D20 模板管理/D22 结果反馈/D23 聚合页/FE-D34 利润分析) // FE-2/D23/S3 关键入口在菜单(D11 上家/D14 通道组/D16 导流+报备/D19 定时任务/D20 模板管理/D22 结果反馈/D23 聚合页/FE-D34 利润分析)
const names = menus.map((m) => m.name) const names = menus.map((m) => m.name)
for (const want of ['channel-upstream', 'channel-group', 'policy-route', 'policy-report', 'sms-timer', 'sms-template', 'sms-result', 'sms-home', 'record-down', 'stats-profit']) { for (const want of ['channel-upstream', 'channel-group', 'policy-route', 'policy-report', 'sms-timer', 'sms-template', 'sms-result', 'sms-home', 'record-down', 'stats-profit', 'ops-callback']) {
expect(names).toContain(want) expect(names).toContain(want)
} }
}) })
+1
View File
@@ -25,6 +25,7 @@ const MATRIX: Record<string, Roles[]> = {
contacts: ALL, contacts: ALL,
'stats-user': ['admin', 'ops', 'service'], 'stats-channel': ['admin', 'ops'], 'stats-user': ['admin', 'ops', 'service'], 'stats-channel': ['admin', 'ops'],
'stats-profit': ['admin', 'ops', 'service'], settle: ['admin', 'ops'], 'stats-profit': ['admin', 'ops', 'service'], settle: ['admin', 'ops'],
'ops-callback': ['admin', 'ops'],
logs: ['admin', 'ops', 'service'], system: ['admin'], faq: ALL, logs: ['admin', 'ops', 'service'], system: ['admin'], faq: ALL,
} }