handleAllNodeMetricsStream 推送所有节点的实时指标(SSE)。 所有 SSE 连接共享同一个 metricsHub 广播,避免每个连接独立轮询 DB 导致连接池耗尽。
(w http.ResponseWriter, r *http.Request)
| 380 | // handleAllNodeMetricsStream 推送所有节点的实时指标(SSE)。 |
| 381 | // 所有 SSE 连接共享同一个 metricsHub 广播,避免每个连接独立轮询 DB 导致连接池耗尽。 |
| 382 | func (a *API) handleAllNodeMetricsStream(w http.ResponseWriter, r *http.Request) { |
| 383 | flusher, ok := w.(http.Flusher) |
| 384 | if !ok { |
| 385 | http.Error(w, "streaming not supported", http.StatusInternalServerError) |
| 386 | return |
| 387 | } |
| 388 | if a.mHub == nil { |
| 389 | http.Error(w, "metrics hub not ready", http.StatusServiceUnavailable) |
| 390 | return |
| 391 | } |
| 392 | |
| 393 | w.Header().Set("Content-Type", "text/event-stream") |
| 394 | w.Header().Set("Cache-Control", "no-cache") |
| 395 | w.Header().Set("Connection", "keep-alive") |
| 396 | |
| 397 | ch := a.mHub.subscribe() |
| 398 | defer a.mHub.unsubscribe(ch) |
| 399 | |
| 400 | for { |
| 401 | select { |
| 402 | case <-r.Context().Done(): |
| 403 | return |
| 404 | case data := <-ch: |
| 405 | fmt.Fprintf(w, "data: %s\n\n", data) |
| 406 | flusher.Flush() |
| 407 | } |
| 408 | } |
| 409 | } |
| 410 | |
| 411 | func (a *API) handleNodeStatus(w http.ResponseWriter, r *http.Request, nodeID string) { |
| 412 | if r.Method != http.MethodGet { |
nothing calls this directly
no test coverage detected