Exemplo n.º 1
0
func (hrl httpRateLimiter) ServeHTTP(w http.ResponseWriter, r *http.Request) {
	guid := r.Header.Get("X-Request-Id")
	request := common.HTTPToSphinxRequest(r)
	matches, err := hrl.rateLimiter.Add(request)
	if err == leakybucket.ErrorFull {
		addRateLimitHeaders(w, matches)
		common.Log.InfoD("ratelimited", common.ConcatWithRequest(common.M{"guid": guid}, request))
		w.WriteHeader(StatusTooManyRequests)
		return
	}
	if err != nil {
		common.Log.WarnD("error",
			common.ConcatWithRequest(common.M{"guid": guid, "err": err.Error()}, request))
		if !hrl.allowOnError {
			w.WriteHeader(http.StatusInternalServerError)
			return
		}
		matches = []ratelimiter.Status{ratelimiter.NilStatus}
	}
	addRateLimitHeaders(w, matches)
	hrl.proxy.ServeHTTP(w, r)
}
Exemplo n.º 2
0
func (hrl httpRateLogger) ServeHTTP(w http.ResponseWriter, r *http.Request) {
	guid := uuid.New()
	request := common.HTTPToSphinxRequest(r)
	matches, err := hrl.rateLimiter.Add(request)
	if err != nil && err != leakybucket.ErrorFull {
		log.Printf("[%s] ERROR: %s", guid, err)
		hrl.proxy.ServeHTTP(w, r)
		return
	}

	rateLimitResponse := flattenRateLimitHeaders(getRateLimitHeaders(matches))
	if err == leakybucket.ErrorFull {
		rateLimitResponse["limit"] = true
	} else {
		rateLimitResponse["limit"] = false
	}
	rateLimitResponse["guid"] = guid

	common.Log.InfoD("http-logger", common.ConcatWithRequest(rateLimitResponse, request))
	hrl.proxy.ServeHTTP(w, r)
}