今天主要讲的是 NSQD 中 queueScanLoop代码实现,这个函数的作用是用来维护 channel 中的延时队列和等待消费确认队列。将超时的消息重新加入消费队列中。
主要代码文件:
1.nsqd/nsqd.go
queueScanLoop 函数在 Main 函数中启动 n.waitGroup.Wrap(n.queueScanLoop)
func (n *NSQD) queueScanLoop() {
//传递需要处理的 channel
workCh := make(chan *Channel, n.getOpts().QueueScanSelectionCount)
responseCh := make(chan bool, n.getOpts().QueueScanSelectionCount)
closeCh := make(chan int)
//定时执行 loop 的间隔时间
workTicker := time.NewTicker(n.getOpts().QueueScanInterval)
//goroutine 池维护时间
refreshTicker := time.NewTicker(n.getOpts().QueueScanRefreshInterval)
channels := n.channels() //获取当前nsqd 中所有的channels
//goroutine 池任务处理和维护
n.resizePool(len(channels), workCh, responseCh, closeCh)
for {
select {
case <-workTicker.C:
if len(channels) == 0 {
continue
}
case <-refreshTicker.C:
channels = n.channels()
n.resizePool(len(channels), w