动态修改线程数量
This commit is contained in:
@@ -89,6 +89,7 @@ func (s *Server) Handler() http.Handler {
|
||||
mux.HandleFunc("/admin/priority", s.handleAdminPriority)
|
||||
mux.HandleFunc("/admin/flush", s.handleAdminFlush)
|
||||
mux.HandleFunc("/admin/pending", s.handleAdminPending)
|
||||
mux.HandleFunc("/admin/workers", s.handleAdminWorkers)
|
||||
// 静态文件(SPA fallback)
|
||||
mux.Handle("/", spaHandler{dist: "dist"})
|
||||
return mux
|
||||
@@ -408,6 +409,51 @@ func (s *Server) handleAdminPending(w http.ResponseWriter, r *http.Request) {
|
||||
json.NewEncoder(w).Encode(map[string]int64{"pending": count})
|
||||
}
|
||||
|
||||
// handleAdminWorkers 查看和动态调整爬虫并发线程数。
|
||||
// GET 返回当前 workers 值
|
||||
// POST {"workers": N} 动态修改(范围 1~500),下一轮 epoch 立即生效
|
||||
func (s *Server) handleAdminWorkers(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Set("Access-Control-Allow-Origin", "*")
|
||||
w.Header().Set("Content-Type", "application/json; charset=utf-8")
|
||||
|
||||
switch r.Method {
|
||||
case http.MethodOptions:
|
||||
w.Header().Set("Access-Control-Allow-Origin", "*")
|
||||
w.Header().Set("Access-Control-Allow-Methods", "GET,POST,OPTIONS")
|
||||
w.Header().Set("Access-Control-Allow-Headers", "Content-Type")
|
||||
w.WriteHeader(204)
|
||||
return
|
||||
|
||||
case http.MethodGet:
|
||||
json.NewEncoder(w).Encode(map[string]int{"workers": config.CrawlerWorkers()})
|
||||
|
||||
case http.MethodPost:
|
||||
var body struct {
|
||||
Workers int `json:"workers"`
|
||||
}
|
||||
if err := json.NewDecoder(r.Body).Decode(&body); err != nil {
|
||||
http.Error(w, `{"error":"invalid json"}`, 400)
|
||||
return
|
||||
}
|
||||
if body.Workers < 1 || body.Workers > 500 {
|
||||
http.Error(w, `{"error":"workers must be between 1 and 500"}`, 400)
|
||||
return
|
||||
}
|
||||
old := config.CrawlerWorkers()
|
||||
config.SetCrawlerWorkers(body.Workers)
|
||||
log.Printf("[admin] workers changed: %d → %d (takes effect next epoch)", old, body.Workers)
|
||||
json.NewEncoder(w).Encode(map[string]interface{}{
|
||||
"status": "ok",
|
||||
"old": old,
|
||||
"current": config.CrawlerWorkers(),
|
||||
})
|
||||
|
||||
default:
|
||||
w.Header().Set("Allow", "GET,POST")
|
||||
http.Error(w, `{"error":"method not allowed"}`, 405)
|
||||
}
|
||||
}
|
||||
|
||||
// ---- 搜索处理器 ----
|
||||
|
||||
// searchResponse 是搜索 API 的 JSON 响应结构。
|
||||
|
||||
Reference in New Issue
Block a user