2016-06-15 17:07:33 +00:00
|
|
|
package middlewares
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
2017-02-02 16:09:47 +00:00
|
|
|
"io/ioutil"
|
2016-06-15 17:07:33 +00:00
|
|
|
"net"
|
|
|
|
"net/http"
|
2018-06-19 11:56:04 +00:00
|
|
|
"net/http/httptrace"
|
2016-12-30 08:21:13 +00:00
|
|
|
|
|
|
|
"github.com/containous/traefik/log"
|
2016-06-15 17:07:33 +00:00
|
|
|
)
|
|
|
|
|
2018-01-26 17:22:03 +00:00
|
|
|
// Compile time validation that the response writer implements http interfaces correctly.
|
|
|
|
var _ Stateful = &retryResponseWriterWithCloseNotify{}
|
2016-08-03 12:50:52 +00:00
|
|
|
|
2016-06-15 17:07:33 +00:00
|
|
|
// Retry is a middleware that retries requests
|
|
|
|
type Retry struct {
|
|
|
|
attempts int
|
|
|
|
next http.Handler
|
2017-04-18 06:22:06 +00:00
|
|
|
listener RetryListener
|
2016-06-15 17:07:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewRetry returns a new Retry instance
|
2017-04-18 06:22:06 +00:00
|
|
|
func NewRetry(attempts int, next http.Handler, listener RetryListener) *Retry {
|
2016-06-15 17:07:33 +00:00
|
|
|
return &Retry{
|
|
|
|
attempts: attempts,
|
|
|
|
next: next,
|
2017-04-18 06:22:06 +00:00
|
|
|
listener: listener,
|
2016-06-15 17:07:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (retry *Retry) ServeHTTP(rw http.ResponseWriter, r *http.Request) {
|
2017-02-02 16:09:47 +00:00
|
|
|
// 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)
|
|
|
|
}
|
2018-01-26 17:22:03 +00:00
|
|
|
|
2016-06-15 17:07:33 +00:00
|
|
|
attempts := 1
|
|
|
|
for {
|
2018-06-19 11:56:04 +00:00
|
|
|
attemptsExhausted := attempts >= retry.attempts
|
|
|
|
// Websocket requests can't be retried at this point in time.
|
|
|
|
// This is due to the fact that gorilla/websocket doesn't use the request
|
|
|
|
// context and so we don't get httptrace information.
|
|
|
|
// Websocket clients should however retry on their own anyway.
|
|
|
|
shouldRetry := !attemptsExhausted && !isWebsocketRequest(r)
|
|
|
|
retryResponseWriter := newRetryResponseWriter(rw, shouldRetry)
|
|
|
|
|
|
|
|
// Disable retries when the backend already received request data
|
|
|
|
trace := &httptrace.ClientTrace{
|
|
|
|
WroteHeaders: func() {
|
|
|
|
retryResponseWriter.DisableRetries()
|
|
|
|
},
|
|
|
|
WroteRequest: func(httptrace.WroteRequestInfo) {
|
|
|
|
retryResponseWriter.DisableRetries()
|
|
|
|
},
|
|
|
|
}
|
|
|
|
newCtx := httptrace.WithClientTrace(r.Context(), trace)
|
2017-05-03 08:20:33 +00:00
|
|
|
|
2018-01-26 17:22:03 +00:00
|
|
|
retry.next.ServeHTTP(retryResponseWriter, r.WithContext(newCtx))
|
|
|
|
if !retryResponseWriter.ShouldRetry() {
|
2017-09-20 16:40:03 +00:00
|
|
|
break
|
|
|
|
}
|
|
|
|
|
2016-06-15 17:07:33 +00:00
|
|
|
attempts++
|
|
|
|
log.Debugf("New attempt %d for request: %v", attempts, r.URL)
|
2017-08-28 10:50:02 +00:00
|
|
|
retry.listener.Retried(r, attempts)
|
2016-06-15 17:07:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-18 06:22:06 +00:00
|
|
|
// RetryListener is used to inform about retry attempts.
|
|
|
|
type RetryListener interface {
|
|
|
|
// Retried will be called when a retry happens, with the request attempt passed to it.
|
|
|
|
// For the first retry this will be attempt 2.
|
2017-08-28 10:50:02 +00:00
|
|
|
Retried(req *http.Request, attempt int)
|
|
|
|
}
|
|
|
|
|
|
|
|
// RetryListeners is a convenience type to construct a list of RetryListener and notify
|
|
|
|
// each of them about a retry attempt.
|
|
|
|
type RetryListeners []RetryListener
|
|
|
|
|
|
|
|
// Retried exists to implement the RetryListener interface. It calls Retried on each of its slice entries.
|
|
|
|
func (l RetryListeners) Retried(req *http.Request, attempt int) {
|
|
|
|
for _, retryListener := range l {
|
|
|
|
retryListener.Retried(req, attempt)
|
|
|
|
}
|
2017-04-18 06:22:06 +00:00
|
|
|
}
|
|
|
|
|
2018-01-26 17:22:03 +00:00
|
|
|
type retryResponseWriter interface {
|
2018-01-04 10:18:03 +00:00
|
|
|
http.ResponseWriter
|
|
|
|
http.Flusher
|
2018-01-26 17:22:03 +00:00
|
|
|
ShouldRetry() bool
|
2018-06-19 11:56:04 +00:00
|
|
|
DisableRetries()
|
2018-01-26 17:22:03 +00:00
|
|
|
}
|
|
|
|
|
2018-06-19 11:56:04 +00:00
|
|
|
func newRetryResponseWriter(rw http.ResponseWriter, shouldRetry bool) retryResponseWriter {
|
2018-01-26 17:22:03 +00:00
|
|
|
responseWriter := &retryResponseWriterWithoutCloseNotify{
|
2018-06-19 11:56:04 +00:00
|
|
|
responseWriter: rw,
|
|
|
|
shouldRetry: shouldRetry,
|
2018-01-04 10:18:03 +00:00
|
|
|
}
|
|
|
|
if _, ok := rw.(http.CloseNotifier); ok {
|
2018-01-26 17:22:03 +00:00
|
|
|
return &retryResponseWriterWithCloseNotify{responseWriter}
|
2018-01-04 10:18:03 +00:00
|
|
|
}
|
2018-01-26 17:22:03 +00:00
|
|
|
return responseWriter
|
2016-06-15 17:07:33 +00:00
|
|
|
}
|
|
|
|
|
2018-01-26 17:22:03 +00:00
|
|
|
type retryResponseWriterWithoutCloseNotify struct {
|
2018-06-19 11:56:04 +00:00
|
|
|
responseWriter http.ResponseWriter
|
|
|
|
shouldRetry bool
|
2018-01-04 10:18:03 +00:00
|
|
|
}
|
|
|
|
|
2018-01-26 17:22:03 +00:00
|
|
|
func (rr *retryResponseWriterWithoutCloseNotify) ShouldRetry() bool {
|
2018-06-19 11:56:04 +00:00
|
|
|
return rr.shouldRetry
|
|
|
|
}
|
|
|
|
|
|
|
|
func (rr *retryResponseWriterWithoutCloseNotify) DisableRetries() {
|
|
|
|
rr.shouldRetry = false
|
2016-06-15 17:07:33 +00:00
|
|
|
}
|
|
|
|
|
2018-01-26 17:22:03 +00:00
|
|
|
func (rr *retryResponseWriterWithoutCloseNotify) Header() http.Header {
|
|
|
|
if rr.ShouldRetry() {
|
|
|
|
return make(http.Header)
|
2016-06-15 17:07:33 +00:00
|
|
|
}
|
2018-01-26 17:22:03 +00:00
|
|
|
return rr.responseWriter.Header()
|
2016-06-15 17:07:33 +00:00
|
|
|
}
|
|
|
|
|
2018-01-26 17:22:03 +00:00
|
|
|
func (rr *retryResponseWriterWithoutCloseNotify) Write(buf []byte) (int, error) {
|
|
|
|
if rr.ShouldRetry() {
|
|
|
|
return 0, nil
|
|
|
|
}
|
|
|
|
return rr.responseWriter.Write(buf)
|
2018-01-04 10:18:03 +00:00
|
|
|
}
|
|
|
|
|
2018-01-26 17:22:03 +00:00
|
|
|
func (rr *retryResponseWriterWithoutCloseNotify) WriteHeader(code int) {
|
2018-06-19 11:56:04 +00:00
|
|
|
if rr.ShouldRetry() && code == http.StatusServiceUnavailable {
|
|
|
|
// We get a 503 HTTP Status Code when there is no backend server in the pool
|
|
|
|
// to which the request could be sent. Also, note that rr.ShouldRetry()
|
2018-07-17 10:02:03 +00:00
|
|
|
// will never return true in case there was a connection established to
|
2018-06-19 11:56:04 +00:00
|
|
|
// the backend server and so we can be sure that the 503 was produced
|
|
|
|
// inside Traefik already and we don't have to retry in this cases.
|
|
|
|
rr.DisableRetries()
|
|
|
|
}
|
|
|
|
|
2018-01-26 17:22:03 +00:00
|
|
|
if rr.ShouldRetry() {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
rr.responseWriter.WriteHeader(code)
|
2018-01-04 10:18:03 +00:00
|
|
|
}
|
|
|
|
|
2018-01-26 17:22:03 +00:00
|
|
|
func (rr *retryResponseWriterWithoutCloseNotify) Hijack() (net.Conn, *bufio.ReadWriter, error) {
|
|
|
|
return rr.responseWriter.(http.Hijacker).Hijack()
|
2018-01-04 10:18:03 +00:00
|
|
|
}
|
|
|
|
|
2018-01-26 17:22:03 +00:00
|
|
|
func (rr *retryResponseWriterWithoutCloseNotify) Flush() {
|
|
|
|
if flusher, ok := rr.responseWriter.(http.Flusher); ok {
|
|
|
|
flusher.Flush()
|
2016-06-15 17:07:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-26 17:22:03 +00:00
|
|
|
type retryResponseWriterWithCloseNotify struct {
|
|
|
|
*retryResponseWriterWithoutCloseNotify
|
2016-06-15 17:07:33 +00:00
|
|
|
}
|
2016-08-03 12:50:52 +00:00
|
|
|
|
2018-01-26 17:22:03 +00:00
|
|
|
func (rr *retryResponseWriterWithCloseNotify) CloseNotify() <-chan bool {
|
|
|
|
return rr.responseWriter.(http.CloseNotifier).CloseNotify()
|
2016-08-03 12:50:52 +00:00
|
|
|
}
|