From 71cec1580b049594879d17f7eb9e97c22c58c38f Mon Sep 17 00:00:00 2001 From: Emile Vauge Date: Sat, 13 May 2017 19:02:06 +0200 Subject: [PATCH] Fix stats responseRecorder Hijacker Signed-off-by: Emile Vauge --- middlewares/retry.go | 5 +---- middlewares/stateful.go | 12 ++++++++++++ middlewares/stats.go | 23 +++++++++++++++++++++++ 3 files changed, 36 insertions(+), 4 deletions(-) create mode 100644 middlewares/stateful.go diff --git a/middlewares/retry.go b/middlewares/retry.go index 21568eb97..031490045 100644 --- a/middlewares/retry.go +++ b/middlewares/retry.go @@ -12,10 +12,7 @@ import ( ) var ( - _ http.ResponseWriter = &ResponseRecorder{} - _ http.Hijacker = &ResponseRecorder{} - _ http.Flusher = &ResponseRecorder{} - _ http.CloseNotifier = &ResponseRecorder{} + _ Stateful = &ResponseRecorder{} ) // Retry is a middleware that retries requests diff --git a/middlewares/stateful.go b/middlewares/stateful.go new file mode 100644 index 000000000..4762d97a1 --- /dev/null +++ b/middlewares/stateful.go @@ -0,0 +1,12 @@ +package middlewares + +import "net/http" + +// Stateful interface groups all http interfaces that must be +// implemented by a stateful middleware (ie: recorders) +type Stateful interface { + http.ResponseWriter + http.Hijacker + http.Flusher + http.CloseNotifier +} diff --git a/middlewares/stats.go b/middlewares/stats.go index c166799fc..faac75eba 100644 --- a/middlewares/stats.go +++ b/middlewares/stats.go @@ -1,11 +1,17 @@ package middlewares import ( + "bufio" + "net" "net/http" "sync" "time" ) +var ( + _ Stateful = &responseRecorder{} +) + // StatsRecorder is an optional middleware that records more details statistics // about requests and how they are processed. This currently consists of recent // requests that have caused errors (4xx and 5xx status codes), making it easy @@ -51,6 +57,23 @@ func (r *responseRecorder) WriteHeader(status int) { r.statusCode = status } +// Hijack hijacks the connection +func (r *responseRecorder) Hijack() (net.Conn, *bufio.ReadWriter, error) { + return r.ResponseWriter.(http.Hijacker).Hijack() +} + +// CloseNotify returns a channel that receives at most a +// single value (true) when the client connection has gone +// away. +func (r *responseRecorder) CloseNotify() <-chan bool { + return r.ResponseWriter.(http.CloseNotifier).CloseNotify() +} + +// Flush sends any buffered data to the client. +func (r *responseRecorder) Flush() { + r.ResponseWriter.(http.Flusher).Flush() +} + // ServeHTTP silently extracts information from the request and response as it // is processed. If the response is 4xx or 5xx, add it to the list of 10 most // recent errors.