实时显示发现的url数量
This commit is contained in:
+19
-2
@@ -69,6 +69,9 @@ type Crawler struct {
|
||||
// 运行时活跃线程计数(atomic,每轮 epoch 自动归零前重新开始计数)
|
||||
activeWorkers int64
|
||||
|
||||
// 本轮发现的新链接计数(atomic,供前端实时监控)
|
||||
newLinksCount int64
|
||||
|
||||
// ---- Priority Worker(独立 goroutine,不受主 workers 限制)----
|
||||
priorityCh chan string // Priority URL 任务队列(用户手动添加)
|
||||
priorityChildCh chan string // Priority 子链接队列(子 URL 继续由 priority worker 爬取)
|
||||
@@ -93,7 +96,8 @@ type CrawlStatus struct {
|
||||
QueueLength int `json:"queue_length"` // 本轮队列长度
|
||||
CompletedCount int `json:"completed_count"` // 本轮已完成的 URL 数
|
||||
VisitedTotal int `json:"visited_total"` // 已收录 URL 总数
|
||||
NextPoolSize int `json:"next_pool_size"` // 下一轮链接池大小(newLinks 调度后的队列长度)
|
||||
NewLinksCount int64 `json:"new_links_count"` // 本轮已发现的新链接数(实时更新)
|
||||
NextPoolSize int `json:"next_pool_size"` // 下一轮链接池大小(调度后的队列长度)
|
||||
IsRunning bool `json:"is_running"` // 是否正在运行
|
||||
}
|
||||
|
||||
@@ -457,11 +461,15 @@ func (c *Crawler) Run(entryURL string, maxEpoch int) {
|
||||
// 每轮 epoch 从 config 读取最新 workers 值,支持运行时动态调整
|
||||
workers := config.CrawlerWorkers()
|
||||
|
||||
// 重置新链接计数器
|
||||
atomic.StoreInt64(&c.newLinksCount, 0)
|
||||
|
||||
// 更新爬取状态:新一轮开始
|
||||
c.updateCrawlStatus(func(cs *CrawlStatus) {
|
||||
cs.CurrentEpoch = ep + 1
|
||||
cs.QueueLength = len(queue)
|
||||
cs.CompletedCount = 0
|
||||
cs.NewLinksCount = 0
|
||||
})
|
||||
|
||||
// 每轮开始前:拉取 priority URLs,插入队列前端
|
||||
@@ -526,11 +534,16 @@ func (c *Crawler) Run(entryURL string, maxEpoch int) {
|
||||
// 分配权重
|
||||
w := 1.0 / float64(n)
|
||||
|
||||
// 子 URL 进入 newLinks 调度,由普通 BFS 下一轮处理。
|
||||
// 子 URL 进入 newLinks 调度,由普通 BFS 下一轮处理。
|
||||
// (priority worker 通过 priorityChildCh 独立爬取子 URL,两者互不干扰)
|
||||
mu.Lock()
|
||||
for _, h := range children {
|
||||
newLinks = append(newLinks, URLWeight{URL: h, Weight: w})
|
||||
// 实时自增计数(供前端监控)
|
||||
atomic.AddInt64(&c.newLinksCount, 1)
|
||||
c.updateCrawlStatus(func(cs *CrawlStatus) {
|
||||
cs.NewLinksCount = atomic.LoadInt64(&c.newLinksCount)
|
||||
})
|
||||
}
|
||||
mu.Unlock()
|
||||
}(u)
|
||||
@@ -545,6 +558,10 @@ func (c *Crawler) Run(entryURL string, maxEpoch int) {
|
||||
select {
|
||||
case gc := <-c.normalChildCh:
|
||||
newLinks = append(newLinks, gc)
|
||||
atomic.AddInt64(&c.newLinksCount, 1) // 孙链接也要计入
|
||||
c.updateCrawlStatus(func(cs *CrawlStatus) {
|
||||
cs.NewLinksCount = atomic.LoadInt64(&c.newLinksCount)
|
||||
})
|
||||
case <-timeout:
|
||||
drained = true
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user