func SWifMetricToTransfer() { log.Println("start SWifMetricToTransfer") sema := nsema.NewSemaphore(10) for { items := IfstatsQueue.PopBackBy(5000) count := len(items) if count == 0 { time.Sleep(DefaultSendTaskSleepInterval) continue } mvsSend := make([]*model.MetricValue, count) for i := 0; i < count; i++ { mvsSend[i] = items[i].(*model.MetricValue) } // 同步Call + 有限并发 进行发送 sema.Acquire() go func(mvsend []*model.MetricValue) { defer sema.Release() g.SendToTransfer(mvsend) }(mvsSend) } }
func configPushRoutes() { http.HandleFunc("/v1/push", func(w http.ResponseWriter, req *http.Request) { if req.ContentLength == 0 { http.Error(w, "body is blank", http.StatusBadRequest) return } decoder := json.NewDecoder(req.Body) var metrics []*model.MetricValue err := decoder.Decode(&metrics) if err != nil { http.Error(w, "connot decode body", http.StatusBadRequest) return } g.SendToTransfer(metrics) w.Write([]byte("success")) }) }
func MetricToTransfer(sec int64, fns []func() []*model.MetricValue) { mvs := []*model.MetricValue{} for _, fn := range fns { items := fn() if items == nil { continue } if len(items) == 0 { continue } for _, mv := range items { mvs = append(mvs, mv) } } startTime := time.Now() //分批次传给transfer n := 5000 lenMvs := len(mvs) div := lenMvs / n mod := math.Mod(float64(lenMvs), float64(n)) mvsSend := []*model.MetricValue{} for i := 1; i <= div+1; i++ { if i < div+1 { mvsSend = mvs[n*(i-1) : n*i] } else { mvsSend = mvs[n*(i-1) : (n*(i-1))+int(mod)] } time.Sleep(100 * time.Millisecond) go g.SendToTransfer(mvsSend) } endTime := time.Now() log.Println("INFO : Send metrics to transfer running in the background. Process time :", endTime.Sub(startTime), "Send metrics :", len(mvs)) }