This commit is contained in:
2026-06-22 08:55:57 +08:00
parent dbb87823e8
commit 6ade6e8fa9
325 changed files with 41131 additions and 855 deletions
+11
View File
@@ -21,6 +21,8 @@ type Dispatcher struct {
log *slog.Logger
now func() time.Time
wg sync.WaitGroup
// hub 是可选的实时推送中心;落库成功后 Publish。nil 时仅持久化 + 外发通道。
hub StreamHub
}
// NewDispatcher 用 Repository 与 Logger 构造 Dispatcher。注册 Notifier 通过
@@ -35,6 +37,11 @@ func (d *Dispatcher) Register(n Notifier) {
d.notifiers = append(d.notifiers, n)
}
// SetStreamHub 注入实时推送中心(装配期)。落库成功后会向其 Publish。
func (d *Dispatcher) SetStreamHub(h StreamHub) {
d.hub = h
}
// Dispatch 同步落库;落库失败返回错误,不再异步投递。落库成功后,
// fire-and-forget 派发给所有外部 notifier,单个 notifier 的失败仅记日志,
// 不阻塞其它通道;调用方无法感知异步投递结果。
@@ -54,6 +61,10 @@ func (d *Dispatcher) Dispatch(ctx context.Context, msg Message) error {
if err := d.repo.Insert(persistCtx, msg); err != nil {
return err
}
// 落库成功后才推送实时事件(推送失败 / 慢订阅者不影响落库的权威 inbox)。
if d.hub != nil {
d.hub.Publish(msg.UserID, msg)
}
for _, n := range d.notifiers {
d.wg.Add(1)
go func(n Notifier) {