Skip to content

Commit

Permalink
Set a NopCloser request body with retry middleware
Browse files Browse the repository at this point in the history
As the http client always closes the request body,
this makes sure the request can be retried if needed.

Fixes traefik#1008
  • Loading branch information
bamarni authored and emilevauge committed Feb 2, 2017
1 parent 1131a97 commit 86fd5b4
Showing 1 changed file with 8 additions and 0 deletions.
8 changes: 8 additions & 0 deletions middlewares/retry.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package middlewares
import (
"bufio"
"bytes"
"io/ioutil"
"net"
"net/http"

Expand Down Expand Up @@ -32,6 +33,13 @@ func NewRetry(attempts int, next http.Handler) *Retry {
}

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
for {
recorder := NewRecorder()
Expand Down

0 comments on commit 86fd5b4

Please sign in to comment.