2016-06-15 17:07:33 +00:00
|
|
|
package middlewares
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
|
|
|
"bytes"
|
2017-05-03 08:20:33 +00:00
|
|
|
"context"
|
2017-02-02 16:09:47 +00:00
|
|
|
"io/ioutil"
|
2016-06-15 17:07:33 +00:00
|
|
|
"net"
|
|
|
|
"net/http"
|
2016-12-30 08:21:13 +00:00
|
|
|
|
|
|
|
"github.com/containous/traefik/log"
|
|
|
|
"github.com/vulcand/oxy/utils"
|
2016-06-15 17:07:33 +00:00
|
|
|
)
|
|
|
|
|
2017-04-18 06:22:06 +00:00
|
|
|
// Compile time validation responseRecorder implements http interfaces correctly.
|
2016-08-03 12:50:52 +00:00
|
|
|
var (
|
2018-01-04 10:18:03 +00:00
|
|
|
_ Stateful = &retryResponseRecorderWithCloseNotify{}
|
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)
|
|
|
|
}
|
2016-06-15 17:07:33 +00:00
|
|
|
attempts := 1
|
|
|
|
for {
|
2017-05-03 08:20:33 +00:00
|
|
|
netErrorOccurred := false
|
|
|
|
// We pass in a pointer to netErrorOccurred so that we can set it to true on network errors
|
|
|
|
// when proxying the HTTP requests to the backends. This happens in the custom RecordingErrorHandler.
|
|
|
|
newCtx := context.WithValue(r.Context(), defaultNetErrCtxKey, &netErrorOccurred)
|
|
|
|
|
2018-01-04 10:18:03 +00:00
|
|
|
recorder := newRetryResponseRecorder(rw)
|
2017-05-03 08:20:33 +00:00
|
|
|
|
|
|
|
retry.next.ServeHTTP(recorder, r.WithContext(newCtx))
|
2017-09-20 16:40:03 +00:00
|
|
|
|
|
|
|
// It's a stream request and the body gets already sent to the client.
|
|
|
|
// Therefore we should not send the response a second time.
|
2018-01-04 10:18:03 +00:00
|
|
|
if recorder.IsStreamingResponseStarted() {
|
2018-01-02 15:02:03 +00:00
|
|
|
recorder.Flush()
|
2017-09-20 16:40:03 +00:00
|
|
|
break
|
|
|
|
}
|
|
|
|
|
2017-05-03 08:20:33 +00:00
|
|
|
if !netErrorOccurred || attempts >= retry.attempts {
|
2016-07-04 17:32:19 +00:00
|
|
|
utils.CopyHeaders(rw.Header(), recorder.Header())
|
2018-01-04 10:18:03 +00:00
|
|
|
rw.WriteHeader(recorder.GetCode())
|
|
|
|
rw.Write(recorder.GetBody().Bytes())
|
2016-06-15 17:07:33 +00:00
|
|
|
break
|
|
|
|
}
|
|
|
|
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-05-03 08:20:33 +00:00
|
|
|
// netErrorCtxKey is a custom type that is used as key for the context.
|
|
|
|
type netErrorCtxKey string
|
|
|
|
|
|
|
|
// defaultNetErrCtxKey is the actual key which value is used to record network errors.
|
|
|
|
var defaultNetErrCtxKey netErrorCtxKey = "NetErrCtxKey"
|
|
|
|
|
|
|
|
// NetErrorRecorder is an interface to record net errors.
|
|
|
|
type NetErrorRecorder interface {
|
|
|
|
// Record can be used to signal the retry middleware that an network error happened
|
|
|
|
// and therefore the request should be retried.
|
|
|
|
Record(ctx context.Context)
|
|
|
|
}
|
|
|
|
|
|
|
|
// DefaultNetErrorRecorder is the default NetErrorRecorder implementation.
|
|
|
|
type DefaultNetErrorRecorder struct{}
|
|
|
|
|
|
|
|
// Record is recording network errors by setting the context value for the defaultNetErrCtxKey to true.
|
|
|
|
func (DefaultNetErrorRecorder) Record(ctx context.Context) {
|
|
|
|
val := ctx.Value(defaultNetErrCtxKey)
|
|
|
|
|
|
|
|
if netErrorOccurred, isBoolPointer := val.(*bool); isBoolPointer {
|
|
|
|
*netErrorOccurred = true
|
|
|
|
}
|
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-04 10:18:03 +00:00
|
|
|
type retryResponseRecorder interface {
|
|
|
|
http.ResponseWriter
|
|
|
|
http.Flusher
|
|
|
|
GetCode() int
|
|
|
|
GetBody() *bytes.Buffer
|
|
|
|
IsStreamingResponseStarted() bool
|
|
|
|
}
|
|
|
|
|
|
|
|
// newRetryResponseRecorder returns an initialized retryResponseRecorder.
|
|
|
|
func newRetryResponseRecorder(rw http.ResponseWriter) retryResponseRecorder {
|
|
|
|
recorder := &retryResponseRecorderWithoutCloseNotify{
|
|
|
|
HeaderMap: make(http.Header),
|
|
|
|
Body: new(bytes.Buffer),
|
|
|
|
Code: http.StatusOK,
|
|
|
|
responseWriter: rw,
|
|
|
|
}
|
|
|
|
if _, ok := rw.(http.CloseNotifier); ok {
|
|
|
|
return &retryResponseRecorderWithCloseNotify{recorder}
|
|
|
|
}
|
|
|
|
return recorder
|
|
|
|
}
|
|
|
|
|
|
|
|
// retryResponseRecorderWithoutCloseNotify is an implementation of http.ResponseWriter that
|
2017-04-18 06:22:06 +00:00
|
|
|
// records its mutations for later inspection.
|
2018-01-04 10:18:03 +00:00
|
|
|
type retryResponseRecorderWithoutCloseNotify struct {
|
2016-06-15 17:07:33 +00:00
|
|
|
Code int // the HTTP response code from WriteHeader
|
|
|
|
HeaderMap http.Header // the HTTP response headers
|
|
|
|
Body *bytes.Buffer // if non-nil, the bytes.Buffer to append written data to
|
|
|
|
|
2017-09-20 16:40:03 +00:00
|
|
|
responseWriter http.ResponseWriter
|
|
|
|
err error
|
|
|
|
streamingResponseStarted bool
|
2016-06-15 17:07:33 +00:00
|
|
|
}
|
|
|
|
|
2018-01-04 10:18:03 +00:00
|
|
|
type retryResponseRecorderWithCloseNotify struct {
|
|
|
|
*retryResponseRecorderWithoutCloseNotify
|
|
|
|
}
|
|
|
|
|
|
|
|
// CloseNotify returns a channel that receives at most a
|
|
|
|
// single value (true) when the client connection has gone
|
|
|
|
// away.
|
|
|
|
func (rw *retryResponseRecorderWithCloseNotify) CloseNotify() <-chan bool {
|
|
|
|
return rw.responseWriter.(http.CloseNotifier).CloseNotify()
|
2016-06-15 17:07:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Header returns the response headers.
|
2018-01-04 10:18:03 +00:00
|
|
|
func (rw *retryResponseRecorderWithoutCloseNotify) Header() http.Header {
|
2016-06-15 17:07:33 +00:00
|
|
|
m := rw.HeaderMap
|
|
|
|
if m == nil {
|
|
|
|
m = make(http.Header)
|
|
|
|
rw.HeaderMap = m
|
|
|
|
}
|
|
|
|
return m
|
|
|
|
}
|
|
|
|
|
2018-01-04 10:18:03 +00:00
|
|
|
func (rw *retryResponseRecorderWithoutCloseNotify) GetCode() int {
|
|
|
|
return rw.Code
|
|
|
|
}
|
|
|
|
|
|
|
|
func (rw *retryResponseRecorderWithoutCloseNotify) GetBody() *bytes.Buffer {
|
|
|
|
return rw.Body
|
|
|
|
}
|
|
|
|
|
|
|
|
func (rw *retryResponseRecorderWithoutCloseNotify) IsStreamingResponseStarted() bool {
|
|
|
|
return rw.streamingResponseStarted
|
|
|
|
}
|
|
|
|
|
2016-06-15 17:07:33 +00:00
|
|
|
// Write always succeeds and writes to rw.Body, if not nil.
|
2018-01-04 10:18:03 +00:00
|
|
|
func (rw *retryResponseRecorderWithoutCloseNotify) Write(buf []byte) (int, error) {
|
2016-08-03 12:50:52 +00:00
|
|
|
if rw.err != nil {
|
|
|
|
return 0, rw.err
|
2016-06-15 17:07:33 +00:00
|
|
|
}
|
2016-08-03 12:50:52 +00:00
|
|
|
return rw.Body.Write(buf)
|
2016-06-15 17:07:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// WriteHeader sets rw.Code.
|
2018-01-04 10:18:03 +00:00
|
|
|
func (rw *retryResponseRecorderWithoutCloseNotify) WriteHeader(code int) {
|
2016-07-04 17:32:19 +00:00
|
|
|
rw.Code = code
|
2016-06-15 17:07:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Hijack hijacks the connection
|
2018-01-04 10:18:03 +00:00
|
|
|
func (rw *retryResponseRecorderWithoutCloseNotify) Hijack() (net.Conn, *bufio.ReadWriter, error) {
|
2016-06-15 17:07:33 +00:00
|
|
|
return rw.responseWriter.(http.Hijacker).Hijack()
|
|
|
|
}
|
2016-08-03 12:50:52 +00:00
|
|
|
|
|
|
|
// Flush sends any buffered data to the client.
|
2018-01-04 10:18:03 +00:00
|
|
|
func (rw *retryResponseRecorderWithoutCloseNotify) Flush() {
|
2017-09-20 16:40:03 +00:00
|
|
|
if !rw.streamingResponseStarted {
|
|
|
|
utils.CopyHeaders(rw.responseWriter.Header(), rw.Header())
|
|
|
|
rw.responseWriter.WriteHeader(rw.Code)
|
|
|
|
rw.streamingResponseStarted = true
|
|
|
|
}
|
|
|
|
|
2016-08-03 12:50:52 +00:00
|
|
|
_, err := rw.responseWriter.Write(rw.Body.Bytes())
|
|
|
|
if err != nil {
|
2017-04-18 06:22:06 +00:00
|
|
|
log.Errorf("Error writing response in retryResponseRecorder: %s", err)
|
2016-08-03 12:50:52 +00:00
|
|
|
rw.err = err
|
|
|
|
}
|
|
|
|
rw.Body.Reset()
|
|
|
|
flusher, ok := rw.responseWriter.(http.Flusher)
|
|
|
|
if ok {
|
|
|
|
flusher.Flush()
|
|
|
|
}
|
|
|
|
}
|