Documentation
¶
Overview ¶
Package chat 提供频道聊天消息:按 channel 持久化 + 自增 message_id 游标分页。
与 pkg/domain/notification 互补:notification 是"给个人的离线信"(按 userID 游标), chat 是"给频道的历史信"(按 channelID 游标)。IM 频道消息需要持久化 + 历史拉取 + 游标分页,区别于 pkg/match 的实时(不持久)与 pkg/router 的投递(不存历史)。
channel message:
- 每条消息有 channel 内单调递增的 message_id(游标);
- Before(messageID, limit) 返回该 ID 之前的历史(翻页往前看);
- 频道有容量上限,超限删最旧(message_id 不因截断回退,保持单调);
- 实时投递与持久化解耦:本包只管存历史,实时扇出由 pkg/router 负责。
零值不可用,用 New 构造。Store 并发安全。
Index ¶
- type Message
- type Option
- type Store
- func (s *Store) After(channelID string, afterID int64, limit int) []Message
- func (s *Store) Before(channelID string, beforeID int64, limit int) []Message
- func (s *Store) Count(channelID string) int
- func (s *Store) Delete(channelID string, id int64) bool
- func (s *Store) LastMsgID(channelID string) int64
- func (s *Store) Latest(channelID string, limit int) []Message
- func (s *Store) Post(channelID, userID, content string, nowNano int64) *Message
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Message ¶
type Message struct {
ID int64 // 全局唯一 ID(Store 分配)
ChannelID string // 频道 ID
MsgID int64 // 频道内单调序号(游标分页用)
UserID string // 发送者
Content string // 消息内容(业务可自定义编码,这里用 string 简化)
CreatedAt int64 // 创建时间(UnixNano)
}
Message 一条频道消息。
type Option ¶
type Option func(*config)
Option 配置 Store。
func WithMaxPerChannel ¶
WithMaxPerChannel 设置每频道最大保留条数,超限删最旧。默认 1000。
type Store ¶
type Store struct {
// contains filtered or unexported fields
}
Store 频道消息存储:按 channel 持久化 + 游标分页。
func (*Store) After ¶
After 返回 channelID 中 msgID > afterID 的消息(拉取新消息)。 afterID<0 表示从最新开始(返回最新 limit 条)。 返回按 msgID 升序(旧→新),便于客户端追加到列表尾部。 limit<=0 默认 50。
func (*Store) Before ¶
Before 返回 channelID 中 msgID < beforeID 的历史(翻页往前看)。 beforeID<=0 表示返回最新的 limit 条。 返回按 msgID 降序(最新在前),便于客户端"加载更早消息"。 limit<=0 默认 50。