Documentation
¶
Overview ¶
Package notification 提供离线通知队列:用户不在线时存通知,上线后拉取, 与 pkg/router 的实时路由互补——router 投在线者,notification 投离线者。
设计要点:
- persistent 标志区分持久通知(存 DB+在线投)与瞬时通知(仅在线投);
- 离线通知按 (userID, seq) 有序存储,游标分页避免重复推送;
- 无 read/unread 状态机——删除即消失,简化并发(只删不改)。
与 pkg/router 的分工:Send 时若用户在线(通过 liveSink)即时投递, 持久通知无论在线与否都存一份,供后续拉取/审计。
适用场景:IM 离线消息、系统通知、好友请求、活动奖励提醒。
零值不可用,用 New 构造。Store 并发安全。
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type LiveSink ¶
type LiveSink func(userID string, n *Notification) bool
LiveSink 在线投递函数。返回 false 表示用户当前不在线。 实现通常用 pkg/router.SendToPresenceIDs 或 pkg/ws/session.Send。
type Notification ¶
type Notification struct {
ID int64 // 全局单调递增 ID
UserID string // 接收者
SenderID string // 发送者(系统为空)
Subject string // 主题/类型(如 "friend_request")
Content string // 业务自定义 JSON
Code int // 分类码(业务自定义)
Persistent bool // 持久化:存库 + 上线可拉取;false 则仅在线投
Seq int64 // 用户内单调序号(游标分页用)
CreateTime int64 // unix nano
}
Notification 是一条通知。持久化后 ID/Seq 由 Store 分配。
type Store ¶
type Store struct {
// contains filtered or unexported fields
}
Store 管理持久通知的存储、在线投递与游标拉取。
func (*Store) List ¶
func (s *Store) List(userID string, afterSeq int64, limit int) []Notification
List 拉取某用户的持久通知,支持游标分页。 afterSeq:返回 seq > afterSeq 的通知(0 表示从头);limit<=0 默认 50。 返回的通知按 seq 升序。调用方下次用最后一条的 Seq 作为 afterSeq 续传。
func (*Store) Send ¶
func (s *Store) Send(ctx context.Context, n *Notification) *Notification
Send 发送一条通知。
- persistent=true:存库 + 若在线则实时投(在线投失败也不影响存储);
- persistent=false:仅尝试在线投,不存库。投失败即丢弃(瞬时通知)。
返回存库后的 Notification(ID/Seq 已填充),未存库则返回 nil。
Click to show internal directories.
Click to hide internal directories.