From 6be5081851f006d99e203ce02fed760c5faffaf1 Mon Sep 17 00:00:00 2001 From: Young Xu Date: Fri, 29 Nov 2024 08:49:47 +0800 Subject: [PATCH] fix(metrics): concurrent map read and map write (#805) Signed-off-by: Young Xu --- lib/util/lifted/influx/httpd/handler_metrics.go | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/lib/util/lifted/influx/httpd/handler_metrics.go b/lib/util/lifted/influx/httpd/handler_metrics.go index 8b0d3d4e1..90897092b 100644 --- a/lib/util/lifted/influx/httpd/handler_metrics.go +++ b/lib/util/lifted/influx/httpd/handler_metrics.go @@ -21,6 +21,7 @@ import ( "net/http" "strconv" "strings" + "sync" "sync/atomic" "time" @@ -52,6 +53,7 @@ func init() { } type OpenGeminiCollector struct { + mux sync.RWMutex indexMap map[string][]*metrics.ModuleIndex } @@ -66,7 +68,9 @@ func (c *OpenGeminiCollector) Describe(chan<- *prometheus.Desc) {} func (c *OpenGeminiCollector) Collect(ch chan<- prometheus.Metric) { for _, moduleName := range metricMsts { + c.mux.RLock() metricSlice, ok := c.indexMap[moduleName] + c.mux.RUnlock() if !ok { continue } @@ -103,7 +107,9 @@ func (h *Handler) serveMetrics(w http.ResponseWriter, r *http.Request, user meta if err != nil { continue } + openGeminiCollector.mux.Lock() openGeminiCollector.indexMap[moduleName] = moduleIndex + openGeminiCollector.mux.Unlock() } promhttp.Handler().ServeHTTP(w, r) @@ -211,6 +217,9 @@ func getMetrics(h *Handler, r *http.Request, user meta.User, tableName string) ( } resp := h.getStmtResult(stmtID2Result) + if len(resp.Results) == 0 { + return []*metrics.ModuleIndex{}, resp.Err + } if resp.Results[0].Err != nil { return nil, resp.Results[0].Err }