117 lines
4.6 KiB
Go
117 lines
4.6 KiB
Go
// pipeline.go Day 48:发送全流程串联(API→预扣→审核→拦截→路由→Kafka)。
|
|
// SubmitService 经 SubmitOption 注入拦截器链与事件出口,受理主链升级为显式状态机:
|
|
//
|
|
// ①Validate → ②余额只读快检(UX 快速失败) → ③holdFee 原子预扣(WHERE balance>=fee)
|
|
// → ④事务落库(失败→RefundTask 对冲) → ⑤逐号拦截(命中→明细终态 5+该号即时退费)
|
|
// → ⑥审核分流 Route(拒绝→退剩余全款) → ⑦EmitSubmit 入队 sms-submit(消费侧路由下发)。
|
|
//
|
|
// 路由/下发不在受理期(同步链路 ≤5ms 禁令,技术方案 10.4):由 submit 消费者
|
|
// (consumer.go)认领后经 ChannelDispatcher 路由→pacer 限速→聚合/直发链路完成。
|
|
package submit
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"time"
|
|
|
|
"go-sms/internal/model/entity"
|
|
"go-sms/internal/pkg/queue"
|
|
"go-sms/internal/service/balance"
|
|
"go-sms/internal/service/interceptor"
|
|
)
|
|
|
|
// EventSink 受理完成事件出口(装配层注入;nil=不投,发送由 ReclaimScanner 兜底)。
|
|
// Day52 R2:details=入口预生成明细(含拦截终态行)随事件投递,消费侧批量落库;
|
|
// 返回 error——投递失败由 SubmitService 降级同步落明细(零丢失红线)。
|
|
type EventSink interface {
|
|
EmitSubmit(ctx context.Context, task *entity.MessageTask, details []entity.MessageDetail) error
|
|
}
|
|
|
|
// ProducerSink queue.Producer 适配器(sms-submit 统一投递口,Day48 起 openapi
|
|
// 不再自行 emit,避免双投)。
|
|
type ProducerSink struct {
|
|
P interface {
|
|
Send(topic string, key, value []byte) error
|
|
}
|
|
Source string // 来源标识(openapi/console/access)
|
|
}
|
|
|
|
// EmitSubmit 投递 sms-submit(key=task_no;失败返回错误——Day52 起明细在事件里,
|
|
// 丢失=对账事实源丢失,入口须降级同步落库对冲;任务认领仍由 ReclaimScanner 兜底)。
|
|
func (s ProducerSink) EmitSubmit(ctx context.Context, task *entity.MessageTask, details []entity.MessageDetail) error {
|
|
if s.P == nil || task == nil {
|
|
return nil
|
|
}
|
|
eds := make([]EventDetail, 0, len(details))
|
|
pass := 0
|
|
for i := range details {
|
|
d := &details[i]
|
|
if d.Status == 0 {
|
|
pass++
|
|
}
|
|
eds = append(eds, EventDetail{
|
|
ID: d.ID, Mobile: d.Mobile, Content: d.Content, Status: d.Status,
|
|
Fee: d.Fee, ErrorCode: d.ErrorCode, ExtCode: d.ExtCode, CallData: d.CallData,
|
|
Business: d.Business, Source: d.Source, // Day66:明细单表化冗余透传
|
|
})
|
|
}
|
|
// P0-1(Day 63)快路径:task.ID==0=受理侧零 DB 写(SubmitFast),消费侧
|
|
// handleDeferred 补建任务/拦截/退费/审核;TaskMeta 携带任务行补建字段。
|
|
ev := SubmitEvent{
|
|
TaskID: task.ID, TaskNo: task.TaskNo, AccountID: task.AccountID,
|
|
SendType: task.SendType, PassCount: pass, Source: s.Source,
|
|
CreatedAt: task.CreatedAt.Format(time.RFC3339),
|
|
Business: task.Business, // Day 49:biz_type 路由(video 记录隔离)
|
|
Details: eds, // Day52 R2:明细全量(消费侧批量落库)
|
|
}
|
|
if task.ID == 0 {
|
|
ev.Deferred = true
|
|
meta := &SubmitTaskMeta{
|
|
Content: task.Content, Sign: task.Sign, MobileCount: task.MobileCount,
|
|
FeeTotal: task.FeeTotal, SubmitIP: task.SubmitIP, CallData: task.CallData,
|
|
Source: task.Source,
|
|
}
|
|
if task.ScheduleAt != nil {
|
|
meta.ScheduleAt = task.ScheduleAt.Format(time.RFC3339)
|
|
}
|
|
ev.TaskMeta = meta
|
|
}
|
|
payload, err := json.Marshal(ev)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return s.P.Send(queue.TopicSubmit, []byte(task.TaskNo), payload)
|
|
}
|
|
|
|
// SubmitOption SubmitService 可选依赖(零破坏:既有三参构造不变)。
|
|
type SubmitOption func(*SubmitService)
|
|
|
|
// WithInterceptorChain 注入拦截器链(Day42 五类;受理期逐号硬拦截)。
|
|
func WithInterceptorChain(c *interceptor.Chain) SubmitOption {
|
|
return func(s *SubmitService) { s.chain = c }
|
|
}
|
|
|
|
// WithEventSink 注入事件出口(sms-submit 统一投递)。
|
|
func WithEventSink(sink EventSink) SubmitOption {
|
|
return func(s *SubmitService) { s.sink = sink }
|
|
}
|
|
|
|
// WithBalanceStore 注入资金预扣 Store(Day54 R1;nil=MySQL 行锁原路径)。
|
|
func WithBalanceStore(b balance.Store) SubmitOption {
|
|
return func(s *SubmitService) { s.bal = b }
|
|
}
|
|
|
|
// intercept 逐号拦截(非 DryRun:频次计数/落库副作用保留)。
|
|
// 返回命中码列表(nil=该号放行);taskNo 用于 send_block_log 关联。
|
|
// Day51 R3:sc 请求级快照(批量提交入口构造一次,拦截器批量 IN+跨号复用)。
|
|
func (s *SubmitService) intercept(ctx context.Context, taskNo string,
|
|
accountID int64, sendType int8, mobile, content string, sc *interceptor.Scope) *interceptor.Block {
|
|
if s.chain == nil {
|
|
return nil
|
|
}
|
|
return s.chain.Check(ctx, &interceptor.Req{
|
|
TaskNo: taskNo, AccountID: accountID, Mobile: mobile,
|
|
Content: content, SendType: sendType, Scope: sc,
|
|
})
|
|
}
|