动态修改线程数量

This commit is contained in:
2026-04-09 12:52:33 +08:00
parent ab9c005e3d
commit 2e5876004b
3 changed files with 70 additions and 4 deletions
+17
View File
@@ -6,10 +6,14 @@ import (
"fmt"
"os"
"path/filepath"
"sync"
"gopkg.in/yaml.v3"
)
// configMu 保护 Global 的运行时修改(动态调参场景)。
var configMu sync.RWMutex
// Config 是完整的配置结构体
type Config struct {
Index IndexConfig `yaml:"index"`
@@ -171,6 +175,19 @@ func CrawlerCooldown() int { return Global.Crawler.Cooldown }
// CrawlerWorkers 返回配置值
func CrawlerWorkers() int { return Global.Crawler.Workers }
// SetCrawlerWorkers 在运行时动态修改爬虫并发数(线程安全)。
func SetCrawlerWorkers(n int) {
if n < 1 {
n = 1
}
if n > 500 {
n = 500
}
configMu.Lock()
Global.Crawler.Workers = n
configMu.Unlock()
}
// CrawlFocus 返回配置值
func CrawlFocus() float64 { return Global.Crawler.CrawlFocus }