Пример #1
0
func createReq(r *http.Request) (network.Request, error) {
	var err error
	var requestID string
	var start = time.Now()

	//read request
	message, err := network.ReadMessage(r)
	if err != nil {
		log.Println("Cannot read message")
		return network.Request{}, err
	}
	requestID = message.Args
	if requestID == "" {
		log.Println("New request, generating ID")
		requestID = strconv.Itoa(readAndIncrementCounter())
	}
	log.Printf("Received request %s from %s\n", requestID, message.Sender)

	toService, err := network.ReadParam("service", r)
	if err != nil {
		log.Println("Cannot read param 'service'")
	}

	req := network.Request{
		ID:         requestID,
		From:       message.Sender,
		To:         toService,
		Counter:    len(destinations),
		Start:      start,
		ExecTimeMs: 0,
	}

	return req, nil
}
Пример #2
0
func readResponse(w http.ResponseWriter, r *http.Request) {
	var err error
	var respTimeMs float64

	w.Header().Set("Content-Type", "application/json; charset=UTF-8")

	message, err := network.ReadMessage(r)
	if err != nil {
		log.Println("Cannot read message")
		w.WriteHeader(422)
		return
	}
	log.Println("Received response from ", message.Sender)

	reqId := message.Args
	if req, ok := requests[reqId]; ok {
		respTimeMs = time.Since(req.Start).Seconds() * 1000
		complete := updateRequestInHistory(reqId)
		if complete {
			respondeToRequest(req.From, req.ID, message.Body)
		}
	} else {
		log.Println("Cannot find request ID in history")
		w.WriteHeader(422)
		return
	}
	if message.Body == "done" {
		log.Println("service " + name + " " + "response_time" + ":" + strconv.FormatFloat(respTimeMs, 'f', 2, 64) + "ms")
		if useMetrics {
			metric.SendResponseTime(respTimeMs)
		}
	} else {
		log.Println("Error: request lost.")
	}

	w.WriteHeader(http.StatusCreated)
}