Exemple #1
0
/*
MakeHandlerAPI executes f.  A non nil bytes.Buffer is only
passed to f for GET requests. For GET request the response in
b is written to the client with gzipping.

When res.Code is not http.StatusOK the contents of res.Msg are written to w.

Surrogate-Control headers are also set for intermediate caches.
*/
func MakeHandlerAPI(f RequestHandler) http.HandlerFunc {
	return func(w http.ResponseWriter, r *http.Request) {
		t := mtrapp.Start()
		var res *Result

		switch r.Method {
		case "GET":
			b := bufferPool.Get().(*bytes.Buffer)
			defer bufferPool.Put(b)
			b.Reset()

			res = f(r, w.Header(), b)
			t.Stop()
			WriteBytes(w, r, res, b, false)
		default:
			res = f(r, w.Header(), nil)
			t.Stop()
			Write(w, r, res)
		}

		t.Track(name(f) + "." + r.Method)
		res.Count()

		// log errors and slow 200s
		if res.Code != http.StatusOK {
			log.Printf("status: %d serving %s", res.Code, r.RequestURI)
		} else if t.Taken() > 250 {
			log.Printf("slow: took %d ms serving %s", t.Taken(), r.RequestURI)
		}
	}
}
Exemple #2
0
/*
MakeHandler executes f and writes the response in b to the client
with gzipping and Surrogate-Control headers.

HTML error pages are written to the client when res.Code is not http.StatusOK.
*/
func MakeHandlerPage(f RequestHandler) http.HandlerFunc {
	return func(w http.ResponseWriter, r *http.Request) {
		t := mtrapp.Start()

		b := bufferPool.Get().(*bytes.Buffer)
		defer bufferPool.Put(b)
		b.Reset()

		res := f(r, w.Header(), b)
		t.Stop()
		WriteBytes(w, r, res, b, true)

		t.Track(name(f) + "." + r.Method)
		res.Count()

		res.log(r)

		if t.Taken() > 250 {
			log.Printf("slow: took %d ms serving %s", t.Taken(), r.RequestURI)
		}
	}
}