コード例 #1
0
func (h *RequestsHandler) HandleResponse(w http.ResponseWriter, r *http.Request) {
	defer r.Body.Close()
	w.Header().Set("Content-Type", "application/json")

	req, err := openrtb.ParseRequest(r.Body)
	if nil != err {
		h.sendEmptyResponse(w)
		log.Println("ERROR on parsing the request", err.Error())
	} else {
		log.Println("INFO  Received bid request ", *req.Id)

		r := rand.New(rand.NewSource(time.Now().UnixNano()))

		if r.Float32() > h.Probability {
			h.sendEmptyResponse(w)
			return
		}

		time.Sleep(time.Duration(h.Delay) * time.Millisecond)

		resp := h.buildResponse(r, req)
		bin, err := json.Marshal(resp)
		if nil == err {
			w.WriteHeader(200)
			fmt.Fprintf(w, "%s", string(bin))
		} else {
			w.WriteHeader(500)
			fmt.Fprintf(w, "%s", string(err.Error()))
		}
	}
}