2018-11-14 09:18:03 +00:00
|
|
|
package retry
|
2017-04-18 06:22:06 +00:00
|
|
|
|
|
|
|
import (
|
2018-11-14 09:18:03 +00:00
|
|
|
"context"
|
2019-01-07 22:18:03 +00:00
|
|
|
"fmt"
|
2021-03-04 19:08:03 +00:00
|
|
|
"io"
|
2017-04-18 06:22:06 +00:00
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
2019-01-07 22:18:03 +00:00
|
|
|
"net/http/httptrace"
|
2018-08-29 09:58:03 +00:00
|
|
|
"strings"
|
2017-04-18 06:22:06 +00:00
|
|
|
"testing"
|
2020-11-05 15:14:04 +00:00
|
|
|
"time"
|
2018-06-19 11:56:04 +00:00
|
|
|
|
2018-08-29 09:58:03 +00:00
|
|
|
"github.com/gorilla/websocket"
|
2018-08-06 18:00:03 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
2018-08-29 09:58:03 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
2020-11-05 15:14:04 +00:00
|
|
|
ptypes "github.com/traefik/paerser/types"
|
2020-09-16 13:46:04 +00:00
|
|
|
"github.com/traefik/traefik/v2/pkg/config/dynamic"
|
|
|
|
"github.com/traefik/traefik/v2/pkg/testhelpers"
|
2017-04-18 06:22:06 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestRetry(t *testing.T) {
|
|
|
|
testCases := []struct {
|
2018-08-29 09:58:03 +00:00
|
|
|
desc string
|
2019-07-10 07:26:04 +00:00
|
|
|
config dynamic.Retry
|
2018-08-29 09:58:03 +00:00
|
|
|
wantRetryAttempts int
|
|
|
|
wantResponseStatus int
|
|
|
|
amountFaultyEndpoints int
|
2017-04-18 06:22:06 +00:00
|
|
|
}{
|
|
|
|
{
|
2018-06-19 11:56:04 +00:00
|
|
|
desc: "no retry on success",
|
2021-03-04 19:08:03 +00:00
|
|
|
config: dynamic.Retry{Attempts: 5},
|
2018-06-19 11:56:04 +00:00
|
|
|
wantRetryAttempts: 0,
|
|
|
|
wantResponseStatus: http.StatusOK,
|
|
|
|
amountFaultyEndpoints: 0,
|
|
|
|
},
|
2020-11-05 15:14:04 +00:00
|
|
|
{
|
|
|
|
desc: "no retry on success with backoff",
|
2021-03-04 19:08:03 +00:00
|
|
|
config: dynamic.Retry{Attempts: 5, InitialInterval: ptypes.Duration(time.Microsecond * 50)},
|
2020-11-05 15:14:04 +00:00
|
|
|
wantRetryAttempts: 0,
|
|
|
|
wantResponseStatus: http.StatusOK,
|
|
|
|
amountFaultyEndpoints: 0,
|
|
|
|
},
|
2018-06-19 11:56:04 +00:00
|
|
|
{
|
|
|
|
desc: "no retry when max request attempts is one",
|
2019-07-10 07:26:04 +00:00
|
|
|
config: dynamic.Retry{Attempts: 1},
|
2018-06-19 11:56:04 +00:00
|
|
|
wantRetryAttempts: 0,
|
2019-08-14 16:16:04 +00:00
|
|
|
wantResponseStatus: http.StatusBadGateway,
|
2018-06-19 11:56:04 +00:00
|
|
|
amountFaultyEndpoints: 1,
|
|
|
|
},
|
2020-11-05 15:14:04 +00:00
|
|
|
{
|
|
|
|
desc: "no retry when max request attempts is one with backoff",
|
|
|
|
config: dynamic.Retry{Attempts: 1, InitialInterval: ptypes.Duration(time.Microsecond * 50)},
|
|
|
|
wantRetryAttempts: 0,
|
|
|
|
wantResponseStatus: http.StatusBadGateway,
|
|
|
|
amountFaultyEndpoints: 1,
|
|
|
|
},
|
2018-06-19 11:56:04 +00:00
|
|
|
{
|
|
|
|
desc: "one retry when one server is faulty",
|
2019-07-10 07:26:04 +00:00
|
|
|
config: dynamic.Retry{Attempts: 2},
|
2018-06-19 11:56:04 +00:00
|
|
|
wantRetryAttempts: 1,
|
|
|
|
wantResponseStatus: http.StatusOK,
|
|
|
|
amountFaultyEndpoints: 1,
|
|
|
|
},
|
2020-11-05 15:14:04 +00:00
|
|
|
{
|
|
|
|
desc: "one retry when one server is faulty with backoff",
|
|
|
|
config: dynamic.Retry{Attempts: 2, InitialInterval: ptypes.Duration(time.Microsecond * 50)},
|
|
|
|
wantRetryAttempts: 1,
|
|
|
|
wantResponseStatus: http.StatusOK,
|
|
|
|
amountFaultyEndpoints: 1,
|
|
|
|
},
|
2018-06-19 11:56:04 +00:00
|
|
|
{
|
|
|
|
desc: "two retries when two servers are faulty",
|
2019-07-10 07:26:04 +00:00
|
|
|
config: dynamic.Retry{Attempts: 3},
|
2018-06-19 11:56:04 +00:00
|
|
|
wantRetryAttempts: 2,
|
|
|
|
wantResponseStatus: http.StatusOK,
|
|
|
|
amountFaultyEndpoints: 2,
|
|
|
|
},
|
2020-11-05 15:14:04 +00:00
|
|
|
{
|
|
|
|
desc: "two retries when two servers are faulty with backoff",
|
|
|
|
config: dynamic.Retry{Attempts: 3, InitialInterval: ptypes.Duration(time.Microsecond * 50)},
|
|
|
|
wantRetryAttempts: 2,
|
|
|
|
wantResponseStatus: http.StatusOK,
|
|
|
|
amountFaultyEndpoints: 2,
|
|
|
|
},
|
2018-06-19 11:56:04 +00:00
|
|
|
{
|
|
|
|
desc: "max attempts exhausted delivers the 5xx response",
|
2019-07-10 07:26:04 +00:00
|
|
|
config: dynamic.Retry{Attempts: 3},
|
2018-06-19 11:56:04 +00:00
|
|
|
wantRetryAttempts: 2,
|
2020-11-05 15:14:04 +00:00
|
|
|
wantResponseStatus: http.StatusBadGateway,
|
|
|
|
amountFaultyEndpoints: 3,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "max attempts exhausted delivers the 5xx response with backoff",
|
|
|
|
config: dynamic.Retry{Attempts: 3, InitialInterval: ptypes.Duration(time.Microsecond * 50)},
|
|
|
|
wantRetryAttempts: 2,
|
2019-08-14 16:16:04 +00:00
|
|
|
wantResponseStatus: http.StatusBadGateway,
|
2018-06-19 11:56:04 +00:00
|
|
|
amountFaultyEndpoints: 3,
|
2017-04-18 06:22:06 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2018-08-29 09:58:03 +00:00
|
|
|
for _, test := range testCases {
|
|
|
|
test := test
|
|
|
|
t.Run(test.desc, func(t *testing.T) {
|
2017-04-18 06:22:06 +00:00
|
|
|
t.Parallel()
|
|
|
|
|
2022-02-21 11:40:09 +00:00
|
|
|
retryAttempts := 0
|
2021-03-04 19:08:03 +00:00
|
|
|
next := http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
|
2022-02-21 11:40:09 +00:00
|
|
|
retryAttempts++
|
2018-06-19 11:56:04 +00:00
|
|
|
|
2022-02-21 11:40:09 +00:00
|
|
|
if retryAttempts > test.amountFaultyEndpoints {
|
2021-03-04 19:08:03 +00:00
|
|
|
// calls WroteHeaders on httptrace.
|
|
|
|
_ = r.Write(io.Discard)
|
2018-06-19 11:56:04 +00:00
|
|
|
|
2021-03-04 19:08:03 +00:00
|
|
|
rw.WriteHeader(http.StatusOK)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
rw.WriteHeader(http.StatusBadGateway)
|
|
|
|
})
|
2018-06-19 11:56:04 +00:00
|
|
|
|
|
|
|
retryListener := &countingRetryListener{}
|
2021-03-04 19:08:03 +00:00
|
|
|
retry, err := New(context.Background(), next, test.config, retryListener, "traefikTest")
|
2018-11-14 09:18:03 +00:00
|
|
|
require.NoError(t, err)
|
2017-04-18 06:22:06 +00:00
|
|
|
|
|
|
|
recorder := httptest.NewRecorder()
|
2018-06-19 11:56:04 +00:00
|
|
|
req := httptest.NewRequest(http.MethodGet, "http://localhost:3000/ok", nil)
|
|
|
|
|
|
|
|
retry.ServeHTTP(recorder, req)
|
2017-04-18 06:22:06 +00:00
|
|
|
|
2018-08-29 09:58:03 +00:00
|
|
|
assert.Equal(t, test.wantResponseStatus, recorder.Code)
|
|
|
|
assert.Equal(t, test.wantRetryAttempts, retryListener.timesCalled)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-14 09:18:03 +00:00
|
|
|
func TestRetryEmptyServerList(t *testing.T) {
|
2021-03-04 19:08:03 +00:00
|
|
|
next := http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
|
|
|
|
rw.WriteHeader(http.StatusServiceUnavailable)
|
|
|
|
})
|
2018-11-14 09:18:03 +00:00
|
|
|
|
|
|
|
retryListener := &countingRetryListener{}
|
2019-07-10 07:26:04 +00:00
|
|
|
retry, err := New(context.Background(), next, dynamic.Retry{Attempts: 3}, retryListener, "traefikTest")
|
2018-11-14 09:18:03 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
recorder := httptest.NewRecorder()
|
|
|
|
req := httptest.NewRequest(http.MethodGet, "http://localhost:3000/ok", nil)
|
|
|
|
|
|
|
|
retry.ServeHTTP(recorder, req)
|
|
|
|
|
|
|
|
assert.Equal(t, http.StatusServiceUnavailable, recorder.Code)
|
|
|
|
assert.Equal(t, 0, retryListener.timesCalled)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestRetryListeners(t *testing.T) {
|
|
|
|
req := httptest.NewRequest(http.MethodGet, "/", nil)
|
|
|
|
retryListeners := Listeners{&countingRetryListener{}, &countingRetryListener{}}
|
|
|
|
|
|
|
|
retryListeners.Retried(req, 1)
|
|
|
|
retryListeners.Retried(req, 1)
|
|
|
|
|
|
|
|
for _, retryListener := range retryListeners {
|
|
|
|
listener := retryListener.(*countingRetryListener)
|
|
|
|
if listener.timesCalled != 2 {
|
|
|
|
t.Errorf("retry listener was called %d time(s), want %d time(s)", listener.timesCalled, 2)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-07 22:18:03 +00:00
|
|
|
func TestMultipleRetriesShouldNotLooseHeaders(t *testing.T) {
|
|
|
|
attempt := 0
|
|
|
|
expectedHeaderName := "X-Foo-Test-2"
|
|
|
|
expectedHeaderValue := "bar"
|
|
|
|
|
|
|
|
next := http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {
|
|
|
|
headerName := fmt.Sprintf("X-Foo-Test-%d", attempt)
|
|
|
|
rw.Header().Add(headerName, expectedHeaderValue)
|
|
|
|
if attempt < 2 {
|
|
|
|
attempt++
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Request has been successfully written to backend
|
|
|
|
trace := httptrace.ContextClientTrace(req.Context())
|
|
|
|
trace.WroteHeaders()
|
|
|
|
|
|
|
|
// And we decide to answer to client
|
|
|
|
rw.WriteHeader(http.StatusNoContent)
|
|
|
|
})
|
|
|
|
|
2019-07-10 07:26:04 +00:00
|
|
|
retry, err := New(context.Background(), next, dynamic.Retry{Attempts: 3}, &countingRetryListener{}, "traefikTest")
|
2019-01-07 22:18:03 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
responseRecorder := httptest.NewRecorder()
|
|
|
|
retry.ServeHTTP(responseRecorder, testhelpers.MustNewRequest(http.MethodGet, "http://test", http.NoBody))
|
|
|
|
|
|
|
|
headerValue := responseRecorder.Header().Get(expectedHeaderName)
|
|
|
|
|
|
|
|
// Validate if we have the correct header
|
|
|
|
if headerValue != expectedHeaderValue {
|
|
|
|
t.Errorf("Expected to have %s for header %s, got %s", expectedHeaderValue, expectedHeaderName, headerValue)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Validate that we don't have headers from previous attempts
|
|
|
|
for i := 0; i < attempt; i++ {
|
|
|
|
headerName := fmt.Sprintf("X-Foo-Test-%d", i)
|
|
|
|
headerValue = responseRecorder.Header().Get("headerName")
|
|
|
|
if headerValue != "" {
|
|
|
|
t.Errorf("Expected no value for header %s, got %s", headerName, headerValue)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-14 09:18:03 +00:00
|
|
|
// countingRetryListener is a Listener implementation to count the times the Retried fn is called.
|
|
|
|
type countingRetryListener struct {
|
|
|
|
timesCalled int
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l *countingRetryListener) Retried(req *http.Request, attempt int) {
|
|
|
|
l.timesCalled++
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestRetryWithFlush(t *testing.T) {
|
|
|
|
next := http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {
|
|
|
|
rw.WriteHeader(200)
|
|
|
|
_, err := rw.Write([]byte("FULL "))
|
|
|
|
if err != nil {
|
|
|
|
http.Error(rw, err.Error(), http.StatusInternalServerError)
|
|
|
|
}
|
|
|
|
rw.(http.Flusher).Flush()
|
|
|
|
_, err = rw.Write([]byte("DATA"))
|
|
|
|
if err != nil {
|
|
|
|
http.Error(rw, err.Error(), http.StatusInternalServerError)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2019-07-10 07:26:04 +00:00
|
|
|
retry, err := New(context.Background(), next, dynamic.Retry{Attempts: 1}, &countingRetryListener{}, "traefikTest")
|
2018-11-14 09:18:03 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
responseRecorder := httptest.NewRecorder()
|
|
|
|
|
|
|
|
retry.ServeHTTP(responseRecorder, &http.Request{})
|
|
|
|
|
|
|
|
assert.Equal(t, "FULL DATA", responseRecorder.Body.String())
|
|
|
|
}
|
|
|
|
|
2018-08-29 09:58:03 +00:00
|
|
|
func TestRetryWebsocket(t *testing.T) {
|
|
|
|
testCases := []struct {
|
|
|
|
desc string
|
|
|
|
maxRequestAttempts int
|
|
|
|
expectedRetryAttempts int
|
|
|
|
expectedResponseStatus int
|
|
|
|
expectedError bool
|
|
|
|
amountFaultyEndpoints int
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
desc: "Switching ok after 2 retries",
|
|
|
|
maxRequestAttempts: 3,
|
|
|
|
expectedRetryAttempts: 2,
|
|
|
|
amountFaultyEndpoints: 2,
|
|
|
|
expectedResponseStatus: http.StatusSwitchingProtocols,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "Switching failed",
|
|
|
|
maxRequestAttempts: 2,
|
|
|
|
expectedRetryAttempts: 1,
|
|
|
|
amountFaultyEndpoints: 2,
|
|
|
|
expectedResponseStatus: http.StatusBadGateway,
|
|
|
|
expectedError: true,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range testCases {
|
|
|
|
test := test
|
|
|
|
t.Run(test.desc, func(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
2022-02-21 11:40:09 +00:00
|
|
|
retryAttempts := 0
|
2021-03-04 19:08:03 +00:00
|
|
|
next := http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
|
2022-02-21 11:40:09 +00:00
|
|
|
retryAttempts++
|
2018-08-29 09:58:03 +00:00
|
|
|
|
2022-02-21 11:40:09 +00:00
|
|
|
if retryAttempts > test.amountFaultyEndpoints {
|
2021-03-04 19:08:03 +00:00
|
|
|
upgrader := websocket.Upgrader{}
|
|
|
|
_, err := upgrader.Upgrade(rw, r, nil)
|
|
|
|
if err != nil {
|
|
|
|
http.Error(rw, err.Error(), http.StatusInternalServerError)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
2018-08-29 09:58:03 +00:00
|
|
|
|
2021-03-04 19:08:03 +00:00
|
|
|
rw.WriteHeader(http.StatusBadGateway)
|
|
|
|
})
|
2018-08-29 09:58:03 +00:00
|
|
|
|
|
|
|
retryListener := &countingRetryListener{}
|
2021-03-04 19:08:03 +00:00
|
|
|
retryH, err := New(context.Background(), next, dynamic.Retry{Attempts: test.maxRequestAttempts}, retryListener, "traefikTest")
|
2018-11-14 09:18:03 +00:00
|
|
|
require.NoError(t, err)
|
2018-08-29 09:58:03 +00:00
|
|
|
|
2018-11-14 09:18:03 +00:00
|
|
|
retryServer := httptest.NewServer(retryH)
|
2018-08-29 09:58:03 +00:00
|
|
|
|
|
|
|
url := strings.Replace(retryServer.URL, "http", "ws", 1)
|
|
|
|
_, response, err := websocket.DefaultDialer.Dial(url, nil)
|
|
|
|
|
|
|
|
if !test.expectedError {
|
|
|
|
require.NoError(t, err)
|
2017-04-18 06:22:06 +00:00
|
|
|
}
|
2018-08-29 09:58:03 +00:00
|
|
|
|
|
|
|
assert.Equal(t, test.expectedResponseStatus, response.StatusCode)
|
|
|
|
assert.Equal(t, test.expectedRetryAttempts, retryListener.timesCalled)
|
2017-04-18 06:22:06 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|