今天主要讲的是NSQ topic 的代码实现,topic作为MQ的重要概念,了解它的实现,对我们理解其他MQ的topic,有很多的益处。
主要代码文件:
1.nsqd/topic.go
topic结构体
type Topic struct {
// 64bit atomic vars need to be first for proper alignment on 32bit platforms
messageCount uint64 //消息累计条数 后期查看每个topic的状态时有用
messageBytes uint64 //消息累计字节数 后期查看每个topic的状态时有用
sync.RWMutex
name string //主题名称
channelMap map[string]*Channel //主题拥有的channel
backend BackendQueue //磁盘队列
memoryMsgChan chan *Message //内存队列
startChan chan int //topic 开始接收消息的chan
exitChan chan int //topic 结束的chann
channelUpdateChan chan int //channel 更新通知(停止,删除,新增)
waitGroup util.WaitGroupWrapper
exitFlag int32
idFactory *guidFactory //id 生成器
ephemeral bool
deleteCallback func(*Topic) //topic 删除的 回调函数
deleter sync.Once
paused int32
pauseChan chan int
ctx *context //包装了nsqd的context
}