Пример #1
0
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"))
	})
}
Пример #2
0
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))
}