Set a NopCloser request body with retry middleware

As the http client always closes the request body,
this makes sure the request can be retried if needed.

Fixes #1008
This commit is contained in:
Bilal Amarni 2017-02-02 17:09:47 +01:00 committed by Emile Vauge
parent 1131a972cd
commit 86fd5b4c97
No known key found for this signature in database
GPG key ID: D808B4C167352E59

View file

@ -3,6 +3,7 @@ package middlewares
import ( import (
"bufio" "bufio"
"bytes" "bytes"
"io/ioutil"
"net" "net"
"net/http" "net/http"
@ -32,6 +33,13 @@ func NewRetry(attempts int, next http.Handler) *Retry {
} }
func (retry *Retry) ServeHTTP(rw http.ResponseWriter, r *http.Request) { func (retry *Retry) ServeHTTP(rw http.ResponseWriter, r *http.Request) {
// if we might make multiple attempts, swap the body for an ioutil.NopCloser
// cf https://github.com/containous/traefik/issues/1008
if retry.attempts > 1 {
body := r.Body
defer body.Close()
r.Body = ioutil.NopCloser(body)
}
attempts := 1 attempts := 1
for { for {
recorder := NewRecorder() recorder := NewRecorder()