MCPcopy Index your code
hub / github.com/0xUnixIO/pulse / handleAllNodeMetricsStream

Method handleAllNodeMetricsStream

internal/serverapi/api.go:382–409  ·  view source on GitHub ↗

handleAllNodeMetricsStream 推送所有节点的实时指标(SSE)。 所有 SSE 连接共享同一个 metricsHub 广播,避免每个连接独立轮询 DB 导致连接池耗尽。

(w http.ResponseWriter, r *http.Request)

Source from the content-addressed store, hash-verified

380// handleAllNodeMetricsStream 推送所有节点的实时指标(SSE)。
381// 所有 SSE 连接共享同一个 metricsHub 广播,避免每个连接独立轮询 DB 导致连接池耗尽。
382func (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
411func (a *API) handleNodeStatus(w http.ResponseWriter, r *http.Request, nodeID string) {
412 if r.Method != http.MethodGet {

Callers

nothing calls this directly

Calls 5

subscribeMethod · 0.80
unsubscribeMethod · 0.80
FlushMethod · 0.80
DoneMethod · 0.65
ErrorMethod · 0.45

Tested by

no test coverage detected