Files
VLoop/backend/internal/middleware/rabbitmq/socialMQ.go
huanghaosheng af77a979ff
All checks were successful
CI/CD / Verify and Deploy (push) Successful in 3m23s
refactor(rabbitmq): social.events 只承载通知
删除没有消费者的 SocialQueue 队列与 social.unfollow 路由键。SocialMQ 改登记 notification.social。

因此关注事件只走通知链路。social.events 不再是写库副本。

文档同步:解决方案、问题报告、写路径幂等、死信队列、项目设计。

验证:go vet ./... 通过,go test -count=1 ./... 全绿。
2026-11-11 16:19:26 +08:00

62 lines
1.6 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package rabbitmq
import (
"context"
"errors"
"time"
)
// SocialMQ 通过连接管理器发布关注事件(不再持有长生命周期 channel)。
type SocialMQ struct {
mgr *RabbitMQ
}
// 拓扑常量导出:见 likeMQ.go 中的说明。
const (
SocialExchange = "social.events"
SocialFollowRK = "social.follow"
)
type SocialEvent struct {
EventID string `json:"event_id"`
Action string `json:"action"`
FollowerID uint `json:"follower_id"`
VloggerID uint `json:"vlogger_id"`
OccurredAt time.Time `json:"occurred_at"`
}
// NewSocialMQ 只登记关注通知的落点:服务层已先写库,事件不再承担写库职责。
func NewSocialMQ(base *RabbitMQ) (*SocialMQ, error) {
if base == nil {
return nil, errors.New("rabbitmq base is nil")
}
base.RegisterTopology(Topology{Exchange: SocialExchange, Queue: NotificationSocialQueue, BindingKey: SocialFollowRK})
return &SocialMQ{mgr: base}, nil
}
func (s *SocialMQ) Follow(ctx context.Context, followerID, vloggerID uint) error {
return s.publish(ctx, "follow", SocialFollowRK, followerID, vloggerID)
}
func (s *SocialMQ) publish(ctx context.Context, action, routingKey string, followerID, vloggerID uint) error {
if s == nil || s.mgr == nil {
return errors.New("social mq is not initialized")
}
if followerID == 0 || vloggerID == 0 {
return errors.New("followerID and vloggerID are required")
}
id, err := newEventID(16)
if err != nil {
return err
}
evt := SocialEvent{
EventID: id,
Action: action,
FollowerID: followerID,
VloggerID: vloggerID,
OccurredAt: time.Now().UTC(),
}
return s.mgr.PublishJSON(ctx, SocialExchange, routingKey, evt)
}