Use CloseNotifier to supress spurious HTTP 400 errors on early disconnect

When a client disconnects without completing a HTTP request, we were
attempting to process the partial request, which usually leads to a 400
error. These errors can pollute the logs and make it more difficult to
track down real bugs.

This change uses CloseNotifier to detect disconnects. In combination
with checking Content-Length, we can detect a disconnect before sending
the full payload, and avoid logging a 400 error.

This logic is only applied to PUT, POST, and PATCH endpoints, as these
are the places where disconnects during a request are most likely to
happen.

Signed-off-by: Aaron Lehmann <aaron.lehmann@docker.com>
This commit is contained in:
Aaron Lehmann 2015-07-27 10:00:00 -07:00
parent 1788ae4870
commit 6cb5670ba5
4 changed files with 83 additions and 4 deletions

View file

@ -103,8 +103,13 @@ func GetRequestID(ctx Context) string {
// WithResponseWriter returns a new context and response writer that makes
// interesting response statistics available within the context.
func WithResponseWriter(ctx Context, w http.ResponseWriter) (Context, http.ResponseWriter) {
closeNotifier, ok := w.(http.CloseNotifier)
if !ok {
panic("the ResponseWriter does not implement CloseNotifier")
}
irw := &instrumentedResponseWriter{
ResponseWriter: w,
CloseNotifier: closeNotifier,
Context: ctx,
}
@ -262,6 +267,7 @@ func (ctx *muxVarsContext) Value(key interface{}) interface{} {
// context.
type instrumentedResponseWriter struct {
http.ResponseWriter
http.CloseNotifier
Context
mu sync.Mutex