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:
parent
1131a972cd
commit
86fd5b4c97
1 changed files with 8 additions and 0 deletions
|
@ -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()
|
||||||
|
|
Loading…
Reference in a new issue